[英]Shell script if statement always executes else block
我写了一个简单的if
- else
语句,并从 cronjob 中调用它。
cronscript.sh
#!/bin/sh
# Author : TEST USER
# Script follows here:
VAR1="-h"
VAR2="-h"
if [[ "$VAR1" == "$VAR2" ]]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
我总是得到 output 因为Strings are not equal.
即使两个字符串都相同,为什么它正在执行 ELSE 块?
cronjob 命令
34 18 05 * * /var/www/html/cronscript.sh > /var/www/html/testcron.txt
cronjob 正常执行,output 存储在 testcron.txt 文件中。
您的脚本具有 shebang #!/bin/sh
并尝试使用[[
,这是特定于 bash 的语法。 您应该更改 shebang:
#!/bin/bash
或者使用普通的 POSIX sh 条件语法:
if [ "$VAR1" = "$VAR2" ]; then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.