shell基础(二)
7、if判断一些特殊用法
if [ -z $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 ,[]中不能使用<、> 、>=、<=这样的符号。
补充:
[ -z STRING ] “STRING” 的长度为零则为真
8.shell中的case判断
格式:case 变量名 in
value1)
command
;;
value2)
command
;;
... ...
*) //代表其他所有情况
command
;;
esac
在case程序中,可以在条件中使用|,表示或的意思,比如
2|3)
command
;;
当变量为2或者3时,执行该部分命令
#!/bin/bash
#case判断
read -p "Please input a number: " n
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "Please input a number."
exit 1
fi
n2=$[$n%2]
case $n2 in
0)
echo "偶数"
;;
1)
echo "奇数"
;;
*)
echo "Please input a number."
;;
esac
#!/bin/bash
#case判断2
read -p "请输入你的成绩: " n
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "请输入你的分数!"
exit 1
elif [ $n -lt 0 ] || [ $n -gt 100 ]
then
echo "分数正确范围是0-100!"
exit 1
fi
if [ $n -lt 60 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
else
tag=4
fi
case $tag in
1)
echo "不及格"
;;
2)
echo "及格"
;;
3)
echo "良好"
;;
4)
echo "优秀"
;;
*)
echo "系统错误!"
;;
esac
9、循环
for循环,语法结构:for 变量名 in 条件 ;do .... ;done
while 循环语法结构:while 条件;do ...;done
死循环用:表示,例如
while :;do echo ok;sleep 3;done 每三秒显示一个ok;
break直接结束本层循环;
continue忽略continue之下的代码,直接进行下一次循环;
exit直接退出shell
for i in `seq 1 10 `;do echo $i ;done
seq 10 -1 1 是倒序10到1,-1是步长
#!/bin/bash
#for循环
for i in `seq 1 100`
do
sum=$[$sum+$i]
done
echo $sum
#!/bin/bash
#for循环2
for f in `ls /etc/`
do
if [ -d /etc/$f ]
then
echo "/etc/$f"
fi
done
#!/bin/bash
#for循环3,默认空格、tab、空白都会被当成分割符,会导致一行被分成几行
for i in `cat /etc/passwd`
do
echo $i
done
n=`wc -l /etc/passwd|awk '{print $1}'`
for i in `seq 1 $n`
do
sed -n "$i"p 1.txt
done
#!/bin/bash
#while监控脚本
while :
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
echo $load
if [ $load -gt 10 ]
then
top|mail -s "load is hign: $load" admin@abc.com
fi
sleep 30
done
#!/bin/bash
#while循环2
tag=1
while [ $tag == 1 ]
do
read -p "请输入你的成绩: " n
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "请输入你的分数!"
tag=1
elif [ $n -lt 0 ] || [ $n -gt 100 ]
then
echo "分数正确范围是0-100!"
tag=1
else
tag=0
fi
done
if [ $n -lt 60 ]
then
echo "成绩不合格!"
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
echo "成绩合格!"
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
echo "成绩良好!"
else
echo "成绩优秀!"
fi
#!/bin/bash
#continue案例
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
continue
fi
echo $i
done
echo "continue!"
#!/bin/bash
#break案例
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
break
fi
echo $i
done
echo "break!"
#!/bin/bash
#exit案例
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
echo "exit!"
10、shell中的函数
函数就是把一段代码整理到了一个小单元中,并给这个小单元七个名字,当用到这段代码时直接调用这个小单元的名字即可。
格式:function f_name(){
command
}
函数必须放在最前面,函数里可以export全局变量
#!/bin/bash
#函数案例
sum() {
s=$[$1+$2]
echo $s
}
sum 1 2
#!/bin/bash
#显示IP
ip(){
ifconfig|grep -A1 "$1" |tail -1|awk '{print $2}'|awk -F ':' '{print $2}'
}
read -p "Please input the eth name: " e
i=`ip $e`
echo "$e address is $i"
11、select用法
select也是循环的一种,适合用在用户选择的情况下
比如,我们有这样的一个需求,运行脚本后,用用户去选择数字,选择1,会运行w命令,选择2运行top命令,选择3运行free命令,选择4退出:
#!/bin/bash
echo "请输入一个数字,1:运行w命令,2:运行top命令,3:运行free命令,4:退出脚本"
echo
select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "请输入1-4的数字!"
;;
esac
done