分支与循环结构
一. if条件语句
1.单分支结构
语法:
if [条件]
then
指令
fi
或者
if [条件];then
指令
file
#提示:分号相当于命令换行
例1:单分支if条件句整数比较大小
[root@localhost ~]# cat com_num.sh #!/bin/bash a=$1 b=$2 #read -p "Pls input two number:" a b if [ $a -gt $b ];then echo "yes,$a > $b" fi if [ $a -le $b ];then echo "yes,$a <= $b" fi
例2:文件不存在,则创建
方式1: [root@localhost test]# cat test.sh #!/bin/bash FILEPATH="/root/test" if [ -e "$FILEPATH/f.sh" ];then echo "$FILEPATH/f.sh is exist." fi if [ ! -e "$FILEPATH/f.sh" ];then [ ! -d $FILEPATH ] && mkdir -p $FILEPATH [ -d $FILEPATH ] && { cd $FILEPATH touch f.sh echo "f.sh is touched!" } fi 方式2: [root@localhost test]# vim test.sh #!/bin/bash FILEPATH="/root/test" [ ! -d $FILEPATH ] && mkdir -p $FILEPATH cd $FILEPATH if [ -f $FILEPATH/f.sh ];then echo "f.sh is exits" exit 0 else touch $FILEPATH/f.sh echo "$FILEPATH/f.sh make cpmplete." fi
例3:判断内存大小,低于1000报警
[root@localhost test]# vim mem.sh #!/bin/bash cur_free=`free -m|awk '/buffers\// {print $NF}'` chars="current memory is $cur_free" if [$cur_free -lt 1000 ];then echo $chars echo $chars|mail -s "chars" mail.com fi #!/bin/bash gen_size=$(df / |awk '/\//{print $4}') #提取根分区剩余空间 mem_size=$(free |awk '/Mem/{print $4}') #提取内存剩余空间 while : do #注意内存和磁盘提取的空间大小都是以 Kb 为单位 if [ $gen_size -le 512000 -a $mem_size -le 1024000 ];then mail -s Warning root <<EOF Insufficient resources,资源不足 EOF fi done
2.双分支结构
语法
if 条件
then
指令
elif 条件
then
指令
else
指令
fi
例1:多分支判断大小
[root@localhost test]# cat num.sh #!/bin/bash read -p "Pls input two mumbers:" a b if [ $a -gt $b ];then echo "yes,$a > $b" elif [ $a -eq $b ];then echo "yes,$a = $b" else echo "yes,$a <= $b" fi
例2:多分支if语句实现对传入的参数进行判断
[root@localhost test]# cat num.sh #!/bin/bash a=$1 b=$2 if [ $# -ne 2 ];then echo "Usage:bash $0 num1 num2" exit 1 fi if [ $a -gt $b ];then echo "yes,$a > $b" elif [ $a -eq $b ];then echo "yes,$a = $b" else echo "yes,$a < $b" fi
例3:对参数个数以及类型严格判断#!/bin/bash
a=$1 b=$2 if [ $# -ne 2 ];then echo "Usage:bash $0 num1 num2" exit 1 fi [ -n "`echo $1|sed 's/[0-9]//g'`" ] && echo "第一个参数为数字" && exit 1 [ -n "`echo $2|sed 's/[0-9]//g'`" ] && echo "第二个参数为数字" && exit 11 if [ $a -gt $b ];then echo "Yes,$a > $b" elif [ $a -eq $b ];then echo "Yes,$a = $b" else echo "Yes,$a < $b" fi
二.case结构条件句
#语法: case "字符串变量" in 值1) 指令... ;; 值2) 指令... ;; * ) 指令... esac
例1:根据用户的输入判断是哪个数字
[root@localhost scripts]# cat num.sh #!/bin/bash read -p "Pls input a number:" num case "$num" in 1) echo "The num you input is 1" ;; 2) echo "The num you input is 2" ;; [3-9]) echo "The num you input is $num" ;; *) echo "The num input must be less 9" exit; ;; esac
例2:根据用户的选择输入判断是哪种水果并加上不同颜色
[root@localhost scripts]# cat fruit.sh #!/bin/bash RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' RES='\E[0m' read -p "Pls input the fruit name you like:" arg case "$arg" in apple|APPLE) echo -e "The fruit name you like is ${RED_COLOR}"$arg."${RES} " ;; banana|BANANA) echo -e "The fruit name you like is ${YELLOW_COLOR}"$arg."${RES} " ;; pear|PEAR) echo -e "The fruit name you like is ${GREEN_COLOR}"$arg."${RES} " ;; *) echo -e "Here is not the fruit name you like-- ${BLUE_COLOR}"$arg."${RES} " exit; esac
例3:传两个参数,给指定内容(第一个参数)加指定颜色(第二个参数)
[root@localhost scripts]# cat test.sh #!/bin/bash RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK_COLOR='\E[1;35m' RES='\E[0m' if [ $# -ne 2 ];then echo "Usage $0 content {red|yellow|blue|green}" exit fi case "$2" in red|RED) echo -e "${RED_COLOR}$1${RES}" ;; yellow|YELLOW) echo -e "${YELLOW_COLOR}$1${RES}" ;; green|GREEN) echo -e "${BLUE_COLOR}$1${RES}" ;; blue|BLUE) echo -e "${BLUE_COLOR}$1${RES}" ;; pink|PINK) echo -e "${PINK_COLOR}$1${RES}" ;; *) echo -e "${PINK_COLOR}$1${RES}" esac
例4:给指定内容添加颜色
[root@localhost scripts]# cat test.sh #!/bin/bash color_func(){ RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK_COLOR='\E[1;35m' RES='\E[0m' if [ $# -ne 2 ];then echo "Usage $0 content {red|yellow|blue|green}" exit fi case "$2" in red|RED) echo -e "${RED_COLOR}$1${RES}" ;; yellow|YELLOW) echo -e "${YELLOW_COLOR}$1${RES}" ;; green|GREEN) echo -e "${BLUE_COLOR}$1${RES}" ;; blue|BLUE) echo -e "${BLUE_COLOR}$1${RES}" ;; pink|PINK) echo -e "${PINK_COLOR}$1${RES}" ;; *) echo -e "${PINK_COLOR}$1${RES}" esac } color_func hello yellow color_func world blue
三.while循环和until循环
当型和直到循环(while循环和until循环)
1.while条件句
语法: while 条件 do 指令 done
2.until语句
until 条件 do 指令 done #until 了解
例1.屏幕输出
[root@localhost scripts]# cat test.sh #!/bin/bash while true do uptime sleep 2 done
例2.追加到log里,使用微秒单位
[root@localhost scripts]# cat t.sh #!/bin/bash while [ 1 ] do uptime >>./uptime.log usleep 1000000 #以微妙为单位 1000000=1秒 done [root@localhost scripts]# bash t.sh & #后台运行(提示:在后台永久执行,称为守护进程模式) [root@localhost scripts]# fg #调前台 bash t.sh [root@localhost scripts]# jobs [1]+ Running bash t.sh & [root@localhost scripts]# fg 1 bash t.sh
Linux 技巧:让进程在后台可靠运行的几种方法
https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/
例3:
通过while语句计算从1加到100之和
1.while 计算
[root@localhost scripts]# cat sum.sh #!/bin/bash i=1 sum=0 while ((i <= 100)) do ((sum=sum+i)) ((i++)) done echo "sum=$sum" #[ -n "$sum" ] && printf "totalsum is: $sum\n" #使用 printf 输出
2.通过数学公式
[root@localhost scripts]# cat sum.sh #!/bin/bash i=100 ((sum=i*(i+1)/2)) echo $sum
例4:使用while循环打印10,9,8...1
1.双小括号和双中括号
[root@localhost scripts]# cat pri.sh #!/bin/bash i=10 while ((i>0)) #双小括号 do echo $i ((i--)) done [root@localhost scripts]# cat pri.sh #!/bin/bash i=10 while [[ $i > 0 ]] #双中括号 do echo $i ((i--)) done
2.脚本传参
root@localhost scripts]# cat arg.sh #!/bin/bash i="$1" while [ $i -gt 0 ] do echo $i ((i--)) done #while是条件满足执行,until是条件满足退出 [root@localhost scripts]# cat until.sh #!/bin/bash i=1 until ((i>100)) do ((sum=sum+i)) ((i++)) done [ -n "$sum" ] && echo $sum
四. for 循环结构
语法:
for 变量名 in 变量取值列表 do 指令 done 提示:在此结构中"in 变量取值列表"可省略,省略是相当于in"$@",使用 for i 就相当于使用 for i in "$@"
例1:打印5-1
[root@localhost scripts]# cat f.sh #!/bin/bash for i in {5..1} do echo $i done #使用seq [root@localhost scripts]# vim f.sh #!/bin/bash for i in `seq -s " " 5 -1 1` do echo $i done
例2:打印目录
[root@localhost ~]# cat dir.sh #!/bin/bash for i in `ls -F|grep /` do echo $i done
[root@localhost scripts]# ls
1.txt 2.txt 3.txt 4.txt
1.手动测试 [root@localhost scripts]# a=4.txt [root@localhost scripts]# mv $a `echo $a|cut -d . -f1`.JPG [root@localhost scripts]# ls 1.txt 2.txt 3.txt 4.JPG 2.脚本处理 [root@localhost scripts]# cat cf.sh #!/bin/bash for i in `ls *.txt` do mv $i `echo $i|cut -d . -f1`.gif done [root@localhost scripts]# bash cf.sh [root@localhost scripts]# ls 1.gif 2.gif 3.gif 4.JPG 3,使用rename [root@localhost scripts]# ls 1.gif 2.gif 3.gif 4.TXT [root@localhost scripts]# cat cf.sh #!/bin/bash for i in `ls *.gif` do rename .gif .HTML $i done [root@localhost scripts]# bash cf.sh [root@localhost scripts]# ls 1.HTML 2.HTML 3.HTML 4.TXT cf.sh
例3:ls和awk
[root@localhost scripts]# ls *.jpg stu_102999_1_finished.jpg stu_102999_3_finished.jpg stu_102999_2_finished.jpg stu_102999_4_finished.jpg [root@localhost scripts]# ls *.jpg|awk -F '_finished' '{print "mv " $0" "$1".jpg "}' mv stu_102999_1_finished.jpg stu_102999_1.jpg mv stu_102999_2_finished.jpg stu_102999_2.jpg mv stu_102999_3_finished.jpg stu_102999_3.jpg mv stu_102999_4_finished.jpg stu_102999_4.jpg [root@localhost scripts]# [root@localhost scripts]# ls *.jpg|awk -F '_finished' '{print "mv " $0" "$1".jpg "}' |bash [root@localhost scripts]# ls *.jpg stu_102999_1.jpg stu_102999_3.jpg stu_102999_2.jpg stu_102999_4.jpg #[root@localhost scripts]# ls *.jpg |awk -F '_finished' '{print "mv "$0" "$1$2" "}' |bash #另一个写法
例4:打印9*9
1.双for循环
[root@localhost scripts]# cat f9.sh
#!/bin/bash
for a in `seq 1 9`
do
for b in `seq 1 9`
do
if [ $a -ge $b ];then
echo -en "$a x $b = $(expr $a \* $b) "
fi
done
echo " "
done
2.简化写法
[root@localhost scripts]# cat f9.sh #!/bin/bash for a in `seq 9` do for b in `seq 9` do [ $a -ge $b ] && echo -en "$a x $b = $(expr $a \* $b) " done echo " " done
例5:计算从1加到100
1.for [root@localhost scripts]# cat sum.sh #!/bin/bash for((i=0;i<=100;i++)) do ((j=j+i)) done echo $j 2.while [root@localhost scripts]# cat sum.sh #!/bin/bash i=0 while((i<=100)) do ((j=j+i)) ((i++)) done echo $j 3.使用公式 root@localhost scripts]# cat sum.sh #!/bin/bash i=100 ((j=i*(i+1)/2)) echo $j 提示:使用循环效率不佳,此类计算,使用数学公式会更快

浙公网安备 33010602011771号