Shell流程控制

1.选择结构

1.1 单分支

格式一:
if 条件判断表达式;then
条件判断成立时执行的操作
……
……
fi
格式二:
if 条件判断表达式
then
条件判断成立时执行的操作
……
……
fi

#!/bin/bash
if [ $# = 0 ];then //出错的地方,数值比较用-eq等,而=用于字符比较
        echo "please input sth"
exit //exit为函数,无论在程序中的什么位置,只要执行到exit系统调用,进程就会停止剩下的所有操作,清除包括PCB在内的各种数据结构,并终止本进程的运行。
fi
host=$1
ping -c 3 $host &>/dev/null && echo "$1 is online" || echo "$1 is not online"

修改后

#!/bin/bash
if [ $# -eq 0 ];then#i/bin
        echo "please input sth"
        exit
fi
host=$1
ping -c 3 $host &>/dev/null && echo "$1 is online" || echo "$1 is not online"
1.2 双分支

if 条件判断表达式;then
条件判断成立时执行的操作
……
……
else
条件判断不成立时执行的操作
……
……
fi

例子:

#!/bin/bash
if [ $# -eq 0 ];then
        echo "please input sth"
        exit
else
        host=$1
        ping -c 3 $host &>/dev/null && echo "$1 is online" || echo "$1 is not online"
fi

嵌套的使用

#!/bin/bash
if [ $# -eq 0 ];then
        echo "please input sth"
        exit
else
        host=$1
 #ping -c 3 $host &>/dev/null && echo "$1 is online" || echo "$1 is not online"//#号表示注释
        ping -c 3 $host &> /dev/null
        if [ $? -eq 0 ];then
                echo "$1 is online"
        else
                echo "$1 is not online"
        fi
fi
1.3 多分支

if条件测试1;then
命令序列
……
elif条件测试2;then
命令序列
……
elif条件测试3;then
命令序列
……
elif条件测试3;then
命令序列
……
else
命令序列
fi

执行流程:从上向下开始匹配,

例子:对输入的分数,评分档次:100-90分(优秀)、89-80(良好)、79-70(中等)、69-60(及格)、<60(不及格)。

2.循环结构(重要)

反复执行的操作。
格式一:
for 变量名 in 变量列表
do
循环体
done

如:
for i in 1 2 3 4 5 6 7 8 9 10
do
echo $i
done

seq #Print numbers from FIRST to LAST, in steps of INCREMENT 打印一组数字的序列
fo

例子:
输出192.168.1.0/24网段内的前10个地址:

#!/bin/bash
network=192.168.1
#for i in 1 2 3 4 5 6 7 8 9 10 #不灵活
for i in $(seq 10)#等同于for i in `seq 10`
do
        echo $network.$i
done

格式二:
for((初始值;条件;kk))
do
循环体
done

例子:

#!/bin/bash
for((i=1;i<=10;i++))
do
        echo $illlkjkkkjj
done

#!/bin/bash
for((i=1;i<=10;i++))
do
        useradd jerry$i
        echo jerry$i | passwd --stdin jerry$i #(--stdin
This  option is used to indicate that passwd should read the new password from standard input, which can be a pipe)
done

存在问题:
[root@iLor shell_day02_am]# sh addUsers02.sh
Changing password for user jerry1.
passwd: all authentication tokens updated successfully.
Changing password for user jerry2.
passwd: all authentication tokens updated successfully.
Changing password for user jerry3.
passwd: all authentication tokens updated successfully.
Changing password for user jerry4.
passwd: all authentication tokens updated successfully.
Changing password for user jerry5.
passwd: all authentication tokens updated successfully.
Changing password for user jerry6.
passwd: all authentication tokens updated successfully.
Changing password for user jerry7.
passwd: all authentication tokens updated successfully.
Changing password for user jerry8.
passwd: all authentication tokens updated successfully.
Changing password for user jerry9.
passwd: all authentication tokens updated successfully.
Changing password for user jerry10.
passwd: all authentication tokens updated successfully.
优化后:

#!/bin/bash
for((i=1;i<=10;i++))
do
        useradd jerry$i
        echo jerry$i | passwd --stdin jerry$i &> /dev/null
done

格式三:条件成立时执行循环体
while 条件判断表达式
do
循环体
done

例子:

#!/bin/bash
i=1
while [ $i -le 10 ] #不要写成i<=10,因为这不是条件表达式的写法
do
        echo $i
        let i++ # i=`expr $i + 1`或者i=$(expr $i + 1)
done

练习:输出1-20所有奇数
格式四: 条件不成立时执行循环体
until 条件判断表达式
do
循环体
done

3.分支结构

(根据变量的不同值,执行不同的操作,脚本中很常见)
格式:
case “变量名” in
模式1) #模式1可以加双引号也可以不加 : “模式1”
命令序列1……;;
模式2)
命令序列2……;;
模式3)
命令序列3……;;
……
模式n)
命令序列n……;;
*)
默认命令序列
esac

例子:

4.流程控制关键字

break
continue

exit 结束脚本的运行(默认返回0 ,又如exit 3)

return 用于函数中,不能用于脚本中

shift 位置移动函数
把脚本或函数中的位置变量按顺序向左移动一位,并删除没有值的位置变量。

true: 在条件判断中时表示永远为真
false: 在条件判断中时表示永远为假
:在条件判断中时表示永远为真,在循环体中表示什么都不做。





posted @ 2019-01-30 17:27  whatislinux  阅读(100)  评论(0编辑  收藏  举报