(probably) Dumb question for starter

yogaboy

Registered
Hi,

this is my first question on here, so hi to everyone.

Secondly, this may be a really dumb question, but I need help so please be gentle! :)

I'm trying to learn the commandline unixy stuff for osx. I've got this bash script I want to run

Code:
#! /bin/bash
echo "Hello from a bash script file."

I put it in a textedit file and save it as Documents\Bash Scripts\firstBashScript.sh

I open the Terminal, move to the dir and give the permissions
chmod +x firstBashScript.sh

everything seems aok so far.

then I try
firstBashScript

but Terminal says:
-bash: firstBashScript: command not found

I've tried chmod a+x and chmod u+x as I've seen various on the web, and then try running the script, but I still get the same error.

I just want to run the script - what do I need to do?????? Sorry, this is obviously so obvious that no one on the net seems to have explicitly written it down, it's all "now run the script"... :confused:
 
You'll need to point to the script specifically. Ensure you are in the script's directory and enter ./firstBashScript.

Notice the dot-slash prefix? A bit weird, but thats Unix for you.
 
I entered your code into 'TextEdit' and save it as 'firstBashScript' onto the 'Desktop'.

In 'Terminal' I entered ...

sudo bash

... pressed <return>, entered the administrator's password, and pressed <return>. Next I entered ...

mv $HOME/Desktop/firstBashScript /usr/sbin/; cd /usr/sbin/; chmod 755 firstBashScript

... and pressed <return>.

With the 'Terminal' window at '/usr/sbin/' I entered ...

firstBashScript

... pressed <return>, and was greeted with ...

'Hello from a bash script file.'
 
If the file is saved (not in the '/usr/bin/' or '/usr/sbin/' folders) as 'firstBashScript.sh' then you will be required to enter ...

./firstBashScript.sh

... <return>. If you rename the file to 'firstBashScript' then ...

./firstBashScript

... <return> will work.
 
It is not a good idea to install your own scripts with sudo while you are experimenting. In any case, you should not install non-system scripts in /usr/sbin or /usr/bin (or /bin or /sbin). Non-system installations usually go in /usr/local/bin or /usr/local/sbin. For your own scripts, though, it is best to avoid sudo and either give a full path (e.g. ./ from the directory as described by others) or create a special directory in your home directory for these scripts. I use /Users/myname/bin.

You can also then do this:
PATH=$PATH:/Users/myname/bin [return]
export PATH [return]
You can then use just
FirstBashScript
for the rest of your terminal session.

(Later, you may want to automate this to avoid typing it all the time, but that is probably something to leave until you have a little more experience with scripts.)

By the way, the sbin directories (as opposed to bin directories) are supposed to be for secure binaries - the sorts of things ordinary users shouldn't usually be running.

- cfr
 
If one enters (into 'Terminal') ...

$PATH

... <return>, they should see ...

/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin: No such file or directory

..., at least I do in 'Tiger' (MacOS X 10.4.4).

Thus, save 'firstBashScript' in the '/opt/local/bin/' directory (folder) instead. Therefore, one does not need to modify 'PATH'. Also, '/opt/local/bin/', and '/opt/local/sbin/', are not locked; thus, there is no need to enter 'sudo bash' )or equivalent) to add files to, or remove from, either.
 
This will only work if /opt/local/bin and /opt/local/sbin have already been added to path. If you've installed DarwinPorts, for example. The result given by barhar is not standard and not what most users would get. (My path does not have opt in it and this directory does not, indeed, exist on my system since I don't currently have DarwinPorts installed.)
- cfr
 
Back
Top