Cirry's Blog

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

2016-08-09
linux
5分钟
869字
1
# if[条件判断式];then
2
# 程序
3
# fi
4
# 或者
5
# if[条件判断式]
6
# then
7
# 程序
8
# fi

注意:1.if用fi结尾

2.[]==test

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

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

例:判断分区使用率

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

双分支条件语句

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

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

1
#!/bin/bash
2
read -t 30 -p "please input a dir:" dir
3
4
if[-d "$dir"]
5
then
6
echo "It's a dir"
7
else
8
echo "No"
9
fi

例:判断Apache服务是否启动

www.netcraft.com

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

多分支语句

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
read -t 30 -p "Please input num1:" num1
4
read -t 30 -p "Please input num1:" num2
5
6
read -t 30 -p "Please input a operate:" ope
7
8
if[-n "$num1" - a -n "$num2" -a -n "$ope"]
9
then
10
test1=$(echo $num1 | sed 's/[0-9]//g')
11
test2=$(echo $num2 | sed 's/[0-9]//g')
12
if[-z "test1" -a -z "$test2"]
13
then
14
if["$ope"='+']
15
then
22 collapsed lines
16
sum=$(($num1+num2))
17
elif["$ope"='-']
18
then
19
sum=$(($num1-num2))
20
elif["$ope"='*']
21
then
22
sum=$(($num1*num2))
23
elif["$ope"='/']
24
then
25
sum=$(($num1/num2))
26
else
27
echo "Please input a vaild symbol"
28
exit 10
29
fi
30
else
31
echo "Please input a vaild value"
32
exit 11
33
fi
34
echo "请输入内容:"
35
exit12
36
37
echo "$num1 $ope $num2 = $sum"

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

1
#!/bin/bash
2
3
read -p "Please input a filename:" file
4
5
if[-z "$file"]
6
then
7
echo "Error, please input a filename"
8
exit 1
9
10
elif[!-e "$file"]
11
then
12
echo "Your input is not a file !"
13
exit2
14
elif[-f "$file"]
15
then
11 collapsed lines
16
echo "Your input is not a file !"
17
exit 2
18
elif[-f "$file"]
19
then
20
echo "$file is a regulare file !"
21
elif[-d "$file"]
22
then
23
echo "$file is a directory!"
24
else
25
echo "$file is an other file!"
26
fi

case语句:

多分支条件语句

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

for循环

1
# 语法1:
2
# for 变量 in 值1 值2 值3
3
# do
4
# 程序
5
# done

例:批量解压缩脚本

1
#!/bin/bash
2
3
cd /root/test
4
ls *.tar.gz > ls.log
5
for i in $(cat ls.log)
6
do
7
tar -zxf $i &> /dev/null
8
done
9
rm -rf /lamp//ls.log
10
11
语法2:
12
for ((初始值;循环控制条件;变量变化))
13
do
14
程序
15
done

例:

1
#!/bin/bash
2
#从1加到100
3
s=0
4
for ((i=1;i<=100;i=i+1))
5
do
6
s=$(($s+$i))
7
done
8
echo "The sum of 1+2+3...+100 is :$s"
1
#!/bin/bash
2
#批量添加指定数量的用户
3
4
read -p "Please input user name:" -t 30 name
5
read -p "Please input number of user:" -t 30 num
6
read -p "Please input passwd of user:" -t 30 passwd
7
8
if[!-z "$name" -a ! -z "$num" -a ! -z "$passwd"]
9
then
10
y=$(echo $num | sec 's/[0-9]//g')
11
if[-z "$y"]
12
then
13
for((i=1;i<=$num;i=i+1))
14
do
15
/usr/sbin/useradd $name$i &>echo $passwd | /usr/bin/passwd --
3 collapsed lines
16
done
17
fi
18
fi
1
#!/bin/bash
2
#删除用户
3
4
for i in $(cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1)
5
do
6
useradd -r $i
7
done
8
9
10
while 循环和 until 循环
11
while 循环:不定循环,条件循环
12
while (条件判断式)
13
do
14
程序
15
30 collapsed lines
16
done
17
18
19
/dev/null
20
stdin $name &i > /dev/null
21
22
#!/bin/bash
23
#从1加到100
24
25
i=1
26
s=0
27
while($i -le 100)
28
do
29
s=$(($s+$i))
30
i=$(($i+1))
31
done
32
echo "The sum is : $s"
33
34
35
36
until 循环:与while 相反
37
#!/bin/bash
38
i=1
39
s=0
40
until [$i -gt 100]
41
do
42
s=$(($s+$i))
43
i=$(($i+1))
44
done
45
echo $s
本文标题:shell编程——单双多分支条件语句(16)
文章作者:Cirry
发布时间:2016-08-09
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode
总访问量
总访客数人次