Running bash scripts from the command line


10 posts
by tomexx » Wed Dec 19, 2012 1:55 pm
Hi,
I've been playing with simple bash scripts and noticed that I can call them directly from crontab like:

Code: Select all
* * * * myscript.sh

however when I want to run the script from the command line, I have to preceed the script name with 'bash' like:

Code: Select all
$ bash myscript.sh

I thought that the first line of the script ( #!/bin/bash ) meant that I don't need to call bash explicitelly.

Thanks,
Tom
Posts: 106
Joined: Thu Nov 01, 2012 4:58 pm
Location: Kitchener, ON, Canada, Earth
by sdjf » Wed Dec 19, 2012 2:03 pm
If you want to run a script that is located in your home directory and it is not in your "path", then you need to give it's location and also make it an executable file.

To make sure it is executable, type:

chmod +x myscript.sh

And to run it, type:

./myscript.sh

Maybe you will find some helpful tips on the following tutorial for writing short shell scripts:

http://sdjf.esmartdesign.com/scripts/shortsh.html

I will try to cook up some more examples specific to the Pi when I can.
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1292
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by tomexx » Wed Dec 19, 2012 2:59 pm
Thanks for the replay sdjf, but I have done all that and ./myscript.sh is not working.
I can only invoke the script by typiing $ bash myscript.sh

(I'll read the the shortsh.html as it looks promissing)
Tom
Posts: 106
Joined: Thu Nov 01, 2012 4:58 pm
Location: Kitchener, ON, Canada, Earth
by sdjf » Wed Dec 19, 2012 3:25 pm
Then you had better say what you mean by not working. IF you get "command not found" that means it is not running. Otherwise, it is a syntax or logical error.

test syntax with:

bash -n myscript.sh

And view what it is doing, to examine logic errors, with:

bash -x myscript.sh

Oh, where is the script located? And where are you trying to run it from?
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1292
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by Joe Schmoe » Wed Dec 19, 2012 3:46 pm
You gotta admit - this is a new one. The usual complaint is:

Works on command line, not in cron.

This is the first time I've heard the opposite of that.

Anyway, I'm sure it all boils down to one of the usual things about cron and/or the need for "./"

As an aside, do note that if the file "foo" exists in the current directory, either of these will run it:

sh foo

./foo

(I.e., if you use the "sh" prefix, then you don't need the "./")
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2619
Joined: Sun Jan 15, 2012 1:11 pm
by tomexx » Wed Dec 19, 2012 3:48 pm
I was getting
-bash: command not found

It's bizarre. Yesterday I couldn't get any of my scripts to run from the command line without the word 'bash'.
Now it works fine using ./myscript.sh

It might have something to do with the fact that I was modifying my scripts in notepad then SSH-ing them over to the PI.

Thanks,
Tom
Posts: 106
Joined: Thu Nov 01, 2012 4:58 pm
Location: Kitchener, ON, Canada, Earth
by SN » Wed Dec 19, 2012 3:50 pm
Does the first line of your script have

#!/bin/bash

in it?

When a shell encounters is asked to run an exectuable file, if its not a binary, it examines the first line to see what to run it with.
Steve N – binatone mk4->intellivision->zx81->spectrum->cbm64->cpc6128->520stfm->pc->raspi ?
User avatar
Posts: 1008
Joined: Mon Feb 13, 2012 8:06 pm
Location: Romiley, UK
by sdjf » Wed Dec 19, 2012 4:39 pm
If you ssh'd a new version over with scp, then you probably would have had to change the permission to executable again. New file replacing the Old I think would not automatically take on the permissions of the old file.
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1292
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by efflandt » Thu Dec 20, 2012 6:23 am
One potential issue when editing scripts in Windows is that DOS/Windows uses different line endings than *nix systems. Windows uses carriage return (Ctrl+M, ^M, or \r) and newline (Ctrl+J, ^J, or \n) pairs, and Linux only uses the newline. So sometimes the problem is not that the shell cannot find /bin/bash in the shebang line, it is that it cannot find "/bin/bash^M", but the ^M is invisible as a carriage return and may even overwrite part of the error message making the problem tough to figure out.

Try this for example:
Code: Select all
echo -e "how is that\rno not really"


Whereas, following both appear to output the same even though one has carriage return and one does not:
Code: Select all
echo -e "line 1\r\nline 2"
echo -e "line 1\nline 2"


If you mkdir bin in your home user directory, the next time you login the default .profile will automatically include /home/your_username/bin in your $PATH (at least in the text console), so that is a good place to put personal scripts to run by script name without any path (as long as the 1st line is a proper shebang line). What I have not figured out is what strips my ~/bin from my PATH when I startx (ie, in lxterminal).
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA
by -rst- » Thu Dec 20, 2012 3:31 pm
And someone mentioned something about Notepad adding some extra character codes at the start of the file...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 898
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland