#Method 1: assignment
VAR="unassigned"
if [ -z "$VAR" ]
then
echo "$VAR"
else
VAR="assigned"
echo "$VAR"
fi
#Method 2: assignment
[ -z "$VAR" ]&&(VAR="unassigned";echo "$VAR")||(VAR="assigned";echo "$VAR")
#Method 3: assignment
[ "$VAR" ]||(VAR="assigned";echo "$VAR")
#Method 4: assignment
[ ! "$VAR" ]&&(VAR="assigned";echo "$VAR")
# replacement 1
# = 没有定义时,才替换赋值;定义了就显示原值
#username1=""
echo "${username1=$LOGNAME}"
echo "$username1"
# replacement 2
# :=定义为非空值时,才不替换赋值;否则替换
#username2="i"
echo "${username2:=$LOGNAME}"
echo "$username2"
# replacement 3
# :-定义为非空值时,才不替换不赋值;否则替换不赋值
#username3=""
echo "${username3:-$LOGNAME}"
echo "$username3"
# replacement 4
# -未定义时,才替换但不会赋值
#username4=""
echo "${username4-$LOGNAME}"
echo "$username4"
# replacement 5
# :? 非真正值,脚本自行退出
username5="tt"
echo "${username5:?$LOGNAME}"
# replacement 6
# ? 非定义变量,脚本自行退出
username6=""
echo "${username6?$LOGNAME}"
# replacement 7
# :+ 非真正值,不替换
username7="tt"
echo "${username7:+$LOGNAME}"
echo "$username7"
# replacement 8
# + 定义就替换,但不赋值
username8=""
echo "${username8+$LOGNAME}"
echo $username8
阅读(1030) | 评论(0) | 转发(0) |