基本结构
注意:
- if 和 elif 语句后面要跟then才能开始执行语句块
- then 可以换行写, 写在同一行要加分号隔开, 建议写在同一行, 结构会更清晰
- 没有 else if 这种语句, 只能写 elif
if [ ]; then # if 语句块 elif [ ]; then # 语句块 else # 语句块 fi
条件标识符条件运算符 [] 与 [[]] 的不同之处
- [[]] 运算符只有bash中才有, 其他shell如ash, dash中是没有这个语法的
- [[]] 有 []中没有的功能, 比如比较两个字符串是否相等, 且字符串中有空格的时候, 使用 [] 时必须加双引号
if [ "$str" = "hello man" ]; then而使用 [[]] 时就不必担心这个问题if [[ $str = "hello man" ]]; then- 逻辑运算. 使用 [] 条件运算符时, &&, ||, <, > 这几个符号都会被当作命令的一部分, 而不是当作逻辑运算符, 使用 [[]] 条件运算符时就不会有这个问题, 如:
# 下面的 > 符号会被当作重定向命令, && 也作为命令的一部分, 会创建200 和 100两个文件, 且语句会报错: [: missing `]' if [ 300 > 200 && 200 > 100 ]; then # 下面的语句就可以正常的执行逻辑运算 if [[ 300 > 200 && 200 > 100 ]]; then- 模糊匹配. 使用[[]] 时可以使用=~运算符及正则语法实现模糊匹配如匹配 yes :
# 使用 [] 时只能使用如下写法 if [ $str = "y" -o $str = "yes" ];then # 使用 [[]] 时就可以有如下写法 if [[ $str =~ ^y(es)$ ]];then echo ${BASH_REMATCH[1]} # 匹配到的值存在这个系统变量中 ... # 还可以使用下面的通配符写法 if [[ $str = y* ]];then- 经验, 一般做判断时会使用 [] 条件运算符, 但在做字符串比较时, 最好用 [[]] 会降低出错概率
判断数字
if [ $a -eq $b ]; then # 等于 equal if [ $a -ne $b ]; then # 不等于 not equal if [ $a -gt $b ]; then # 大于 greater than if [ $a -ge $b ]; then # 大于等于 greater equal if [ $a -lt $b ]; then # 小于 little than if [ $a -le $b ]; then # 小于等于 little equal
判断字符串
# 使用 [] 运算符 if [ -z "$str" ]; then # 判断为空(字符串变量加引号) if [ -n "$str" ]; then # 判断不为空(字符串变量加引号) if [ "$str" = "abc" ]; then # 判断相等(字符串变量加引号) if [ "$str" == "abc" ]; then # 判断相等(字符串变量加引号) if [ "$str" != "abc" ]; then # 判断不相等(字符串变量加引号) if [ "$str1" > "$str2" ]; then # 判断大于(字符串变量加引号) if [ "$str1" < "$str2" ]; then # 判断小于 # 使用 [[]] 运算符 if [[ -z $str ]]; then # 判断为空 if [[ -n $str ]]; then # 判断不为空 if [[ $str = "ab c" ]]; then # 判断相等 if [[ $str == "ab c" ]]; then # 判断相等 if [[ $str != "ab c" ]]; then # 判断不相等 if [[ $str1 > $str2 ]]; then # 判断大于 if [[ $str1 < $str2 ]]; then # 判断小于
文件判断
if [ -e $file ]; then # 判断文件存在 if [ -L $file ]; then # 判断文件存在且是链接文件(要最先判断, 否则会先去判断软链接指向的文件的其他属性) if [ -s $file ]; then # 判断文件存在且是字符设备 if [ -b $file ]; then # 判断文件存在且是块设备文件 if [ -d $file ]; then # 判断文件存在且是文件夹/目录 if [ -f $file ]; then # 判断文件存在且是普通文件 if [ -S $file ]; then # 判断文件存在且是套接字文件 if [ -p $file ]; then # 判断文件存在且是管道文件 # 对文件权限的判断 if [ -r $file ]; then # 判断文件具有可读权限 if [ -w $file ]; then # 判断文件具有可写权限 if [ -x $file ]; then # 判断文件具有可执行权限
逻辑运算
# 使用 [] 时 if [ $a -o $b ];then # 或 or if [ $a -a $b ];then # 且 and if [ ! $a ];then # 非 if [ expression1 ] || [ expression2 ];then # 或 if [ expression1 ] && [ expression2 ];then # 且 if ! [ expression ] # 非 # 使用 [[]]时 if [[ $a && $b ]];then # 或 if [[ $a && $b ]];then # 且 if [[ ! $a ]];then # 非 if [[ expression1 ]] || [[ expression2 ]];then # 或 if [[ expression1 ]] && [[ expression2 ]];then # 且 if ! [[ expression ]] # 非switch case 选择
case ${var} in
[1-9]) # 范围
# do something
;;
[^a-zA-Z]) # 非此范围
# do something
;;
*)
#do something
;;
esac
select in 选择
select var in ch_a ch_b ch_c ch_d
do
#经常结合case in 语句使用
done
while 循环
while test
do
# 循环体
done
while true # 死循环的写法
do
done
for 循环
for ((i=0; i<=100; i++))
do
# 循环体
done
for var # 不加任何参数会循环所有的位置参数
do
done
for in 循环
for var in a b c d e
do
#
done
for var in {1..100}
do
#
done
break 与 continue
shell 中的break和continue与普通C系列程序中的同名关键词有着不同的行为
break n : 退出n层循环
continue n : 跳过n层循环, 执行



