5.32 if判断的一些特殊用法
5.33 case用法
5.34 for循环
5.35 while循环
一、if判断的一些特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
二、case用法
格式
case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3)
command
;;
案例:
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac
三、for循环
重复执行一系列命令在 编程中很常见。通常你需要重复一组命令直到达到某个特定条件,比如处理某个目录下的所有文件、系统上的所有用户或者是某个文本文件中的所有行。
常见的两种循环,在脚本中普遍被用到。
for循环
while循环
for循环演示:
累加求和
[root@ying01 shell]# vim sum01.sh
#!/bin/bash
sum=0
for i in `seq 1 3`
do
sum=$[ $sum+$i ]
echo $i
done
echo SUM=$sum
[root@ying01 shell]# sh -x sum01.sh
+ sum=0
++ seq 1 3
+ for i in '`seq 1 3`'
+ sum=1
+ echo 1
1
+ for i in '`seq 1 3`'
+ sum=3
+ echo 2
2
+ for i in '`seq 1 3`'
+ sum=6
+ echo 3
3
+ echo SUM=6
SUM=6
遍历一个目录的目录或者文件
[root@ying01 shell]# vim for02.sh
#!/bin/bashcd /root/100 //进入到目录
for a in `ls /root/100` //遍历此目录
do
[ -d $a ] && ls $a
# 判断是否是目录,并列出其下文件和子目录
if [ -d $a ]
then
echo $a
ls $a
fi
done
[root@ying01 shell]# ls /root/100
10 3
[root@ying01 shell]# sh -x for02.sh
+ cd /root/100
++ ls /root/100
+ for a in '`ls /root/100`'
+ '[' -d 10 ']'
+ ls 10
# 4.txt
+ '[' -d 10 ']'
+ echo 10
10
+ ls 10
# 4.txt
+ for a in '`ls /root/100`'
+ '[' -d 3 ']'
+ ls 3
2.txt
+ '[' -d 3 ']'
+ echo 3
3
+ ls 3
2.txt
[root@ying01 shell]# sh for02.sh
# 4.txt
10
# 4.txt
2.txt
3
2.txt
特殊for循环示例:list循环时,会以空格或回车符作为分隔符了
语法:for 变量名 in 条件; do …; done
案例1
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
echo $i
done
echo $sum
文件列表循环
#!/bin/bash
cd /etc/
for a in `ls /etc/`
do
if [ -d $a ]
then
ls -d $a
fi
done
四、while循环
语法 while 条件; do … ; done
案例1:
每隔1分钟检查一下系统负载,当系统的负载大于10的时候,发一封邮件(监控脚本) 最小单元是任务计划 cron
[root@ying01 shell]# vim while01.sh
#!/bin/bash
while :
# 冒号 : 表示死循环的意思,或者1,或者 true都是死循环
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
if [ $load -gt 10 ]
then
/usr/local/sbin/mail.py txwd188@126.com"load high" "$load"
fi
sleep 30 #休眠30秒,因为检查系统负载,不需要一直去检查,过一会再看
done
[root@ying01 shell]# sh -x while01.sh
+ :
++ w
++ cut -d. -f1
++ awk -F 'load average: ' '{print $2}'
++ head -1
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30
代码名词释义
w :查看系统负载 ;
uptime 可以直接显示 w 系统负载的第一行,就可以省去 head -1
head -1 //取第一行
awk -F 'load average: ' '{print $2}' // 以'load average: '分隔,输出第二段
cut -d . -f1 // 以 . 分隔 取第一段
案例2:
在循环过程过,需要用户输入一个数字;输入的不是数字,是数字,输入为空;回应相应的结果
[root@ying01 shell]# vim while02.sh
#!/bin/bashwhile :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue#continue 重新回到循环
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break #break 退出循环
done
echo $n
[root@ying01 shell]# sh while02.sh
Please input a number: k
you just only input numbers.
Please input a number: !
you just only input numbers.
Please input a number: 5
5
补充:
语法 while 条件; do … ; done
案例1
#!/bin/bash
while :
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
if [ $load -gt 10 ]
then
top|mail -s "load is high: $load" asldkfls@11.com
fi
sleep 30
done
案例2
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n