Dogga85
Posts: 28
Joined: Mon Dec 19, 2016 10:15 pm

Running/executing bash scripts

Tue Jun 25, 2019 4:36 am

Hey All,

I am trying to create and execute a simple bash script so that I can use wake on lan for my PC. I've created a 'bin' folder under home/pi, when I do a: echo $PATH I can see the /home/pi/bin: directory.
I have placed a file in there called wolpc, which contains code:
#!/bin/bash
wakeonlan xx:xx:xx:xx:xx:xx (MAC address obviously)

I've tried sudo chmod +x wolpc.sh to make it executable, and ./wolpc.sh , but all i get is 'No such file or directory'. What the hell am i doing wrong? lol

User avatar
rpdom
Posts: 15209
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Running/executing bash scripts

Tue Jun 25, 2019 5:01 am

Dogga85 wrote:
Tue Jun 25, 2019 4:36 am
I've tried sudo chmod +x wolpc.sh to make it executable, and ./wolpc.sh , but all i get is 'No such file or directory'. What the heck am i doing wrong? lol
If the file is in a directory specified in your PATH you just type the name of the file to run it, the same as any other command: wolpc.sh

You could also try /home/pi/bin/wolpc.sh

Actually, you say you created a file called "wolpc" and then mention "wolpc.sh". Which is it?

Verify the file is really in there and runnable with ls -l /home/pi/bin

Also, please post the full error message. It will be something like "bash: blah blah blah: No such file or directory". All of it is important to diagnose the issue.

Which editor did you use to create the bash script?

Dogga85
Posts: 28
Joined: Mon Dec 19, 2016 10:15 pm

Re: Running/executing bash scripts

Tue Jun 25, 2019 5:26 am

Thanks for the help, it's called wolpc.sh. I created the file with Nano
OK, why I type just:
wolpc.sh I get 'bash: /home/pi/bin/wolpc.sh: Permission denied'

sudo chmod +x wolpc.sh : i get 'chmod: cannot access 'wolpc.sh' : No such file or directory'

/home/pi/bin/wolpc.sh : i get: 'bash: /home/pi/bin/wolpc.sh: Permission denied'

Dogga85
Posts: 28
Joined: Mon Dec 19, 2016 10:15 pm

Re: Running/executing bash scripts

Tue Jun 25, 2019 5:30 am

I've tried recreating the file but same issue.
when I run ls -l /home/pi/bin I get results:

total 4
-rw-r--r-- 1 pi pi 41 Jun 25 15:21 wolpc.sh

User avatar
rpdom
Posts: 15209
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Running/executing bash scripts

Tue Jun 25, 2019 5:38 am

Code: Select all

chmod +x /home/pi/bin/wolpc.sh
That command doesn't need sudo, as you "pi" are the owner. You do need to specify the correct location of the file.

Now try typing wolpc.sh

Dogga85
Posts: 28
Joined: Mon Dec 19, 2016 10:15 pm

Re: Running/executing bash scripts

Tue Jun 25, 2019 5:43 am

thankyou! gosh all that because I had sudo. Can't wait to get the hang of linux commands!

Thanks again.

wildfire
Posts: 538
Joined: Sat Sep 03, 2016 10:39 am
Location: Dundee, Scotland

Re: Running/executing bash scripts

Tue Jun 25, 2019 7:01 am

Dogga85 wrote:
Tue Jun 25, 2019 5:43 am
all that because I had sudo.
Nothing to do with sudo in this case though certainly not advised unless required.

Read the latter part of rpdom's post, you did not specify the location of your script when using chmod.
E8 85 A2 64 C9 64 81 94 64 81 95 64 89 84 89 96 A3
Still NF Shirls

jbudd
Posts: 990
Joined: Mon Dec 16, 2013 10:23 am

Re: Running/executing bash scripts

Tue Jun 25, 2019 4:31 pm

When you try to run a command wolpc.sh the shell will look for the [executable] file in directories listed in the environment variable $PATH.
You can see the directory list with echo $PATH

But when you pass a filename to a command chmod +x wolpc.sh it expects the file to be in the current directory.
Otherwise you have to specify where it is, for example
chmod +x ~/bin/wolpc.sh The tilda means "my home directory"
or chmod +x /home/pi/bin/wolpc.sh This is a "fully qualified pathname" since it starts with / It's the unambiguous location of the file
or chmod +x bin/wolpc.sh NB bin/wolpc.sh is not the same as /bin/wolpc.sh!

edit - I managed to omit a ".sh" and for Linux wolpc is not the same thing as wolpc.sh

Dogga85
Posts: 28
Joined: Mon Dec 19, 2016 10:15 pm

Re: Running/executing bash scripts

Wed Jun 26, 2019 11:19 pm

Thanks so much for the help and info guys, love this forum

Return to “Beginners”