条件测试时拒绝Shell脚本权限

hax*_*ode 1 unix linux shell

每当我尝试运行我的shell脚本时,我都会遇到4个不同的测试错误.

script.sh 45: script.sh: : Permission denied
script.sh 52: script.sh: : Permission denied
script.sh 59: script.sh: : Permission denied
script.sh 324: script.sh: : Permission denied
Run Code Online (Sandbox Code Playgroud)

我在我的脚本上做了一个chmod 777,所以每个人都有权限.脚本运行如下:

./script fileToUse
Run Code Online (Sandbox Code Playgroud)

以下是它给出错误消息的那些行看起来像:

if ( "$VAR" == "Condition_1" )
then
    do stuff
elif ( "$VAR" == "Condition_2" )
then 
    do stuff
elif ( "$VAR" == "Condition_3" )
then 
    do stuff
else
    do stuff
fi
Run Code Online (Sandbox Code Playgroud)

并且324行看起来像这样:

if ( "$flagIsSet" -eq 0 )
then
    do stuff
fi
Run Code Online (Sandbox Code Playgroud)

知道为什么我可能会收到此错误消息及其含义吗?

cho*_*oba 5

括号运行子shell.使用括号进行比较:

if [ "$VAR1" = "Condition_1" ] ; then
    # do stuff
fi
Run Code Online (Sandbox Code Playgroud)

  • 当`bash` 用作`/bin/sh` 时,它允许你使用`==` 代替`=`。然而,Ubuntu 使用不同的 shell,`dash` 作为它的 `/bin/sh`,它没有那么宽容。 (2认同)