• shell 编程


    shell 编程

    判断

    • 判断23是否大于22
    [root@localhost ~]# [ 23 -ge 22 ] && echo "yes" || echo "no"
    yes
    
    
    • 判断23 是否小于22
    [root@localhost ~]# [ 23 -le 22 ] && echo "yes"  || echo "no"
    no
    
    
    • 多重条件判断

    判断1 -a 判断2 : 逻辑与
    判断1 -o 判断2 : 逻辑或
    !判断 : 逻辑非

    流程控制

    • 单分支if条件语句
    if [ 条件判断式 ];then
    	程序
    fi
    
    
    if [ 条件判断式 ]
    	then
    	程序
    fi
    

    判断登陆用户是否为root用户

    #!/bin/bash
    test=$(env |grep "USER" | cut -d "=" -f2) 
    if [ "$test"==root ]
    	then
    	echo "Current user is root."
    fi
    

    判断分区使用率

    #!/bin/bash
    test=$(df -h | grep sda5 | awk '{print $5}') | cut -d "%" -f 1)
    #如果使用超过90,则发送信息提醒管理员做出相应措施
    if [ "$test" -ge "90" ]
    	then
    		echo "/ is full"
    fi
    
    • 双分支if条件语句
      [格式]:
    if [ 条件判断式 ]
    	then
    		条件成立时,执行的程序
    	else
    		条件不成立时,执行的另一个程序
    fi
    
    

    判断是否为一个目录

    #!/bin/bash
    read -t 30 -p "please input a dir: " dir
    if [ -d "$dir"]
    	then
    		echo "input is a directary."
    	else
    		echo "input isn't a directary." 
    fi
    
    

    判断apache是否启动

    #!/bin/bash
    #截取apached进程,并把结果赋予变量test
    test=$(ps aux | grep httpd | grep -v grep)
    #测试test是否为空,如果不为空,则执行then中命令
    if [ -n "$test" ]
    	then
    		echo "$(date) httpd is ok! " >> /tmp/autostart-acc.log
    	else 
    		/etc/rc.d/init.d/httpd start &>/dev/null
    		echo "$(date) restart httpd !! " >> /tmp/autostart-err.log
    fi
    
    
    • 多分支if条件语句
    if [ 条件判断式1 ]
    	then
    		当条件判断式1成立时,执行程序1
    elif  [ 条件判断式2 ]
    	then
    		当条件判断式2成立时,执行程序2
    ... 省略更多的条件....	
    else 
    	当所有条件不成立时,最后执行此程序
    fi
    
    

    判断用户输入是什么文件

    #!/bin/bash
    #接收键盘的输入,并赋予变量file
    read -p "Please input a filename: " file
    #判断变量是否为空
    if [ -z "$file" ]
    	then
    		echo "Erroe,please input a filename!!"
    		exit 1 
    #判断file值是否为空
    elif [ ! -e "file" ]
    	then 
    		echo "you input is not a file! "
    		exit 2
    #判断file是否为一个普通文件
    elif [ -f "$file" ]
    	then 
    		echo "file is a regulare file ! "
    #判断file是否为一个目录
    elif [ -d "$file" ]
    	then 
    		ehco "file is a dirctory !"
    else 
    	echo "file is another file ! "
    fi
    		
    
    • 多分支case条件语句
    case $变量名 in
    "值1")
    	如果变量的值等于值1,则执行程序1
    	::
    "值2")
    	如果变量的值等于2,则执行程序2
    	;;
    ...省略其他分支...
    *)
    	如果变量的值都不是以上的值,则执行此程序
    ;;
    esac
    
    
    • For循环语句
    for i in 1 2 3 4 5 
    	do 
    		echo $i
    	done
    	
    

    批量解压缩脚本

    #!/bin/bash
    cd /root/test
    ls *.tar.gz > ls.log
    for i in $(cat ls.log)
    	do 
    		tar -zxf $i &> /dev/null
    	done
    rm -rf /lamp/ls.log
    
    
    

    批量添加指定数量的用户

    read -p "please input user name: " -t 30 name
    read -p "please input the number of users: " -t 30 num
    read -p "please input the password of users: " -t 30 pass
    if [ ! -z "$name" -a ! -z "$num" -a ! -z  "$pass" ] 
    	then 
    	y=$(echo $num | sed 's/[0-9]//g')
    	if [ -z "$y" ]
    		then
    		for (( i=1;i<=$num;i=i+1))
    			do
    				/usr/sbin/useradd $name$i
    				echo $pass |  /usr/bin/passwd --stdin $name$i &>/dev/null
    			done
    	fi
    fi
    
    
  • 相关阅读:
    mysql生成日历表
    入园第一篇
    写于2010年元旦
    C#中abstract与virtual的用法
    大学时的基础知识,回顾一下
    《大话设计模式》读书笔记建造者模式
    转:你真的了解分层架构吗?——写给被PetShop"毒害"的朋友们
    《大话设计模式》读书笔记观察者模式
    客户端与服务器端交互原理[转]
    js取得gridview中获取checkbox选中的值
  • 原文地址:https://www.cnblogs.com/Ethan2lee/p/7532050.html
Copyright © 2020-2023  润新知