bash script problem

Forum: Programming and Scripting
Topic: bash script problem
started by: linyiji

Posted by linyiji on Dec. 11 2005,19:45
Hi, Everyone,

I was trying to write a bash script file as below
cd /opt

But when I run ./, 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!


Posted by linyiji on Dec. 11 2005,20:03
Sorry, I should not put on this Topic here. Can someone move this topic to "Other Help Topic" forum? Thanks!
Posted by mikshaw on Dec. 11 2005,20:03
First, the script needs to be executable:
Code Sample
chmod +x
Code Sample
chmod 755

Second, the script's first line should be the interpreter with which to run the script.  Most commonly it would be sh:
Code Sample

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"

Posted by linyiji on Dec. 11 2005,20:49
Thank mikshaw's great help! I changed to #!/bin/sh and it works.

I am newbie for linux. I use "./" all the time. How can I make it as a global (not current directory) command? Thanks again.


Posted by mikshaw on Dec. 12 2005,16:31
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/$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.

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.