Bash: `chmod a+x` 在 运行 内部脚本时不起作用
Bash: `chmod a+x` Not Working When Run Inside Script
我正在尝试通过脚本模拟更新过程。
我只关心更新脚本的退出代码(0
表示成功,任何其他值表示失败)。
我创建了一个名为 update.sh
的简单脚本来模拟更新:
#!/bin/bash
# 1=true, 0=false
success=1
if [ $success -eq 1 ] ; then
# Success.
exit 0
else
# Failure.
exit 1
fi
为了模拟下载的更新,我将 update.sh
压缩到一个名为 update-file.zip
.
的文件中
我的主脚本提取 update-file.zip
和 运行s update.sh
:
#!/bin/bash
# Create a fresh update folder.
rm -rfv /test/update && mkdir /test/update
# Simulate download by copying zip file to that folder.
cp -rf /update-file.zip /test/update/
cd /test/update
unzip -o update-file.zip
# Make the update script executable.
updateFile="/test/update/update.sh"
chmod a+x $updateFile
# Run the update.
/test/update/update.sh
if [ $? -ne 0 ] ; then
echo "update failed"
else
echo "update success"
fi
# Delete update folder.
rm -rfv /test/update
但是,当我 运行 我的主脚本时(即使使用 sudo
),我收到以下错误消息:
/test/update/update.sh: Permission denied
甚至使用服务(使用 root
来做所有事情)都没有帮助,因为我仍然收到相同的错误消息。
似乎 chmod a+x
在脚本中 运行 时不起作用,因为如果我 运行 chmod a+x /test/update/update.sh
在终端上,一切正常。
每当脚本中的 chmod a+x
是 运行 时,我在尝试 运行 受影响的脚本时收到“权限被拒绝”错误。
令我困惑的是,当我 运行 ls -l /test/update/update.sh
时,我得到以下信息:
-rwxrwxrwx 1 root root 131 Sep 9 2020 /test/update/update.sh
无论 chmod a+x
是来自终端还是脚本的 运行,输出都是相同的。
我在 Ubuntu Server 20.04.1 LTS (Focal Fossa)
.
您可以显式 运行 文件的解释器:
bash /test/update/update.sh
此时无需使文件可执行。
你可以使用
/bin/bash /test/update/update.sh
而不是
/test/update/update.sh
在脚本中。
能否请您提供一次以下命令的输出。
ls -ld /test/update
我正在尝试通过脚本模拟更新过程。
我只关心更新脚本的退出代码(0
表示成功,任何其他值表示失败)。
我创建了一个名为 update.sh
的简单脚本来模拟更新:
#!/bin/bash
# 1=true, 0=false
success=1
if [ $success -eq 1 ] ; then
# Success.
exit 0
else
# Failure.
exit 1
fi
为了模拟下载的更新,我将 update.sh
压缩到一个名为 update-file.zip
.
我的主脚本提取 update-file.zip
和 运行s update.sh
:
#!/bin/bash
# Create a fresh update folder.
rm -rfv /test/update && mkdir /test/update
# Simulate download by copying zip file to that folder.
cp -rf /update-file.zip /test/update/
cd /test/update
unzip -o update-file.zip
# Make the update script executable.
updateFile="/test/update/update.sh"
chmod a+x $updateFile
# Run the update.
/test/update/update.sh
if [ $? -ne 0 ] ; then
echo "update failed"
else
echo "update success"
fi
# Delete update folder.
rm -rfv /test/update
但是,当我 运行 我的主脚本时(即使使用 sudo
),我收到以下错误消息:
/test/update/update.sh: Permission denied
甚至使用服务(使用 root
来做所有事情)都没有帮助,因为我仍然收到相同的错误消息。
似乎 chmod a+x
在脚本中 运行 时不起作用,因为如果我 运行 chmod a+x /test/update/update.sh
在终端上,一切正常。
每当脚本中的 chmod a+x
是 运行 时,我在尝试 运行 受影响的脚本时收到“权限被拒绝”错误。
令我困惑的是,当我 运行 ls -l /test/update/update.sh
时,我得到以下信息:
-rwxrwxrwx 1 root root 131 Sep 9 2020 /test/update/update.sh
无论 chmod a+x
是来自终端还是脚本的 运行,输出都是相同的。
我在 Ubuntu Server 20.04.1 LTS (Focal Fossa)
.
您可以显式 运行 文件的解释器:
bash /test/update/update.sh
此时无需使文件可执行。
你可以使用
/bin/bash /test/update/update.sh
而不是
/test/update/update.sh
在脚本中。
能否请您提供一次以下命令的输出。
ls -ld /test/update