1# if[条件判断式];then2# 程序3# fi4# 或者5# if[条件判断式]6# then7# 程序8# fi
注意:1.if用fi结尾
2.[]==test
例:判断登录的用户是否是root
1#!/bin/bash2test=$(env | grep "USER" | cut -d "=" -f 2)3
4if("$test"=="root")5then6echo "root"7fi
例:判断分区使用率
1#!/bin/bash2test=$(df -h | grep sda5 | awk '{printf $5}') | cut -d "%" -f 1)3if["$test" -ge "90"]4then5echo "full"6fi
双分支条件语句
1# if[条件判断式]2# then3# 条件成立时,执行的程序4# else5# 条件不成立时,执行的另一个程序6# fi
例:判断输入的是不是目录
1#!/bin/bash2read -t 30 -p "please input a dir:" dir3
4if[-d "$dir"]5then6echo "It's a dir"7else8echo "No"9fi
例:判断Apache服务是否启动
1#脚本名不能写成httpd ,不然grep 无论Apache启动都能扫描到 httpd2ps aux | grep httpd | grep -v grep3
4#!/bin/bash5test=$(ps aux | grep httpd | grep -v grep)6if[-n "$test"]7then8echo "httpd is ok"9else10echo "httpd is no"11/etc/rc.d/init.d/httpd start12fi
多分支语句
1# if[条件判断式1]2# then3# 当条件判断1成立时,执行程序14# elif[条件判断式2]5# then6# 当条件判断式2成立时,执行程序27# .....8# else9# 当所有条件都不成立时,最后执行此程序10# fi
例:做一个计算器
1#!/bin/bash2
3read -t 30 -p "Please input num1:" num14read -t 30 -p "Please input num1:" num25
6read -t 30 -p "Please input a operate:" ope7
8if[-n "$num1" - a -n "$num2" -a -n "$ope"]9then10test1=$(echo $num1 | sed 's/[0-9]//g')11test2=$(echo $num2 | sed 's/[0-9]//g')12if[-z "test1" -a -z "$test2"]13then14if["$ope"='+']15then22 collapsed lines
16sum=$(($num1+num2))17elif["$ope"='-']18then19sum=$(($num1-num2))20elif["$ope"='*']21then22sum=$(($num1*num2))23elif["$ope"='/']24then25sum=$(($num1/num2))26else27echo "Please input a vaild symbol"28exit 1029fi30else31echo "Please input a vaild value"32exit 1133fi34echo "请输入内容:"35exit1236
37echo "$num1 $ope $num2 = $sum"
例:判断用户输入的是什么文件
1#!/bin/bash2
3read -p "Please input a filename:" file4
5if[-z "$file"]6then7echo "Error, please input a filename"8exit 19
10 elif[!-e "$file"]11 then12 echo "Your input is not a file !"13 exit214 elif[-f "$file"]15 then11 collapsed lines
16 echo "Your input is not a file !"17 exit 218 elif[-f "$file"]19 then20 echo "$file is a regulare file !"21 elif[-d "$file"]22 then23 echo "$file is a directory!"24 else25 echo "$file is an other file!"26fi
case语句:
多分支条件语句
1# case $变量名 in2# "值1")3# 执行程序14# ;;5# "值2")6# 执行程序27# ;;8# ......9# *)10# 如果变量的值都不是一上的值,执行此程序11# ;;12# esac
1#!/bin/bash2read -p "Please choose yes/no :" -t 30 cho3case $cho in4"yes")5echo "your choose is yes!"6;;7"no")8echo "your choose is no!"9;;10*)11echo "your choose is error!"12;;13esac
for循环
1# 语法1:2# for 变量 in 值1 值2 值33# do4# 程序5# done
例:批量解压缩脚本
1#!/bin/bash2
3cd /root/test4ls *.tar.gz > ls.log5for i in $(cat ls.log)6do7tar -zxf $i &> /dev/null8done9rm -rf /lamp//ls.log10
11语法2:12for ((初始值;循环控制条件;变量变化))13do14程序15done
例:
1#!/bin/bash2#从1加到1003s=04for ((i=1;i<=100;i=i+1))5do6s=$(($s+$i))7done8echo "The sum of 1+2+3...+100 is :$s"
1#!/bin/bash2#批量添加指定数量的用户3
4read -p "Please input user name:" -t 30 name5read -p "Please input number of user:" -t 30 num6read -p "Please input passwd of user:" -t 30 passwd7
8if[!-z "$name" -a ! -z "$num" -a ! -z "$passwd"]9then10y=$(echo $num | sec 's/[0-9]//g')11if[-z "$y"]12then13for((i=1;i<=$num;i=i+1))14do15/usr/sbin/useradd $name$i &>echo $passwd | /usr/bin/passwd --3 collapsed lines
16done17fi18fi
1#!/bin/bash2#删除用户3
4for i in $(cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1)5do6useradd -r $i7done8
9
10while 循环和 until 循环11while 循环:不定循环,条件循环12while (条件判断式)13do14程序15
30 collapsed lines
16 done17
18
19/dev/null20stdin $name &i > /dev/null21
22#!/bin/bash23#从1加到10024
25i=126s=027while($i -le 100)28do29s=$(($s+$i))30i=$(($i+1))31done32echo "The sum is : $s"33
34
35
36until 循环:与while 相反37#!/bin/bash38i=139s=040until [$i -gt 100]41do42s=$(($s+$i))43i=$(($i+1))44done45echo $s