Programming and Scripting :: bash script problem
Hi, Everyone,
I was trying to write a bash script file as below
#go.sh
cd /opt
But when I run ./go.sh, it give me error
:No such file or directory
How can I write a bash script file and use the cd command? Please help. Thank you!
Yiji
Sorry, I should not put on this Topic here. Can someone move this topic to "Other Help Topic" forum? Thanks!
First, the script needs to be executable:
Code Sample |
chmod +x go.sh |
or Code Sample |
chmod 755 go.sh |
Second, the script's first line should be the interpreter with which to run the script. Most commonly it would be sh:
Third, if you use "./" at the beginning of a command, this means that the command is found in your current directory. If the script is anywhere other than the directory in which you are at that time, it will not be found.
Fourth, if your script only does one command like that, it may be more convenient to create an alias instead of a script:
Code Sample |
alias go="cd /opt" |
Thank mikshaw's great help! I changed to #!/bin/sh and it works.
I am newbie for linux. I use "./go.sh" all the time. How can I make it as a global (not current directory) command? Thanks again.
Yiji
There are at least a couple of ways...
1) Open /home/dsl/.bashrc and add this line:
alias go="cd /opt" (or whatever command you want to run when you type "go").
This will work only in new shells...if you already have a shell open when you add the alias, the alias will not work in that shell.
2) Open /home/dsl/.bash_profile and add this line:
export PATH="/path/to/the/directory/containing/go.sh:$PATH"
You'll need to either source .bash_profile from your login shell (exit fluxbox and you'll be there) or log out and back in.
I usually create /home/dsl/bin and add that directory to my PATH...then anything i put in there will be found no matter where i go.
Next Page...
original here.