【发布时间】:2012-01-09 17:44:54
【问题描述】:
我有一个接受用户输入的脚本,REFRESH 选项是可选的。我需要测试 $REFRESH 是否存在并且等于字符串“REFRESH”,如果它运行特定的代码块。
用户会执行
./export_data.sh <user> <type> [REFRESH]
如果我在 PHP 中执行此操作,我只需使用 isset() 函数,ksh 中是否存在等效函数?
我尝试了以下方法,但在第二次测试中失败了 $REFRESH 未设置:
if [ -n $REFRESH ] && [ $REFRESH == "REFRESH" ]
then
echo "variable is set and the expected value";
# do stuff
fi
我能想到的唯一其他方法是嵌套 if 但这看起来很乱:
if [ -n $REFRESH ]
then
if [ $REFRESH == "REFRESH" ]
then
echo "variable is set and the expected value";
# do stuff
fi
有没有更好的方法来做到这一点?
【问题讨论】: