Ksh
使用 if else 條件將字元串分配給變數
我已經寫了程式碼。檢查變數值,如果是 GL,那麼 SQLGL 應該 XDOAPPL,如果是 AP,那麼 SQLAP 應該分配 XDOAPPL 變數。但它給了我錯誤。
APPL=$1 x=AP y=GL echo "Value of x = $x and y = $y." a=SQLAP b=SQLGL if [["$APPL" = "AP"]}; then XDOAPPL=${a} echo "AP XDOAPPL =$XDOAPPL" elif [["$APPL" = "GL"]]; then XDOAPPL=${b} echo "GL XDOAPPL =$XDOAPPL" else echo "Nothing to go" fi
使用時
[[ ]]
,必須在[[
or]]
和內容之間留一個空格。您還在第一個中輸入了錯誤 a}
而不是 a 。]``if
這是正確的程式碼:
APPL=$1 x=AP y=GL echo "Value of x = $x and y = $y." a=SQLAP b=SQLGL if [[ "$APPL" = "AP" ]]; then XDOAPPL=${a} echo "AP XDOAPPL = $XDOAPPL" elif [[ "$APPL" = "GL" ]]; then XDOAPPL=${b} echo "GL XDOAPPL = $XDOAPPL" else echo "Nothing to go" fi