“如果"声明在Debian Linux上不起作用 [英] "if" statement won't work on Debian Linux

查看:51
本文介绍了“如果"声明在Debian Linux上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,其中包含以下"if"语句.问题是我无法在Debian上运行它(在Fedora和CentOS上运行良好).

I have a bash script which contains the following "if" statement. The problem is I can't get it to run on Debian (it runs fine on Fedora and CentOS).

if [ $1 == "--daily" ]  # <--- line 116
then
countDaily
elif [ $1 == "--monthly" ] # <--- line 119
then
countMonthly
fi

运行后:

sh /home/kris/countsc.sh --daily

我遇到错误:

/home/kris/countsc.sh: 116: [: --daily: unexpected operator
/home/kris/countsc.sh: 119: [: --daily: unexpected operator

推荐答案

由于您使用的是 sh ,而不是bash,因此应使用一个等号的 = 进行操作字符串比较,而不是双精度等于 == .在测试语句中双引号也是一个好习惯(尽管此错误并非由错误的引号引起).

Since you are using sh, and not bash, you should use a single equal = to do the string comparison, instead of the double equal ==. Also it is a good practice to double quote the variable in test statement (this error is not caused by incorrect quoting though).

比较应为:

if [ "$1" = "--daily" ]

elif [ "$1" = "--monthly" ]

这篇关于“如果"声明在Debian Linux上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆