Bash
帶有 if-then 語句的 Bash 函式中意外標記“}”附近的語法錯誤
我將以下腳本儲存在一個文件中,並
alias
在使用者的文件中創建了一個該文件bashrc
,然後獲取該文件bashrc
:#!/bin/bash domain="$1" && test -z "$domain" && exit 2 environment() { read -sp "Please enter the app DB root password:" dbrootp_1 && echo read -sp "Please enter the app DB root password again:" dbrootp_2 && echo if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi read -sp "Please enter the app DB user password:" dbuserp_1 && echo read -sp "Please enter the app DB user password again:" dbuserp_2 && echo if [ "$dbuserp_1" != "$dbuserp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi } environment
當我執行時,
alias domain
我得到這個輸出:+ /user/nwsm/nwsm.sh x /user/nwsm/nwsm.sh: line 12: syntax error near unexpected token `}' user/nwsm/nwsm.sh: line 12: `}'
為什麼是語法錯誤?我沒有發現語法錯誤。你?也許還有另一個問題。
我在 Nano(也沒有在 Visual Studio Code)中看不到任何外星人字元:
我似乎在(語句結束
;
)之前錯過了 2 個分號 ( )。fi``if
這些是正確的 if-then 語句:
if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2; fi if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2; fi
注意每
fi
行末尾附近的每個分號。如果 stderr 中的 Bash 錯誤類似於*“Expected a semicolon in lines 6 and 10”*,我可能不會發布問題和答案。
似乎在 JavaScript 中編寫 Bash if-then 語句比說更冗長。