Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
 

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: bash script problem< Next Oldest | Next Newest >
linyiji Offline





Group: Members
Posts: 5
Joined: Dec. 2005
Posted: Dec. 11 2005,19:45 QUOTE

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
Back to top
Profile PM 
linyiji Offline





Group: Members
Posts: 5
Joined: Dec. 2005
Posted: Dec. 11 2005,20:03 QUOTE

Sorry, I should not put on this Topic here. Can someone move this topic to "Other Help Topic" forum? Thanks!
Back to top
Profile PM 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Dec. 11 2005,20:03 QUOTE

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:
Code Sample
#!/bin/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"


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
linyiji Offline





Group: Members
Posts: 5
Joined: Dec. 2005
Posted: Dec. 11 2005,20:49 QUOTE

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
Back to top
Profile PM 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Dec. 12 2005,16:31 QUOTE

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.


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
4 replies since Dec. 11 2005,19:45 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

 
reply to topic new topic new poll
Quick Reply: bash script problem

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code