1. if语句

(1) 单分支if条件语句

格式为:

  1. # 注意条件判断式两端的空格
    if [ 条件判断式 ];then
  2. 程序员
  3. fi
  4.  
  5. 或者
  6.  
  7. if[ 条件判断式 ]
  8. then
  9. 程序
  10. fi

例:判断分区使用率

  1. #!/bin/bash
  2.  
  3. #获取根分区的使用率
  4. rate=$(df -h | grep "/dev/sda5" | awk '{print $5}' | cut -d "%" -f )
  5.  
  6. if [ $rate -ge ]
  7. then
  8. echo "/dev/sda5 is over 10%!!"
  9. fi

  (2) 双分支if条件语句

  格式为:

  1. if [ 条件判断式 ]
  2. then
  3. 条件成立时,执行的程序
  4. else
  5. 条件不成立时,执行的另一个程序
  6. fi

例1:备份/etc目录(同样适用于实际情况中的mysql备份)

  1. #!/bin/bash
  2. #备份mysql数据库
  3. #Author :
  4.  
  5. #同步系统时间
  6. #ntpdate asia.pool.ntp.org &> /dev/null
  7. #把当前系统时间按照"年月日"格式赋予变量date
  8. date=$(date +%y%m%d)
  9. #统计mysql数据库的大小,并将其赋予size变量
  10. size=$(du -sh /etc)
  11.  
  12. if [ -d /tmp/dbbak ]
  13. then
  14. echo "Date : $date!" > /tmp/dbbak/dbinfo.txt
  15. echo "Date size : $size" >> /tmp/dbbak/dbinfo.txt
  16. cd /tmp/dbbak
  17. tar -zcf etc_$date.tar.gz /etc dbinfo.txt &> /dev/null
  18. rm -rf /tmp/dbbak/dbinfo.txt
  19. else
  20. mkdir /tmp/dbbak
  21. echo "Date : $date!" > /tmp/dbbak/dbinfo.txt
  22. echo "Date size : $size" >> /tmp/dbbak/dbinfo.txt
  23. cd /tmp/dbbak
  24. tar -zcf etc_$date.tar.gz /etc dbinfo.txt &> /dev/null
  25. rm -rf /tmp/dbbak/dbinfo.txt
  26. fi

例2:判断apache是否启动

  1. #!/bin/bash
  2. #Author :
  3.  
  4. #使用nmap命令扫描服务器(-sT表示扫描指定服务器上开启的TCP端口),并截取apache服务的状态,赋予变量port
  5. port=$(nmap -sT 192.168.1.155 | grep tcp | grep http | awk '{print $2}')
  6.  
  7. if [ "$port"=="open" ]
  8. then
  9. echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
  10. else
  11. /etc/rc.d/init.d/httpd start &> /dev/null
  12. echo "$(date) restart httpd!!" >> /tmp/autostart-err.log
  13. fi

(3) 多分支if条件语句

格式为:

  1. if [ 条件判断式1 ]
  2. then
  3. 当条件判断式1成立,执行程序1
  4. elif [ 条件判断式2 ]
  5. then
  6. 当条件判断式2成立时,执行程序2
  7. ...省略更多条件...
  8. else
  9. 当所有条件都不成立时,最后执行此程序
  10. fi

例:判断用户输入的是什么文件

  1. #!/bin/bash
  2.  
  3. #接收键盘收入,并赋予变量file
  4. read -p "Please input a filename: " file
  5.  
  6. if [ -z file ] #判断file变量是否为空
  7. then
  8. echo "Error,please input a filename"
  9. exit
  10. elif [ ! -e "$file" ] #判断file的值是否存在
  11. then
  12. echo "Your input is not a file!"
  13. exit
  14. elif [ -f "$file" ] #判断file的值是否为普通文件
  15. then
  16. echo "$file is a regulare file!"
  17. elif [ -d "$file" ]
  18. then
  19. echo "$file is a directory!"
  20. else
  21. echo "$file is an other file!"
  22. fi

输出结果:

  1. [root@localhost sh]# ./if4.sh
  2. Please input a filename:
  3. Your input is not a file!
  4. [root@localhost sh]# echo $?
  5.  
  6. [root@localhost sh]# ./if4.sh
  7. Please input a filename: /root
  8. /root is a directory!

2. case语句

case语句和if...elif...fi语句一样都是多分支条件语句,不过case语句只能判断一种条件关系,而if语句可以判断多种条件关系。

case的格式为:

  1. case $变量名 in
  2. "值1")
  3. 如果变量的值等于值1,则执行程序1
  4. ;;
  5. "值2")
  6. 如果变量的值等于值2,则执行程序2
  7. ;;
  8. ...省略其他分支...
  9. *)
  10. 如果变量的值都不是以上的值,则执行此程序
  11. ;;
  12. esac

例:判断用户输入

  1. [root@localhost sh]# vi case1.sh
  2.  
  3. #!/bin/bash
  4.  
  5. read -p "Please choose yes/no: " -t cho
  6.  
  7. case $cho in
  8. "yes")
  9. echo "Your choose is yes!"
  10. ;;
  11. "no")
  12. echo "Your choose is no!"
  13. ;;
  14. *)
  15. echo "Your choose is error!"
  16. ;;
  17. esac

输出结果:

  1. [root@localhost sh]# chmod 755 case1.sh 
    [root@localhost sh]# ./case1.sh
  2. Please choose yes/no: yes
  3. Your choose is yes!
  4. [root@localhost sh]# ./case1.sh
  5. Please choose yes/no: no
  6. Your choose is no!
  7. [root@localhost sh]# ./case1.sh
  8. Please choose yes/no: ls
  9. Your choose is error!

3. for循环

Linux中的for循环有多种语法格式:

  1. 格式1
  2. for 变量 in 1 2 3
  3. do
  4. 程序
  5. done

例1:打印时间及输出结果

  1. [root@localhost sh]# vi for1.sh
  2. #!/bin/bash
  3.  
  4. for time in morning afternoon evening
  5. do
  6. echo "This time is $time"
  7. done
  8.  
  9. [root@localhost sh]# chmod for1.sh
  10. [root@localhost sh]# ./for1.sh
  11. This time is morning
  12. This time is afternoon
  13. This time is evening

例2:批量解压缩

  1. [root@localhost sh]# vi for2.sh
  2. #!/bin/bash
  3.  
  4. cd /tmp
  5. ls *.tar.gz > ls.log
  6. for i in $(cat ls.log)
  7. do
  8. tar -zxf $i &> /dev/null
  9. done
  10.  
  11. rm -rf /tmp/ls.lo
  1. 格式2
  2. for (( 初始值;循环控制;变量变化 ))
  3. do
  4. 程序
  5. done

例1:从1加到100,并显示输出结果

  1. [root@localhost sh]# vi for3.sh
  2. #!/bin/bash
  3.  
  4. s=
  5. for(( i=;i<=;i=i+ ))
  6. do
  7. s=$(( $s + $i ))
  8. done
  9.  
  10. echo "The sum of 1+2+..+100 is : $s"
  11.  
  12. [root@localhost sh]# chmod for3.sh
  13. [root@localhost sh]# ./for3.sh
  14. The sum of ++..+ is :

注:两种for的区别在于是否事先知道处理的个数,如果知道,执行第二种for语法,否则执行第一种语法。

例2:批量添加指定数量的用户

  1. [root@localhost sh]# vim for4.sh
  2. #!/bin/bash
  3. #Author:
  4.  
  5. read -t -p "Please input username:" name
  6. read -t -p "Please input the number of users:" num
  7. read -t -p "Please input the passed of users:" pass
  8. if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
  9. then
  10. y=$(echo $num | sed 's/^[0-9]*$//g')
  11. if [ -z "$y" ]
  12. then
  13. for(( i=;i<=$num;i=i+ ))
  14. do
  15. /usr/sbin/useradd $name$i &> /dev/null
  16. echo pass | /usr/bin/passwd --stdin $name$i &> /dev/null
  17. done
  18. fi
  19. fi

执行结果:

  1. [root@localhost sh]# cat /etc/passwd
  2. ......
  3. user1:x::::/home/user1:/bin/bash
  4. user2:x::::/home/user2:/bin/bash

4. while循环与until循环

(1) while循环是不定循环,也称作条件循环。只要条件判断式成立,循环就会一直继续,直到条件判断式不成立,循环才会停止。这就和for的固定循环不太一样,其格式为:

  1. while [ 条件表达式 ]
  2. do
  3. 程序
  4. done

例1:从1加到100

  1. [root@localhost sh]# vim while1.sh
  2. #!/bin/bash
  3. #Author:
  4.  
  5. s=
  6. i=
  7. while [ $i -le ]
  8. do
  9. s=$(( $s + $i ))
  10. i=$(( $i + ))
  11. done
  12.  
  13. echo "The sum is : $s"

(2) until循环和while循环相反。until循环只要条件判断式不成立则进行循环,并执行循环程序。一旦条件判断式成立,则循环终止,其格式为:

  1. until [ 条件判断式 ]
  2. do
  3. 程序
  4. done

例:从1加到100

  1. [root@localhost sh]# vim while1.sh
  2. #!/bin/bash
  3. #Author:
  4.  
  5. s=
  6. i=
  7. until [ $i -gt ]
  8. do
  9. s=$(( $s + $i ))
  10. i=$(( $i + ))
  11. done
  12.  
  13. echo "The sum is : $s"

Linux学习笔记(18) Shell编程之流程控制的更多相关文章

  1. Linux学习笔记(17) Shell编程之基础

    1. 正则表达式 (1) 正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表达式:通配符用来匹配符合条件的文件名,通配符是完全匹配.ls.find ...

  2. Linux学习笔记:Shell脚本学习

    概念 真正能够控制计算机硬件(CPU.内存.显示器等)的只有操作系统内核(Kernel),图形界面和命令行只是架设在用户和内核之间的一座桥梁. 由于安全.复杂.繁琐等原因,用户不能直接接触内核(也没有 ...

  3. Linux学习笔记 -- 初识 Shell

    Shell 是什么 Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务.Shell既是 ...

  4. linux学习(七)Shell编程中的变量

    目录 shell编程的建立 shell的hello world! Shell的环境变量 使用和设置环境变量 Shell的系统变量 用户自定义变量 @(Shell编程) shell编程的建立 [root ...

  5. golang学习笔记(二):流程控制

    欢迎访问我的博客和github! 今天咱们把烦人的事情丢一丢,继续来学习go的基础知识. 这篇文章记录go语言的流程控制和更多类型. 流程控制 for Go 只有一种循环结构:for 循环. 基本的 ...

  6. #Linux学习笔记# 自定义shell终端提示符

    我使用的Linux发行版是LinuxMint 17.2 Rafaela,默认情况下Terminal中的shell提示包括了用户名.主机名.当前目录(绝对路径)和提示符.这样会导致当进入一个比较深的目录 ...

  7. linux学习笔记之shell

    本文参考:shell脚本学习指南 本文阅读前提为:知道shell指令,但不知道如何完成一个自动化的shell脚本. 因为编辑本文时,作者也是一个新手.所以,在一些理论上,可能存在错误.如果存在错误,希 ...

  8. Linux学习笔记<五>——<Shell部分>

    管道命令(pipe) 1.把一个命令的输出作为另一个命令的输入 ls -al /etc | less 2.选取命令:cut和grep cut命令可以将一段消息的某段切出来. -d接分隔符,-f是取出第 ...

  9. Linux学习笔记 -- 为 Shell 传递参数

    我们可以在执行 Shell 脚本时,可以向脚本传递参数.脚本内获取参数的格式为:$n.(n 代表一个数字,0为所执行的shell脚本名称,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类 ...

随机推荐

  1. nginx做本地目录映射

    有时候需要访问服务器上的一些静态资源,比如挂载其他设备上的图片到本地的目录,而本地的目录不在nginx根目录下,这个时候就需要简单的做一下目录映射来解决,比如想通过浏览器http://ip/image ...

  2. ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

    //转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路 //Time:16Ms Memory:208K #include<iostream> #include<cstring ...

  3. Java Thread.join()方法

    一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...

  4. java获取本月或某月的第一天和最后一天

    获取某月的第一天和最后一天的日期 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Ca ...

  5. web开发,关于jsp的常见问题,重复提交,防止后退。

    看了网上的,有几种方法:1 在你的表单页里HEAD区加入这段代码: <META HTTP-EQUIV="pragma" CONTENT="no-cache" ...

  6. Linux面试题汇总答案

    转自:小女生的Linux技术~~~Linux面试题汇总答案~~ 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的 ...

  7. 生成Geometry

    // 由一组点集生成一张三角面片网格Geometry osg::Geometry* createTRIANGLESGeometry(MyMesh &mesh) { osg::ref_ptr&l ...

  8. MFC CheckBox

    if ( BST_CHECKED == IsDlgButtonChecked( IDC_CHECK1 ) ){// 勾选}else{}

  9. Warp divergence

    Threads are executed in warps of 32, with all threads in the warp executing the same instruction at ...

  10. mvn命令备忘

    转换成eclipse项目mvn eclipse:eclipse 跳过testmvn install -Dmaven.test.skip=true mvn clean install -DskipTes ...