# if[条件判断式];then# 程序# fi# 或者# if[条件判断式]# then# 程序# fi注意:1.if用fi结尾
2.[]==test
例:判断登录的用户是否是root
#!/bin/bashtest=$(env | grep "USER" | cut -d "=" -f 2)
if("$test"=="root")thenecho "root"fi例:判断分区使用率
#!/bin/bashtest=$(df -h | grep sda5 | awk '{printf $5}') | cut -d "%" -f 1)if["$test" -ge "90"]thenecho "full"fi双分支条件语句
# if[条件判断式]# then# 条件成立时,执行的程序# else# 条件不成立时,执行的另一个程序# fi例:判断输入的是不是目录
#!/bin/bashread -t 30 -p "please input a dir:" dir
if[-d "$dir"]thenecho "It's a dir"elseecho "No"fi例:判断Apache服务是否启动
#脚本名不能写成httpd ,不然grep 无论Apache启动都能扫描到 httpdps aux | grep httpd | grep -v grep
#!/bin/bashtest=$(ps aux | grep httpd | grep -v grep)if[-n "$test"]thenecho "httpd is ok"elseecho "httpd is no"/etc/rc.d/init.d/httpd startfi多分支语句
# if[条件判断式1]# then# 当条件判断1成立时,执行程序1# elif[条件判断式2]# then# 当条件判断式2成立时,执行程序2# .....# else# 当所有条件都不成立时,最后执行此程序# fi例:做一个计算器
#!/bin/bash
read -t 30 -p "Please input num1:" num1read -t 30 -p "Please input num1:" num2
read -t 30 -p "Please input a operate:" ope
if[-n "$num1" - a -n "$num2" -a -n "$ope"]thentest1=$(echo $num1 | sed 's/[0-9]//g')test2=$(echo $num2 | sed 's/[0-9]//g')if[-z "test1" -a -z "$test2"]thenif["$ope"='+']then22 collapsed lines
sum=$(($num1+num2))elif["$ope"='-']thensum=$(($num1-num2))elif["$ope"='*']thensum=$(($num1*num2))elif["$ope"='/']thensum=$(($num1/num2))elseecho "Please input a vaild symbol"exit 10fielseecho "Please input a vaild value"exit 11fiecho "请输入内容:"exit12
echo "$num1 $ope $num2 = $sum"例:判断用户输入的是什么文件
#!/bin/bash
read -p "Please input a filename:" file
if[-z "$file"]thenecho "Error, please input a filename"exit 1
elif[!-e "$file"] then echo "Your input is not a file !" exit2 elif[-f "$file"] then11 collapsed lines
echo "Your input is not a file !" exit 2 elif[-f "$file"] then echo "$file is a regulare file !" elif[-d "$file"] then echo "$file is a directory!" else echo "$file is an other file!"ficase语句:
多分支条件语句
# case $变量名 in# "值1")# 执行程序1# ;;# "值2")# 执行程序2# ;;# ......# *)# 如果变量的值都不是一上的值,执行此程序# ;;# esac#!/bin/bashread -p "Please choose yes/no :" -t 30 chocase $cho in"yes")echo "your choose is yes!";;"no")echo "your choose is no!";;*)echo "your choose is error!";;esacfor循环
# 语法1:# for 变量 in 值1 值2 值3# do# 程序# done例:批量解压缩脚本
#!/bin/bash
cd /root/testls *.tar.gz > ls.logfor i in $(cat ls.log)dotar -zxf $i &> /dev/nulldonerm -rf /lamp//ls.log
语法2:for ((初始值;循环控制条件;变量变化))do程序done例:
#!/bin/bash#从1加到100s=0for ((i=1;i<=100;i=i+1))dos=$(($s+$i))doneecho "The sum of 1+2+3...+100 is :$s"#!/bin/bash#批量添加指定数量的用户
read -p "Please input user name:" -t 30 nameread -p "Please input number of user:" -t 30 numread -p "Please input passwd of user:" -t 30 passwd
if[!-z "$name" -a ! -z "$num" -a ! -z "$passwd"]theny=$(echo $num | sec 's/[0-9]//g')if[-z "$y"]thenfor((i=1;i<=$num;i=i+1))do/usr/sbin/useradd $name$i &>echo $passwd | /usr/bin/passwd --3 collapsed lines
donefifi#!/bin/bash#删除用户
for i in $(cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1)douseradd -r $idone
while 循环和 until 循环while 循环:不定循环,条件循环while (条件判断式)do程序
30 collapsed lines
done
/dev/nullstdin $name &i > /dev/null
#!/bin/bash#从1加到100
i=1s=0while($i -le 100)dos=$(($s+$i))i=$(($i+1))doneecho "The sum is : $s"
until 循环:与while 相反#!/bin/bashi=1s=0until [$i -gt 100]dos=$(($s+$i))i=$(($i+1))doneecho $s