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

发布于 2024-12-15 00:01:22 字数 232 浏览 4 评论 0原文

我有一个奇怪的问题,我无法执行 bash 脚本,甚至像这样基本:

#!/bin/bash
echo "me"

我将其保存为 test.sh,然后执行 chmod 755 test.sh 并运行一次 ./test.sh 获取:

bash: ./test.sh: Permission denied

任何可能导致此问题的想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

无人问我粥可暖 2024-12-22 00:01:22

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

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

半步萧音过轻尘 2024-12-22 00:01:22

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

chmod +x <script-name>

Script needs be executable. Use this:

chmod +x <script-name>
孤千羽 2024-12-22 00:01:22

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

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

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

要删除,请使用 dos2unix。

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.

猫腻 2024-12-22 00:01:22

尝试

ls-la

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

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

墨小墨 2024-12-22 00:01:22

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

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

梦断已成空 2024-12-22 00:01:22

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

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

妄想挽回 2024-12-22 00:01:22

在 macOS 中,如果存在 com.apple.quarantine 标志,则可能会发生这种情况。如果在脚本路径上运行 ls -l 后看到权限上有 @ 后缀,请执行 ls -l@ *script_path* 进行确认。然后运行 ​​xattred -d com.apple.quarantine *script_path* 以删除隔离标志。

In macOS this can occur if a com.apple.quarantine flag exists. If you see a @ suffix on the permissions after running a ls -l on the script's path, execute ls -l@ *script_path* to confirm. Then run a xattred -d com.apple.quarantine *script_path* to remove the quarantine flag.

乜一 2024-12-22 00:01:22

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

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.

装迷糊 2024-12-22 00:01:22

对于默认使用 noexec 挂载的文件系统(例如 NFS),在末尾显式添加 exec 会有所帮助,即使前面提供了选项列表默认也暗示noexec,例如user 选项。

因此,如果您有以下选项之一:

  • noexec
  • user

将它们更改为:

  • exec
  • user,exec

它是将 exec 放在末尾很重要。如果您之前使用过 user 等其他选项,则仅删除 noexec 在某些情况下可能会有所帮助,但并非在所有情况下都有帮助。

For filesystems which are mounted with the noexec by default, for example NFS, explicitly adding exec at the end helps, even when options provided earlier in the list default imply noexec as well, e.g. the user option.

So if you have one of those options:

  • noexec
  • user

Change them to:

  • exec or
  • user,exec

It is important to place exec at the end. Just removing noexec may help in certain cases, but not in all, if you are using other options like user before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文