[英]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.