|
shell中的流程控制语句
1、if语句
(1)单分支if条n件语句:
1)语法:
if [ 条件判断式 ];then
动作
fi
或:
if [ 条件判断式 ]
then
动作
fi
2)案例:
例1:判断文件是否存在,不存在则创建
#!/bin/sh
path=/root/script/
file=if1.sh
#mkdir
if [ ! -d ${path} ];then
mkdir -p ${path}
echo "${path} dir is not exit,already created it"
fi
#touch file
if [ ! -f ${path}${file} ]
then
touch ${path}${file}
echo "${path}${file} is not exit,already created it"
exit
fi
ls -l ${path}${file}
例2:判断磁盘使用率最大的分区,大于80%则报警
#!/bin/sh
usate=`df -h | tr -s " " | sort -nr -t" " -k5 | head -1 | awk '{print $5}' | cut -d"%" -f1`
namedev=`df -h | tr -s " " | sort -nr -t" " -k5 | head -1 | awk '{print $1}'`
if [ $usate -ge 80 ];then
echo "warning! ${namedev} is full"
fi
例3:新建shell脚本时写入注释信息并进入到第六行
#!/bin/bash
NEWNAME=$1
if ! grep -q "#!" ${NEWNAME} ;then
cat>>${NEWNAME} /tmp/etcbak/etcinfo.txt
echo "Date> cd /tmp/etcbak
tar -zcf etc-${rate}.tar.gz /etc etcinfo.txt &>/dev/null
rm -rf /tmp/etcbak/etcifo.txt
else
mkdir /tmp/etcbak
echo "Date:${rate}" >> /tmp/etcbak/etcinfo.txt
echo "Date> cd /tmp/etcbak
tar -zcf etc-${rate}.tar.gz /etc etcinfo.txt &>/dev/null
rm -rf /tmp/etcbak/etcifo.txt
fi
例2:(方法1)判断httpd服务是否启动
#!/bin/sh
port=`nmap -sT 172.16.250.102 | grep tcp | grep http | awk '{print $2}'`
if [ "$port" == "open" ]
then
echo "$(date +%F-%T) httpd is ok" >> /tmp/autostart-acc.log
else
systemctl start httpd &> /dev.null
echo "$(date +%F-%T) restart httpd!!" >>/tmp/autostart-err.log
fi
(方法2)判断httpd服务是否启动
#!/bin/bash
while true; do
COUNT=`ps aux | grep "httpd" | grep -v "grep" | wc -l`
#echo ${COUNT}
if [ ${COUNT} -eq 0 ];then
systemctl start httpd
echo "------`date "+%F %T"` server down" >> httpd.log
fi
sleep 60
done
(2)双分支if条件语句:
1)语法:
if [ 条件判断式1 ]
then
条件1成立时,执行该动作
elif [ 条件判断式2 ]
then
条件2成立时,执行该动作
elif [ 条件判断式3 ]
then
条件3成立时,执行该动作
……
else
当所有条件都不成立时,执行该动作
fi
2)案例
例1:判断两个整数的大小
#!/bin/sh
read -p "please input two number:" a b
#no1
[ -z $a ] || [ -z $b ] && {
echo "please input two number agcogin"
exit 1
}
#no 2
expr $a + 0 &>/dev/null
RETVAL1=$?
expr $b + 0 &>/dev/null
RETVAL2=$?
test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 || {
echo -e "please input two \033[31m number \033[0m again!!"
exit 2
}
#no3
if [ $a -lt $b ]
then
echo "$a < $b "
elif [ $a -eq $b ]
then
echo "$a = $b"
else
echo "$a > $b "
fi
例2:输入一个文件判断是什么文件
#!/bin/sh
read -p "please input one filename:" file
if [ -z $file ]
then
echo "error,please input a filename"
exit 1
elif [ -f "$file" ]
then
echo "$file is a ragulare file"
elif [ -d "$file" ]
then
echo "$file is a directory"
elif [ -L "$file" ]
then
echo "$file is a linkfile"
else
echo "sorry i don't know"
fi
2、for循环
(1)语法
1)语法一:
for 变量 in 值1 值2 值3
do
程序
done
例:批量解压
#!/bin/sh
cd /tmp/baktar
ls *.tar.gz > ls.log
for i in $(cat ls.log)
do
tar -zxf $i &>> /dev/null
done
rm -rf /tmp/baktar/ls.log
2)语法二:
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
例:从1加到100
#!/bin/sh
s=0
for (( i=1;i /dev/null
done
else
echo "user number error"
fi
fi
例2:计算0到100的和并输出计算过程:
#!/bin/sh
sum=0
for(( i=1;i /etc/hosts.deny
fi
fi
done < hacker.log
done
例2:DOS***自动防护脚本(执行时需要放入后台如:sh whidos.sh &)
#!/bin/sh
while true
do
awk '{print $1}' /var/log/httpd/access_log | grep -v "^$" | sort | uniq -c >/tmp/tmp.log
exec >/tmp/drop/list.log
fi
done
sleep 200
done
例3:编写一个猜数字的游戏:要猜的数字为1 到100 的随机整数,用户执行程序后根据提示输入一个数字,若输入的数字等于要猜的数字,提示成功,程序结束。若输入的数字不等于要猜的数字,提示大于或者小于要猜的数字,然后提示用户继续输入答案,直到用户猜出正确答案数字,程序终止
#!/bin/sh
AGE=`echo $[RANDOM%101]`
#echo $AGE
while true
do
read -p "please age,'q' or 'exit' to exit:" age
[ "$age" == "q" -o "$age" == "exit" ] && exit 0
#no1
[ -z "$age" ] && echo "please a number" && continue
#no2
expr $age + 0 &>/dev/null
ret=$?
[ $ret -ne 0 ] && echo "please input a integer" && continue
#no3
if [ $age -eq $AGE ]
then
echo "guess right.........."
exit 0
elif [ $age -lt $AGE ]
then
echo "cai xiao le"
else
echo "cai da le"
fi
done
|
|
|
|
|
|
|