bash shell script - SOLVED!

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
wimpy
Posts: 406
Joined: Wed 22 Aug 2012, 10:30
Location: Essex, UK

bash shell script - SOLVED!

#1 Post by wimpy »

Just starting off working through a bash tutorial. The first problem. A "hello world" script (say hello.sh) can be easily copied and chmodded to make it executable. The tutorial says that typing hello.sh at the terminal should run the program. It doesn't. Comes back and says "No such command". But if I type "sh hello.sh" (without quotes) it runs fine. How can I turn a script into an executable file which runs without invoking the shell?
Any help would be greatly appreciated.
Last edited by wimpy on Wed 14 Nov 2012, 12:24, edited 1 time in total.
LxXenial16.08, LxPupSc17.07.01,Lucid 5.2.8 and others - all frugal

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: bash shell script

#2 Post by L18L »

wimpy wrote:...typing hello.sh at the terminal should run the program....
Yes it should !
if its directory is found in the PATH

try

Code: Select all

echo $PATH
is it?

No !

so start your script like so:

Code: Select all

./hello.sh
which will work if you are in the same directory as your script hello.sh

Howto find out in which directory you are type

Code: Select all

pwd
Hope that helps

User avatar
wimpy
Posts: 406
Joined: Wed 22 Aug 2012, 10:30
Location: Essex, UK

#3 Post by wimpy »

Thanks for the reply. The directory I was in was /root and the file was also in there. I checked with $PATH and /root is not there. So... moved the hello.sh file to usr/bin which is in $PATH and it worked fine. .Is the omission of the current directory from the $PATH a security thing or is it easy to add a .. to $PATH?
Of course your suggestion of ./hello.sh works fine as well.
LxXenial16.08, LxPupSc17.07.01,Lucid 5.2.8 and others - all frugal

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#4 Post by L18L »

wimpy wrote:...Is the omission of the current directory from the $PATH a security thing or is it easy to add a .. to $PATH?...
Yes it is a security thing.

Adding to the PATH is not recommended instead move your script to /root/my-applications/bin which is in the PATH

Code: Select all

PATH="another/directory:$PATH"
would prepend another/directory to the PATH

jsl06
Posts: 9
Joined: Fri 02 Mar 2012, 11:25

chmod

#5 Post by jsl06 »

Even though you mention chmod in in your post, you do not say that you performed chmod on the file. chmod makes the file eaecutable. Perform ls on the file. You should see an x in the description. If not, chmod +x hello.sh.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#6 Post by L18L »

I should have mentioned that typing the entire file name including the path is always succesful

Code: Select all

/root/hello.sh 
if hello.sh is in there again

you might also copy or just symlink it to that location

User avatar
wimpy
Posts: 406
Joined: Wed 22 Aug 2012, 10:30
Location: Essex, UK

#7 Post by wimpy »

@js106 Yes. I did a chmod +x .on the file and checked with a ls-la which gave
-rwxr-xr-x
Thanks for the reply.
@L18L You've removed what for me seemed to be a major roadblock.
Thanks. I'll now be able to continue with the tutorial. I'll mark this as solved
LxXenial16.08, LxPupSc17.07.01,Lucid 5.2.8 and others - all frugal

Post Reply