Bash:在脚本中运行时,`chmod a + x`不起作用

问题描述

我正在尝试通过脚本模拟更新过程。

我只关心更新脚本的退出代码({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并运行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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...