我将以下脚本存储在一个文件中,并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 中也没有):
答案1
我似乎在(语句结束);
之前漏掉了 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 错误类似于“第 6 行和第 10 行中应该有分号”,我可能不会发布问题和答案。
似乎在 JavaScript 中编写 Bash if-then 语句比说的要冗长一些。