Why won't this bash script run?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
igor83

Why won't this bash script run?

Post by igor83 »

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

#!/bin/bash
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?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29606
Joined: Wed Jul 06, 2011 3:58 am

Re: Why won't this bash script run?

Post by xenopeek »

Post split from http://forums.linuxmint.com/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 ~ $ 
Image
baptiste

Re: Why won't this bash script run?

Post by baptiste »

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)
Locked

Return to “Scripts & Bash”