Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to execute bash scripts even as root?

I have a weird problem, I cant execute bash script even as basic as:

#!/bin/bash
echo "me"

I am saving it as a test.sh and then do chmod 755 test.sh and once run ./test.sh getting:

bash: ./test.sh: Permission denied

Any ideas what could be causing this?

like image 961
Marcin Avatar asked Nov 11 '11 14:11

Marcin


People also ask

How do I run a bash script as root?

To give root privileges to a user while executing a shell script, we can use the sudo bash command with the shebang. This will run the shell script as a root user. Example: #!/usr/bin/sudo bash ....

Why is bash permission denied?

Usually, you get the error bash permission denied when running some script/file that does not have execute permissions. All you need to do is to change file permissions and add executive one.

How do I fix bash Permission denied in Ubuntu?

The Bash permission denied error indicates you are trying to execute a file which you do not have permission to run. To fix this issue, use the chmod u+x command to give yourself permissions. If you cannot use this command, you may need to contact your system administrator to get access to a file.

How do I know if a script is running as root?

Check the script is being run by root user #!/bin/bash # Init FILE="/tmp/out. $$" GREP="/bin/grep" #.... # Make sure only root can run our script if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1 fi # ...


2 Answers

That can happen if you have mounted the file system with the "noexec" option. You should remove it.

like image 187
themel Avatar answered Oct 08 '22 18:10

themel


Script needs be executable. Use this:

chmod +x <script-name>
like image 30
Abhinav Damarapati Avatar answered Oct 08 '22 18:10

Abhinav Damarapati