简体   繁体   English

为什么直接执行/ shell / bin / sh或/ usr / bin / bash时,shell脚本不能运行?

[英]Why shell script won't run when executed directly, but runs with /usr/bin/sh or /usr/bin/bash?

Shell script file dummy.sh with -rw-r--r-- permission, runs fine with below commands. 具有-rw-r--r--权限的Shell脚本文件dummy.sh可以通过以下命令正常运行。

/usr/bin/sh dummy.sh

(OR) (要么)

/usr/bin/bash dummy.sh

But ends up with bash: ./dummy.sh: Permission denied error, when executed directly as below. 但是最终以bash开头:./dummy.sh:按以下所示直接执行时, 权限被拒绝错误。 What's the reason behind this? 这是什么原因呢?

./dummy.sh

Your Script needs to be marked as executable for your system. 您的脚本需要标记为系统可执行文件。 This is done by setting the "x" bit for either the owner, the group or the rest of the world. 这是通过为所有者,组或世界其他地方设置“ x”位来完成的。 See: Wikipedia - Unix permissions By executing 请参阅: Wikipedia-Unix权限通过执行

chmod 755 dummy.sh

you will set read, write and execute permissions for the owner of the script and read and execute permissions for the group and the rest of the world. 您将为脚本所有者设置读取,写入和执行权限,并为组和世界其他地方设置读取和执行权限。

Provide "execute" permission to your shell script, using either of the following options: 使用以下两个选项之一为您的shell脚本提供“执行”权限:

  1. chmod 744 dummy.sh chmod 744 dummy.sh
  2. chmod u+x dummy.sh chmod u + x dummy.sh

Do refer to chmod documentation 请参考chmod文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM