• Shell流程控制及循环


    1 单分支if判断

    # 表达式两端必须有空格
    # 格式1
    if第一种书写方式
    	if [ 表达式 ];then
    		执行的命令
    	fi
    
    # 格式2
    if第一种书写方式
    	if [ 表达式 ]
    	then
    		执行的命令
    	fi
    
    [root@shell /server/scripts]# cat test_if.sh
    #!/bin/bash
    if [ -f /etc/passwd ];then
        echo "密码文件存在!"
    fi
    
    if [ -d /etc ]
    then
        echo "etc目录存在!"
    fi
    

    2 双分支if判断

    # 格式
    if [ 表达式 ];then
        执行的命令
    else
    	执行的命令
    fi
    
    if [ -f /etc/hosts ];then
    	echo "文件存在!"
    	else
    	  echo "文件不存在!"
    	fi
    

    3 多分支if判断

    if [ 如果你有钱 ];then
    	    我就嫁给你
    elif [ 如果你有房子 ];then
    	    我就嫁给你
    elif [ 你有车 ];then
    	    先谈朋友
    elif [ 如果你月入五万 ];then
    	    我们先谈朋友
    else	# 以上都不成立
    	   拜拜了
    fi
    
    # 判断两个数的大小
    [root@shell /server/scripts]# cat if.sh 
    #!/bin/bash
    if [ $# -ne 2 ];then
    	echo "请输入2个整数!"
    	exit 4
    else
    	if [[ "$1" =~ ^[0-9]+$ && "$2" =~ ^[0-9]+$ ]];then
    		if [ $1 -eq $2 ];then
    			echo "$1 = $2"
    		elif [ $1 -lt $2 ];then
    			echo "$1 < $2"
    		else
    			echo "$1 > $2"
    		fi
    	else
    		echo "请输入2个整数啊,沙雕!"
    		exit 8
    	fi
    fi
    

    4 while循环

    # 格式
    while true
    do
        执行的命令
    done
    
    # 随机数大小判断
    [root@shell /server/scripts]# cat ran.sh 
    #!/bin/sh
    ran=`echo $((RANDOM%100+1))`
    while true
    do
    let i++
    read -p "请输入你要猜的数字[1-100]: " num
    if [ $num -gt $ran ];then
    	 echo "你输入的数字 $num 大了"
    elif [ $num -lt $ran ];then
    	 echo "你输入的数字 $num 小了"
    else
    	 echo "恭喜你答对了随机数为$ran"
    	 echo "总共猜了$i 次"
    	 exit
    fi
    done
    

    5 for循环

    # 格式
    for i in `seq 10` #数字|字符串|`命令`
    do
        执行命令
    done
    
    # 批量创建用户名
    [root@shell /server/scripts]# cat add_user.sh 
    #!/bin/sh
    read -p "请输入用户的前缀名称: " prefix
    read -p "请输入要创建多少个:"   num
    for i in `seq $num`
    do
    	 echo ${prefix}_${i}
    done
    

    6 case判断

    # 格式
    case 变量 可以是直接传参 赋值 read读入
    case 变量  in
    		   模式1)
    			    命令序列
    				;;
    			模式2)
    			    命令序列
    				;;
    			模式3)
    			    命令序列
    				;;
    			 *)
    			    无匹配后命令序列
        esac
    
    # 根据屏幕输出安装对应软件
    [root@shell /server/scripts]# cat case.sh 
    #!/bin/sh
    cat<<EOF
    				1.NFS
    				2.MySQL
    				3.Redis
    				4.DOCKER
    				5.KVM
    				6.退出脚本
    EOF
    while true
    do
    read -p "请输入你想要安装的服务编号或者服务名称: " num
    case $num in
    		1)
    			echo "yum -y instll NFS........"
    			;;
    		2)
    			echo "yum -y install MySQL......"
    			;;
    		3)
    			echo "yum -y install Redis......."
    			;;
    		6)
    			exit
    			;;
    		   *)
    			echo "USAGE: $0 read -p [NFS|MySQL|Redis|DOCKER|KVM]"
    esac
    done
    
    # 自定义nginx启动脚本
    [root@shell /server/scripts]# cat nginx.sh 
    #!/bin/bash
    [ -f /etc/init.d/functions ] && . /etc/init.d/functions
    
    ACTION=$1
    NG_STATE=$(netstat -lntup|grep nginx|wc -l)
    LISTEN_PORT=$(netstat -lntup|grep nginx|awk '{print $4}'|awk -F: '{print $2}'|xargs)
    
    #TEST STATUS
    TEST(){
    	if [ $? -eq 0 ];then
    		action "${ACTION} nginx" /bin/true
    	else
    		action "${ACTION} nginx" /bin/false
    	fi
    }
    
    #START NGINX
    START(){
    	if [ ${NG_STATE} -ne 0 ];then
    		echo "Nginx is Running,You need to do nothing."
    		exit
    	else
    		/usr/sbin/nginx -t &>/dev/null
    		if [ $? -eq 0 ];then
    			/usr/sbin/nginx
    			[ $? -eq 0 ] && TEST
    		else
    			/usr/sbin/nginx -t
    		fi
    	fi
    }
    
    STOP(){
    	/usr/sbin/nginx -s stop
    	[ $? -eq 0 ] && TEST
    }
    
    RELOAD(){
    	if [ ${NG_STATE} -ne 0 ];then
    		/usr/sbin/nginx -s reload
    		[ $? -eq 0 ] && TEST
    	else
    		echo "No Process is running."
    	fi
    }
    
    RESTART(){
    	if [ ${NG_STATE} -ne 0 ];then
    		/usr/sbin/nginx -s stop
    		[ $? -eq 0 ] && sleep 1
    		/usr/sbin/nginx
    		[ $? -eq 0 ] && TEST
    	else
    		echo "No Process is running."
    	fi
    }
    
    STATUS(){
    	if [ ${NG_STATE} -ne 0 ];then
    		echo "Nginx is Running and Listening Port are ${LISTEN_PORT}."	
    	else
    		echo "Nginx is dead."
    	fi
    }
    
    case ${ACTION} in 
    	start)
    		START
    		;;
    	restart)
    		RESTART
    		;;
    	reload)
    		RELOAD
    		;;
    	stop)
    		STOP
    		;;
    	status)
    		STATUS
    		;;
    	*)
    		echo "Usage $0 {start|stop|restart|reload|status}"
    esac
    

    参考博客:https://www.pingface.com/archives/ifcase

  • 相关阅读:
    flask 知识积累
    python中下划线
    pipenv知识积累
    shell知识积累
    AttributeError: type object 'testClass' has no attribute 'testMothod'
    python知识积累
    补全爬取的url
    linux 的基本命令
    Python里的拷贝
    关于第一次java课的感想
  • 原文地址:https://www.cnblogs.com/centlnx/p/12710017.html
Copyright © 2020-2023  润新知