我写了一个 bash 脚本,但由于我是一个自学者 bash 菜鸟,我想问我是否可以更有效地检查给定的参数。我也用谷歌搜索了这个问题并在这里检查了主题,但到目前为止我看到的例子太复杂了。在 python3 中,有很多更简单的方法,但我想在 bash 中它有点复杂。
#!/bin/bash
ERR_MSG="You did not give the argument required"
if [[ ${1?$ERR_MSG} == "a" ]]; then
echo "ABC"
elif [[ ${1?$ERR_MSG} == "b" ]]; then
echo "123"
elif [[ ${1?$ERR_MSG} == "c" ]]; then
echo ".*?"
else
echo "You did not provide the argument correctly"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)