Shell if语句中的意外运算符

Shell if语句中的意外运算符,shell,if-statement,operators,sh,Shell,If Statement,Operators,Sh,在下面的两行中,我得到了这个错误 怎么了 Debian Buster my.sh:101:[:!=:意外运算符 my.sh:103:[:!=:意外运算符 更新 您发布的脚本有各种问题,突出显示为: 但是,尽管存在这些问题,脚本实际上在Debian(Buster)的默认shell(即dash)上按预期运行。您可能正在运行非默认shell。因此,解决问题的最简单方法是 声明一个有效的 修复上面强调的问题 留给我们的是: !/bin/sh printf“\n是否继续下载?[y/n]” read-r

在下面的两行中,我得到了这个错误

怎么了

Debian Buster

my.sh:101:[:!=:意外运算符

my.sh:103:[:!=:意外运算符

更新
您发布的脚本有各种问题,突出显示为:

但是,尽管存在这些问题,脚本实际上在Debian(Buster)的默认shell(即
dash
)上按预期运行。您可能正在运行非默认shell。因此,解决问题的最简单方法是

  • 声明一个有效的
  • 修复上面强调的问题
留给我们的是:

!/bin/sh
printf“\n是否继续下载?[y/n]”
read-r继续
错误(){
printf>&2'%s\n'$@
出口1
}
如果[“$CONTINUE”!=y]&&[“$CONTINUE”!=n];则
错误“无效参数”
elif[“$CONTINUE”=n];然后
printf“\n下载已终止!\n”
出口
fi

(这也为未定义的
错误
调用添加了一个定义;根据需要进行替换。)

您实际使用的是Bash还是sh?这对解决方案产生了至关重要的影响。
if [ $CONTINUE != "y" ] && [ "$CONTINUE" != "n" ]; then

elif [ $CONTINUE = "n" ]; then
echo "\nContinue downloading? [y/n]"
read CONTINUE

#   Error: Invalid argument
if [ $CONTINUE != "y" ] && [ $CONTINUE != "n" ]; then
    error "Invalid argument"
elif [ $CONTINUE = "n" ]; then
    echo "\nDonwload terminated!"
    exit
fi
Line 1:
echo "\nContinue downloading? [y/n]"
     ^-- SC2028: echo may not expand escape sequences. Use printf.

Line 2:
read CONTINUE
^-- SC2162: read without -r will mangle backslashes.

Line 5:
if [ $CONTINUE != "y" ] && [ $CONTINUE != "n" ]; then
     ^-- SC2086: Double quote to prevent globbing and word splitting.
                             ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
if [ "$CONTINUE" != "y" ] && [ "$CONTINUE" != "n" ]; then

Line 7:
elif [ $CONTINUE = "n" ]; then
       ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
elif [ "$CONTINUE" = "n" ]; then

Line 8:
    echo "\nDonwload terminated!"
         ^-- SC2028: echo may not expand escape sequences. Use printf.