LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-18-2003, 12:57 PM   #1
kbeaver
Member
 
Registered: Jul 2003
Location: Lakewood, CO
Distribution: SuSE 8.2
Posts: 41

Rep: Reputation: 15
Bash scripting


Got a quick question:
I am trying to run scripts in Red Hat 9.0, I include the #!/bin/bash directive, so I try to run the script my typing its name, nothing. If I type "bash script_name" it will run fine. Any way to get around the bash command?
 
Old 07-18-2003, 01:06 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
What error do you get?
What are the permissions on the file? Make sure it is executable by the user you are running it as.

Is it in your path? If not then cd to the dir it is in and run:
./script_name

Last edited by david_ross; 07-18-2003 at 01:08 PM.
 
Old 07-18-2003, 01:35 PM   #3
kbeaver
Member
 
Registered: Jul 2003
Location: Lakewood, CO
Distribution: SuSE 8.2
Posts: 41

Original Poster
Rep: Reputation: 15
I do not get any error, it just hard returns back to by prompt. I set the file permission to 777, and I am in the correct directory. The ./ worked...I thought there was a way to make it work by just typing the file name...am I wrong?
 
Old 07-18-2003, 01:41 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Yes you need to put it somewhere in your path. eg:
/bin

So the script would be:
/bin/your_script

then running:
your_script will work.
 
Old 07-18-2003, 06:58 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
To be more precise, you either need to specify the directory where the script is (e.g. ./your_script, or /home/yourname/your_script, etc.) or put it in a directory listed in the PATH environment variable (seperated by colons ':'). Normally the directories /usr/local/bin:/usr/bin:/bin are in the PATH var. This is arranged by your distribution. To see the directories where the shell will find the script by itself through the PATH var, type:

echo $PATH

If you put your script in one of the directories you see, you can just type its name (without ./ ).

You can also include a directory of your choice to the PATH var, say the directory "myscripts" in your home directory. Make sure this dir exists, put you script there, and type:

export PATH=${PATH}:${HOME}/myscripts

By convention a directory named "bin" is used for this. Some distributions arrange for the bin directory as a subdir of your homedir to be included automatically. This is done by some lines in your ~/.bashrc or ~/.bash_profile or maybe ~/.profile. These lines resemble somthing like:

# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
PATH=~/bin:"${PATH}"
fi

To have you custom dir "/home/yourname/myscripts" permanently in the PATH var. Add the following line at the end of your ~/.bashrc or ~/bash_profile:

export PATH=${PATH}:${HOME}/myscripts

Hope this helps.

Last edited by Hko; 07-18-2003 at 06:59 PM.
 
Old 07-18-2003, 08:35 PM   #6
gabriele_101
Member
 
Registered: Oct 2001
Location: CAMBRIDGE, MA USA
Distribution: RH9 Kernel 2.4.20-18.9
Posts: 69

Rep: Reputation: 15
FYI: You can also put "." in your PATH and then the current directory is searched, much like in DOS. Generally, however, this is not recommended, though I personally prefer it for my non-root development user.

e.g. put in your ~/.bashrc:

PATH=$PATH:.

Or something of the sort. If you do put "." in your PATH, you should make it the last thing in the PATH.

-G
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM


All times are GMT -5. The time now is 04:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration