1. if语句

(1) 单分支if条件语句

格式为:

# 注意条件判断式两端的空格
if [ 条件判断式 ];then
程序员
fi 或者 if[ 条件判断式 ]
then
程序
fi

例:判断分区使用率

#!/bin/bash

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

  (2) 双分支if条件语句

  格式为:

if [ 条件判断式 ]
then
条件成立时,执行的程序
else
条件不成立时,执行的另一个程序
fi

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

#!/bin/bash
#备份mysql数据库
#Author : #同步系统时间
#ntpdate asia.pool.ntp.org &> /dev/null
#把当前系统时间按照"年月日"格式赋予变量date
date=$(date +%y%m%d)
#统计mysql数据库的大小,并将其赋予size变量
size=$(du -sh /etc) if [ -d /tmp/dbbak ]
then
echo "Date : $date!" > /tmp/dbbak/dbinfo.txt
echo "Date size : $size" >> /tmp/dbbak/dbinfo.txt
cd /tmp/dbbak
tar -zcf etc_$date.tar.gz /etc dbinfo.txt &> /dev/null
rm -rf /tmp/dbbak/dbinfo.txt
else
mkdir /tmp/dbbak
echo "Date : $date!" > /tmp/dbbak/dbinfo.txt
echo "Date size : $size" >> /tmp/dbbak/dbinfo.txt
cd /tmp/dbbak
tar -zcf etc_$date.tar.gz /etc dbinfo.txt &> /dev/null
rm -rf /tmp/dbbak/dbinfo.txt
fi

例2:判断apache是否启动

#!/bin/bash
#Author : #使用nmap命令扫描服务器(-sT表示扫描指定服务器上开启的TCP端口),并截取apache服务的状态,赋予变量port
port=$(nmap -sT 192.168.1.155 | grep tcp | grep http | awk '{print $2}') if [ "$port"=="open" ]
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

(3) 多分支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 ] #判断file变量是否为空
then
echo "Error,please input a filename"
exit
elif [ ! -e "$file" ] #判断file的值是否存在
then
echo "Your input is not a file!"
exit
elif [ -f "$file" ] #判断file的值是否为普通文件
then
echo "$file is a regulare file!"
elif [ -d "$file" ]
then
echo "$file is a directory!"
else
echo "$file is an other file!"
fi

输出结果:

[root@localhost sh]# ./if4.sh
Please input a filename:
Your input is not a file!
[root@localhost sh]# echo $? [root@localhost sh]# ./if4.sh
Please input a filename: /root
/root is a directory!

2. case语句

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

case的格式为:

case $变量名 in
"值1")
如果变量的值等于值1,则执行程序1
;;
"值2")
如果变量的值等于值2,则执行程序2
;;
...省略其他分支...
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac

例:判断用户输入

[root@localhost sh]# vi case1.sh

#!/bin/bash

read -p "Please choose yes/no: " -t  cho

case $cho in
"yes")
echo "Your choose is yes!"
;;
"no")
echo "Your choose is no!"
;;
*)
echo "Your choose is error!"
;;
esac

输出结果:

[root@localhost sh]# chmod 755 case1.sh 
[root@localhost sh]# ./case1.sh
Please choose yes/no: yes
Your choose is yes!
[root@localhost sh]# ./case1.sh
Please choose yes/no: no
Your choose is no!
[root@localhost sh]# ./case1.sh
Please choose yes/no: ls
Your choose is error!

3. for循环

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

格式1:
for 变量 in 值1 值2 值3
do
程序
done

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

[root@localhost sh]# vi for1.sh
#!/bin/bash for time in morning afternoon evening
do
echo "This time is $time"
done [root@localhost sh]# chmod for1.sh
[root@localhost sh]# ./for1.sh
This time is morning
This time is afternoon
This time is evening

例2:批量解压缩

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

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

[root@localhost sh]# vi for3.sh
#!/bin/bash s=
for(( i=;i<=;i=i+ ))
do
s=$(( $s + $i ))
done echo "The sum of 1+2+..+100 is : $s" [root@localhost sh]# chmod for3.sh
[root@localhost sh]# ./for3.sh
The sum of ++..+ is :

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

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

[root@localhost sh]# vim for4.sh
#!/bin/bash
#Author: read -t -p "Please input username:" name
read -t -p "Please input the number of users:" num
read -t -p "Please input the passed of users:" 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=;i<=$num;i=i+ ))
do
/usr/sbin/useradd $name$i &> /dev/null
echo pass | /usr/bin/passwd --stdin $name$i &> /dev/null
done
fi
fi

执行结果:

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

4. while循环与until循环

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

while [ 条件表达式 ]
do
程序
done

例1:从1加到100

[root@localhost sh]# vim while1.sh
#!/bin/bash
#Author: s=
i=
while [ $i -le ]
do
s=$(( $s + $i ))
i=$(( $i + ))
done echo "The sum is : $s"

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

until [ 条件判断式 ]
do
程序
done

例:从1加到100

[root@localhost sh]# vim while1.sh
#!/bin/bash
#Author: s=
i=
until [ $i -gt ]
do
s=$(( $s + $i ))
i=$(( $i + ))
done 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. 转:C++编程隐蔽错误:error C2533: 构造函数不能有返回类型

    C++编程隐蔽错误:error C2533: 构造函数不能有返回类型 今天在编写类的时候,出现的错误. 提示一个类的构造函数不能够有返回类型.在cpp文件里,该构造函数定义处并没有返回类型.在头文件里 ...

  2. ccf559c

    题意:给出一个矩阵棋盘,大小不超过10^5.上面有n个非法点(n<=2000)不可以踩,从左上角开始走到右下角,每次只能向下或者向右移动.问有多少种走法.结果对10^9+7取模. 分析: 组合数 ...

  3. iOS CLLocationManager 定位

    今天写个定位,本来很简单,但是在填写plist时,通过系统提示,只能看到NSLocationUsageDescription项目,根本不提示 (1)NSLocationAlwaysUsageDescr ...

  4. 开发简单的Kafka应用

    之前基于集群和单机安装过kafka,现在利用kafka提供的API构建一个简单的生产者消费者的项目示例,来跑通kafka的流程,具体过程如下: 首先使用eclipse for javaee建立一个ma ...

  5. Python~第三方模块

    第三方库还有MySQL的驱动:MySQL-python,用于科学计算的NumPy库:numpy,用于生成文本的模板工具Jinja2 模块搜索路径 Windows下: 双\\   sys.path.ap ...

  6. WebLogic部署

    1.抓取解压WAR包,放在相应目录下 2.登录部署,激活 http://jingyan.baidu.com/article/c74d6000650d470f6b595d72.html Linux环境中 ...

  7. 关于内核调试(Windbg)的虚拟机配置问题

    注:本文配置 环境为VMware® Workstation11.1.2 build-2780323+Windows xp SP2+Windbg 6.12.0002.63 x86 *在win7以后的操作 ...

  8. Navicat连接oracle,出现Only compatible with oci version 8.1 and&nb (转)

    与本地oracle连接的时候,一般没问题,sqlplus和oci都是本地oracle自带的,(设置: 工具->选项->oci) 分别为:   oci:D:\app\pcman\produc ...

  9. eclipse修改jdk后版本冲突问题

    将安装的jdk1.8改为1.7之后出现了很淡疼的问题 修改工程下.setting/ org.eclipse.jdt.core.prefs eclipse.preferences.version=1or ...

  10. 【leetcode】Reverse Words in a String(hard)☆

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...