Linux 即使以 root 身份也无法执行 bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8095242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:08:58  来源:igfitidea点击:

Unable to execute bash scripts even as root?

linuxbashcentospermission-denied

提问by Marcin

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

我有一个奇怪的问题,即使是基本的 bash 脚本,我也无法执行:

#!/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:

我将它保存为 test.sh,然后执行 chmod 755 test.sh 并运行 ./test.sh 得到:

bash: ./test.sh: Permission denied

any ideas ?

有任何想法吗 ?

cheers

干杯

采纳答案by themel

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

如果您使用“noexec”选项挂载了文件系统,就会发生这种情况。你应该删除它。

回答by Stainedart

Try

尝试

ls -la

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

查看文件的实际权利和所有权。查看 chmod 命令是否真的有效。您可能想要更改所有权以及文件检查的模式:http: //www.tuxfiles.org/linuxhelp/fileowner.html

回答by DaveOfTheDogs

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.

尽管与此特定线程没有直接关系;如果文件来自 Windows 系统,则行尾可能有 CR/LF。这将影响文件中的所有行,包括初始执行行,并且在您查看文件时将不可见。

$ ./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$

要看到这一点,你可以 cat -A 文件: $ cat -A ./test.sh #!/bin/bash^M$ echo "me"^M$

To remove, use dos2unix.

要删除,请使用 dos2unix。

回答by Serem

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

使用 chmod +x ./test.sh它应该允许您运行它。

回答by jceifrig

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

此外,检查包含脚本的目录/文件系统是否已安装 nfs。root 不会从安装了 nfs 的位置运行脚本。

回答by Lan...

you need use ./test.shwhen 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 /dataand 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.

./test.sh当您在该文件的目录中时需要使用,如果没有,请尝试。PATH TO THE SCRIPT或者您可以将其复制到某个目录/data并为shell chmod,然后执行上述陡峭的操作。如果您仍然失败,没关系,因为我有同样的问题,我只是成功了一次。

回答by Abhinav Damarapati

Script needs be executable. Use this:

脚本需要可执行。用这个:

chmod +x <script-name>