在'script.sh'中找不到命令(chmod +x mybinary)。

10 浏览
0 Comments

在'script.sh'中找不到命令(chmod +x mybinary)。

我正在尝试创建一个bash脚本,用于检查二进制文件是否具有可执行位,如果没有,则自动添加。

当我执行脚本时,出现以下错误:script/repair.sh: 37: chmod: not found.

以下是代码:

PATH="/my/path/"
BIN="mybin"
if chmod +x $PATH$BIN != -1; then
    echo "已将可执行位添加到'$BIN'"
else
    echo "无法将可执行位添加到'$BIN'"  
fi

0
0 Comments

当运行脚本时,出现了 "Command not found (chmod +x mybinary) in 'script.sh'" 的错误提示。这个问题的原因是因为在脚本中修改了"PATH"变量的值,而"PATH"是一个用于搜索和执行文件的shell环境变量,因此无法再找到bin chmod命令。

要解决这个问题,可以采取以下方法:

1. 在脚本中使用绝对路径来执行chmod命令,而不是依赖于"PATH"变量:

if /bin/chmod +x "$P$BIN"; then
    echo "Added the executable bit to '$BIN'"
else
    echo "Couldn't add the executable bit to '$BIN'"
fi

2. 在更改"PATH"变量之前,将其备份并在使用chmod命令后进行恢复:

OLD_PATH=$PATH
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
if chmod +x "$P$BIN"; then
    echo "Added the executable bit to '$BIN'"
else
    echo "Couldn't add the executable bit to '$BIN'"
fi
export PATH=$OLD_PATH

通过采取上述方法,可以解决"Command not found (chmod +x mybinary) in 'script.sh'"的问题,脚本将能够找到并执行chmod命令。

0