Why won't this bash script run?

Forum rules
Before you post please read how to get help

Why won't this bash script run?

Postby igor83 » Sun Oct 28, 2012 10:00 pm

I opened a terminal and created a bash script as follows:
sudo pluma ssoff.sh

In ssoff.sh, I entered the following :

Code: Select all

echo yo yo

I saved, then did a chmod a+x ssoff.sh.
Then I tried to execute via ssoff.sh.
"Command not found."
Then I tried to execute via ./ssoff.sh.
"Permission denied."
I did sudo ./ssoff.sh and entered my password.
This time no error message was displayed, but nothing happened. No text appeared at all. The next command line prompt appeared ready for my next command. It was as if I had pressed the Enter key.

Apparently there is some trick that is eluding me to getting shell scripts to work in Linux. I have read 3-4 online guides and followed their directions and inserted all kinds of text into the shell scripts but nothing ever shows up. I have inserted syntax errors into shell scripts, such as "Eat at McDonalds" and not even an error message shows up. Is there a step I am missing?

User avatar
Level 24
Level 24
Posts: 21649
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Why won't this bash script run?

Postby xenopeek » Mon Oct 29, 2012 2:50 am

Post split from viewtopic.php?f=213&t=77062.

Not sure what went wrong. First thing to note is that it is a bit strange to do "sudo pluma", unless you want the file to be owned by root. And because Pluma has a GUI, normally you'd use "gksudo pluma" instead if you are editing a file owned by root. Because the file is owned by root, you would also have to do "sudo chmod a+x ssoff.sh" instead of the "chmod a+x ssoff.sh" you wrote.

I created the file, changed owner to root, gave execute permission to all, and then ran it. Works fine. So you must have missed some step, I'm thinking the permission thingy noted above.

Code: Select all

vincent@maya ~ $ echo '#!/bin/bash' >> ssoff.sh
vincent@maya ~ $ echo 'echo yo yo' >> ssoff.sh
vincent@maya ~ $ sudo chown root:root ssoff.sh
[sudo] password for vincent:
vincent@maya ~ $ sudo chmod a+x ssoff.sh
vincent@maya ~ $ ls -l ssoff.sh
-rwxr-xr-x 1 root root 23 Oct 29 07:47 ssoff.sh
vincent@maya ~ $ ./ssoff.sh
yo yo
vincent@maya ~ $

Level 3
Level 3
Posts: 104
Joined: Sun Jan 22, 2012 12:25 am

Re: Why won't this bash script run?

Postby baptiste » Wed Oct 31, 2012 8:40 pm

Did you merely forgot to set the read permission? Don't forget to check it. Though, I did similar tests as Vincent and by default files do are created with reading permission for all.. "umask" is 0022.
So, doing what you did just worked.

The thing is, you have absolutely no reason to create shell scripts as root unless you really need to. Just work from your user directory with files owned by you. If a script is so useful you want other users to be able to run it, you may copy it as root under /usr/local/bin for instance. Feels like you've created a new command :mrgreen:.
If your script does root things, I would log in as root (or use su or sudo su), then write it from the /root directory, using nano as an editor (or well, if you want pluma or leafpad so be it)

Return to “Scripts & Bash”