Cirry's Blog

shell编程——单双多分支条件语句(16)

Aug 9, 2016
linux
5分钟
869字
# if[条件判断式];then
# 程序
# fi
# 或者
# if[条件判断式]
# then
# 程序
# fi

注意:1.if用fi结尾

2.[]==test

例:判断登录的用户是否是root

#!/bin/bash
test=$(env | grep "USER" | cut -d "=" -f 2)
if("$test"=="root")
then
echo "root"
fi

例:判断分区使用率

#!/bin/bash
test=$(df -h | grep sda5 | awk '{printf $5}') | cut -d "%" -f 1)
if["$test" -ge "90"]
then
echo "full"
fi

双分支条件语句

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

例:判断输入的是不是目录

#!/bin/bash
read -t 30 -p "please input a dir:" dir
if[-d "$dir"]
then
echo "It's a dir"
else
echo "No"
fi

例:判断Apache服务是否启动

www.netcraft.com

#脚本名不能写成httpd ,不然grep 无论Apache启动都能扫描到 httpd
ps aux | grep httpd | grep -v grep
#!/bin/bash
test=$(ps aux | grep httpd | grep -v grep)
if[-n "$test"]
then
echo "httpd is ok"
else
echo "httpd is no"
/etc/rc.d/init.d/httpd start
fi

多分支语句

# if[条件判断式1]
# then
# 当条件判断1成立时,执行程序1
# elif[条件判断式2]
# then
# 当条件判断式2成立时,执行程序2
# .....
# else
# 当所有条件都不成立时,最后执行此程序
# fi

例:做一个计算器

#!/bin/bash
read -t 30 -p "Please input num1:" num1
read -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"]
then
test1=$(echo $num1 | sed 's/[0-9]//g')
test2=$(echo $num2 | sed 's/[0-9]//g')
if[-z "test1" -a -z "$test2"]
then
if["$ope"='+']
then
22 collapsed lines
sum=$(($num1+num2))
elif["$ope"='-']
then
sum=$(($num1-num2))
elif["$ope"='*']
then
sum=$(($num1*num2))
elif["$ope"='/']
then
sum=$(($num1/num2))
else
echo "Please input a vaild symbol"
exit 10
fi
else
echo "Please input a vaild value"
exit 11
fi
echo "请输入内容:"
exit12
echo "$num1 $ope $num2 = $sum"

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

#!/bin/bash
read -p "Please input a filename:" file
if[-z "$file"]
then
echo "Error, please input a filename"
exit 1
elif[!-e "$file"]
then
echo "Your input is not a file !"
exit2
elif[-f "$file"]
then
11 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!"
fi

case语句:

多分支条件语句

# case $变量名 in
# "值1")
# 执行程序1
# ;;
# "值2")
# 执行程序2
# ;;
# ......
# *)
# 如果变量的值都不是一上的值,执行此程序
# ;;
# esac
#!/bin/bash
read -p "Please choose yes/no :" -t 30 cho
case $cho in
"yes")
echo "your choose is yes!"
;;
"no")
echo "your choose is no!"
;;
*)
echo "your choose is error!"
;;
esac

for循环

# 语法1:
# for 变量 in 值1 值2 值3
# do
# 程序
# 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
语法2:
for ((初始值;循环控制条件;变量变化))
do
程序
done

例:

#!/bin/bash
#从1加到100
s=0
for ((i=1;i<=100;i=i+1))
do
s=$(($s+$i))
done
echo "The sum of 1+2+3...+100 is :$s"
#!/bin/bash
#批量添加指定数量的用户
read -p "Please input user name:" -t 30 name
read -p "Please input number of user:" -t 30 num
read -p "Please input passwd of user:" -t 30 passwd
if[!-z "$name" -a ! -z "$num" -a ! -z "$passwd"]
then
y=$(echo $num | sec 's/[0-9]//g')
if[-z "$y"]
then
for((i=1;i<=$num;i=i+1))
do
/usr/sbin/useradd $name$i &>echo $passwd | /usr/bin/passwd --
3 collapsed lines
done
fi
fi
#!/bin/bash
#删除用户
for i in $(cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1)
do
useradd -r $i
done
while 循环和 until 循环
while 循环:不定循环,条件循环
while (条件判断式)
do
程序
30 collapsed lines
done
/dev/null
stdin $name &i > /dev/null
#!/bin/bash
#从1加到100
i=1
s=0
while($i -le 100)
do
s=$(($s+$i))
i=$(($i+1))
done
echo "The sum is : $s"
until 循环:与while 相反
#!/bin/bash
i=1
s=0
until [$i -gt 100]
do
s=$(($s+$i))
i=$(($i+1))
done
echo $s
本文标题:shell编程——单双多分支条件语句(16)
文章作者:Cirry
发布时间:Aug 9, 2016
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode
总访问量
总访客数人次