0

I'm creating a script that when is run create various alias for various scripts that are in other folders. The script and the other folders are inside a specific folder as shown in the image but it is gonna be executable only when I want too. Assuming this is only gonna be to execute on this machine i don't have to change paths.

I got this in the script that runs perfectly, prints the echo and everything but the alias isn't created. Now if i just do the same alias line out of the script it creates the alias perfectly.

This script I'm creating is sh does it have any influence on this situation ?

Now i only want to use alias because this folder is going to stay in that machine and i'm not going to have other people running these.

What i want is to be able to instead of going to the folders and run the executables i want this script to create the alias so i can call them directly through the prompt like$~ zenmap and it runs.

#!/bin/bash

alias zenmap="/home/user/Desktop/folder/nmap/zenmap/zenmap"
echo "zenmap imported !"

Any clue on what can be happening ?

10
  • Using functions instead of aliases is probably better unless this is only ever intended for interactive use. Commented Oct 27, 2015 at 17:53
  • @EtanReisner Yes It is to for example : i got a script in folder b that deletes everything I only want to execute him when i writte examplingb in the prompt. Commented Oct 27, 2015 at 18:01
  • If you want these aliases for interactive shell usage only then that's fine (though a function works too and doesn't hurt anything). Commented Oct 27, 2015 at 18:13
  • @EtanReisner yes but I'm currently running into a problem that when i run it from the script the alias isn't created but the echo's are printing. Commented Oct 27, 2015 at 18:19
  • Yes, like I said. "interactive shell usage only". A script doesn't count. Use a function. Commented Oct 27, 2015 at 18:25

2 Answers 2

3

You should source your aliases script rather than simply running it. i.e.

source script.sh

or

. script.sh
Sign up to request clarification or add additional context in comments.

6 Comments

The thing is that i after i run this script i want to call the alias and not executing them for example : i got a script in folder b that deletes everything I only want to execute him when i writte examplingb in the prompt
So source script.sh; examplingb should work. Maybe I am missing the question. Can you please indicate the sequence of commands you wish to run and where it fails?
I don't want them to execute, I was looking for a way to create like a symbolic link to the various scripts through the various folders.
@Trusthefllow Do you want a symlink (on disk in some other location) or do you want an alias (in the shell)?
@EtanReisner I'm looking for alias because they only exist during the session. Don't symbolic links get stored till they get removed ?
|
0

From your comments in jayant answer it seems you are confusing when functions get executed. So a little example:

file_with_alias.sh

alias do_this="do_some_function"
" sourcing the file will make the function available but not execute it!
source file_with_function.sh

" This will only create the alias but not execute it.
alias execute_script="./path/to/script_that_does_something.sh"

file_with_function.sh

do_some_function(){
  echo "look ma! i'm doing things!"
}

script_that_does_something.sh

echo "Doing something directly!"

Now when you source . file_with_alias.sh the function will not be executed, only the alias generated. You will need to execute the alias do_this or call the function for it to work.

$ source file_with_alias.sh
$ do_this
Look ma! I'm doing things!
$ execute_script
Doing something directly!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.