Unable to execute bash scripts even as root?

跟風遠走 提交于 2020-01-12 06:59:21

问题


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 ?

cheers


回答1:


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




回答2:


Script needs be executable. Use this:

chmod +x <script-name>



回答3:


Although not directly pertinent to this particular thread; if a file has come form a Windows system there may be a CR/LF at the end of the line. This would affect all lines in the file, including the initial execution line, and would not be visible if you are viewing the file.

$ ./test.sh 
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

To see this, you could cat -A the file: $ cat -A ./test.sh #!/bin/bash^M$ echo "me"^M$

To remove, use dos2unix.




回答4:


Try

ls -la

to see the actual rights and ownership of the file. To see if the chmod command actually worked. You might want to change the ownership along with the mod of the file check : http://www.tuxfiles.org/linuxhelp/fileowner.html




回答5:


Use chmod +x ./test.sh this should allow you to run it.




回答6:


Also, check to see if the directory/filesystem containing the script is nfs-mounted. root won't run scripts from nfs-mounted locations.




回答7:


you need use ./test.sh when you in the directory of that file,if you don't,try PATH TO THE SCRIPT.or you can copy it to some directory of /data and chmod it for shell,then do the above steeps.if you still fail,it's ok because i have a same problem,i just did it success for once time.



来源:https://stackoverflow.com/questions/8095242/unable-to-execute-bash-scripts-even-as-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!