我已經搜索,並嘗試了許多解決方案,但對殼新來說,我迷路了。我在一個.sh中設置一個變量,調用一秒,然後嘗試根據它是否爲真。殼有條件的現象
testA.sh
#!/bin/sh
DB=1
. testB.sh
------------------------------------
testB.sh
if ${DB}==1; then
echo "DB is on"
fi
我找不到1 == 1命令。
我已經搜索,並嘗試了許多解決方案,但對殼新來說,我迷路了。我在一個.sh中設置一個變量,調用一秒,然後嘗試根據它是否爲真。殼有條件的現象
testA.sh
#!/bin/sh
DB=1
. testB.sh
------------------------------------
testB.sh
if ${DB}==1; then
echo "DB is on"
fi
我找不到1 == 1命令。
無論是否變量設置,標準方式是:
if test "${DB+set}" = set; then # here, $DB is set fi
如果要檢查它被設置爲一個特定的字符串值,使用:
if test "$DB" = value; ...
,如果你想檢查是否有特定的整數值:
if test "$DB" -eq 4; ...
最後一種形式如果DB是一個看起來不像整數的字符串值,將會生成一條錯誤消息。
這不是你做的Bash equality testing的方式......
testB.sh
if test ${DB} == 1; then
echo "DB is on"
fi
應該是:
if [[ "$DB" = "1"]]; then
或
if [[ $DB -eq 1 ]]; then
perl與此有什麼關係?我將刪除標籤。 – ChristopheD