1 -- if语法:if 后面接条件判断语句,条件成立逻辑使用then,如果then和if在同一行,使用;(分号)隔开,不在一行则不用;
if [ -d "a" ]; then
echo "sss"
elif [-f "a" ]
then
echo "sss"
fi
例一:只使用简单的if,else成立后的语句
#! /bin/sh
if [ -f /bin/bash ]
then echo "/bin/bash is a file"
else echo "/bin/bash is not a file"
fi
例二:使用if,elif,else
#! /bin/sh
echo "Is it monday? yes or no"
read YES_OR_NO
if [ "$YES_OR_NO" = "yes" ];then
echo "good mornong"
elif [ "$YES_OR_NO" = "no" ];then
echo "good afternoon"
else
echo "sorry"
exit 1
fi
exit 0
2 -- case语法 case不像if,没有then的使用,换行使用就好,但是每一个条件成立后的语句结尾要使用;;(双分号)
#! /bin/sh
echo "Is it morning? yes or no?"
read YES_OR_NO
case "$YES_OR_NO" in
yes|y|YES)
echo "yes, good morning";;
no|n|NO)
echo "no, good afternoon";;
*)
echo "sorry"
exit 1;;
esac
exit 0
本文详细介绍了Shell脚本中if和case两种条件语句的用法,并通过具体示例展示了如何进行条件判断和选择执行不同的代码块。适用于初学者理解和掌握Shell脚本的基本控制结构。
7180

被折叠的 条评论
为什么被折叠?



