shell-04-流程控制

举报
kaliarch 发表于 2021/12/07 13:54:02 2021/12/07
【摘要】 一、if语句 1.1 单分支if conditionthen command1 command2 ... commandN fieg:if [ `ps -ef |grep /usr/sbin/sshd|grep -v grep|wc -l` -eq 1 ];then echo "sshd server exist";fi 1.2 双分支if conditionth...

一、if语句

1.1 单分支

if condition
then
    command1 
    command2
    ...
    commandN 
fi

eg:

if [ `ps -ef |grep /usr/sbin/sshd|grep -v grep|wc -l` -eq 1 ];then echo "sshd server exist";fi

1.2 双分支

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

eg:

if [ `ps -ef |grep /usr/sbin/sshd|grep -v grep|wc -l` -eq 0 ];then echo "sshd server exist";else echo "sshd server not exist";fi

1.3 多分支

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

eg:

  #! /bin/bash

  cmd=`rpm -q centos-release|cut -d- -f3`

  if [ $cmd -eq 6 ];then
    echo "sysversion is $cmd"
  elif [ $cmd -eq 7 ];then
    echo "sysversion is $cmd"
  else
    echo "sysversion is `rpm -q centos-release`"
  fi

二、for循环

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

eg1:

for i in /*; 
do
    echo -e "   \c";
    find $i |wc -l|sort -nr;
done

eg2:

#!/bin/bash
for i in {1..3};
do
    echo $i
done

eg3:

#!/bin/bash
for i in "$@"; {	# $@是将位置参数作为单个来处理
echo $i
}

默认 for 循环的取值列表是以空白符分隔,也就是第一章讲系统变量里的$IFS:

#!/bin/bash
OLD_IFS=$IFS
IFS=":"
for i in $(head -1 /etc/passwd); do
echo $i
done
#!/bin/bash

for ip in 192.168.1.{1..254}; do

    if ping -c 1 $ip >/dev/null; then

        echo "$ip OK."

    else

        echo "$ip NO!"

    fi

done

读取文件,判断url可用性

#!/bin/bash
#function:check url
filename=urllist.txt
for url in $(cat $filename)
do
status=`curl -I --connect-timeout 5 $url -s|awk '/HTTP/{print $2}'`
if [ $status == "200" ];then
	echo "Url:$url is ok!   status is $status"
else 
	echo "Url:$url is error! status is $status"
fi
done

三、while语句

格式:

while 条件表达式:do
    command
done

eg1:

#!/bin/bash
N=0
while [ $N -lt 5 ]; do
let N++
echo $N
done

条件表达式为 true,将会产生死循环,利用此可以将脚本一直放在后台进行执行
eg2:

#!/bin/bash
IP=10.75.128.8
dir="/DATA/oracle/netdir/"
if [ ! -d ${dir} ];then
	mkdir -p ${dir}
fi
echo 1 > ${dir}ping.lock
while true
do
	Time=`date +%F`
	TIME="${Time} 23:59"
	if [ "${data}" == "${TIME}" ];then
		mkdir ${dir}${Time} && mv ${dir}ping2.log ${dir}${Time}-ping2.log
		mv ${dir}${Time}-ping2.log ${dir}${Time}
	fi
	find ${dir} -mtime +7 -name "*-ping2.log" -exec rm -rf {} \;
	find ${dir} -mtime +7 -type d -exec rm -rf {} \;

	data=`date +%F' '%H:%M`
	data1=`date +%F' '%H:%M:%S`
	echo "------------${data1}---------------">>${dir}ping2.log
	ping -c 10 ${IP} >>${dir}ping2.log
	if [ $? -eq 1 ];then
		STAT=`cat ${dir}ping.lock`
		if [ ${STAT} -eq 1 ];then
			/usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1
50) ping 金融云(10.75.128.8)中断,请检查深信服VPN! \n TIME:${data1}"			echo 0 > ${dir}ping.lock
		else
			continue
		fi
	else
		STAT=`cat ${dir}ping.lock`
		if [ ${STAT} -eq 0 ];then
			/usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1
50) ping 金融云(10.75.128.8)恢复! \n TIME:${data1}"			echo 1 > ${dir}ping.lock
		else
			continue
		fi
	fi


done

文件处理

eg3:

#!/bin/bash
#function:check url
filename=urllist.txt
cat $filename | while read url;do
status=`curl -I $url -s|awk '/HTTP/{print $2}'`
if [ $status == "200" ];then
        echo "Url:$url is ok!status is $status"
else
        echo "Url:$url is error!status is $status"
fi
done

#!/bin/bash
#function:check url
filename=urllist.txt
while read url;
do
status=`curl -I $url -s|awk '/HTTP/{print $2}'`
if [ $status == "200" ];then
        echo "Url:$url is ok!status is ${status}"
else
        echo "Url:$url is error!status is ${status}"
fi
done <$filename

四、break 和 continue 语句

break跳出循环

#!/bin/bash

N=0
while true; do
    let N++
    if [ $N -eq 5 ]; then
    break
    fi
    echo $N
done

continue

#!/bin/bash
N=0
while [ $N -lt 5 ]; do
    let N++
    if [ $N -eq 3 ]; then
        continue
    fi
    echo $N
done

五、case语句

语句

case 模式名	in
    模式 1)
        命令
        ;;
    模式 2)
        命令
        ;;
    *)
        不符合以上模式执行的命令
esac

eg

#!/bin/bash
case $1 in
    start)
        echo "start."   
        ;;
    stop)
        echo "stop."
        ;;
    restart)
        echo "restart."
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
esac
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。