shell if语句与for循环语句及while流程语句

发布时间:2020-02-04编辑:脚本学堂
有关shell if语句的例子,shell流程控制语句之for循环、while循环语句的例子,学习下shell条件判断与循环语句的用法。

一,shell流程语句(shell条件判断
1,条件语句if else if

例子:
 

[root@squid-2 script]# cat s1.sh
#!/bin/bash
echo "Please choose project:"
echo "1:zhu  2:sha"
read project_no
if [ $project_no = "1" ];then
echo "111111"
elif [ $project_no = "2" ];then
echo "222222"
else echo "error"
fi
[root@squid-2 script]#

执行过程:
 

[root@squid-2 script]# sh s1.sh
Please choose project:
1:zhu 2:sha
1
111111
[root@squid-2 script]# sh s1.sh
Please choose project:
1:zhu 2:sha
2
222222
[root@squid-2 script]# sh s1.sh
Please choose project:
1:zhu 2:sha
3
error
[root@squid-2 script]#

2,for 循环
2.1 for i in
脚本
 

[root@squid-2 script]# cat host_list.txt
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
[root@squid-2 script]#

结果:
[root@squid-2 script]# sh s21.sh
the host ip address is: 192.168.1.10
the host ip address is: 192.168.1.11
the host ip address is: 192.168.1.12
the host ip address is: 192.168.1.13
[root@squid-2 script]#

2.2 for((赋值;条件;运算语句))
脚本:
 

[root@squid-2 script]# cat s22.sh
for((i=1;i<=10;i++));do
echo "the loop number i: $i";
done;
[root@squid-2 script]#

执行结果:
 

[root@squid-2 script]# sh s22.sh
the loop number i: 1
the loop number i: 2
the loop number i: 3
the loop number i: 4
the loop number i: 5
the loop number i: 6
the loop number i: 7
the loop number i: 8
the loop number i: 9
the loop number i: 10
[root@squid-2 script]#

3,while循环使用
条件语句结构:
while
do
action
done;

测试脚本:
 

[root@squid-2 script]# cat s3.sh
#!/bin/sh
i=10;
while [[ $i -gt 5 ]];do
echo"the loop number of while case is: $i";
((i--));
done;
[root@squid-2 script]

执行结果:
 

[root@squid-2 script]# sh s3.sh
the loop number of while case is: 10
the loop number of while case is: 9
the loop number of while case is: 8
the loop number of while case is: 7
the loop number of while case is: 6
[root@squid-2 script]#

4,until循环语句
示例脚本:
 

[root@squid-2 script]# cat s4.sh
#!/bin/sh
a=1;
until [[ $a -gt 6 ]];do
echo"the until number is: $a";
((a++));
done;
[root@squid-2 script]#

执行结果:
 

[root@squid-2 script]# sh s4.sh
the until number is: 1
the until number is: 2
the until number is: 3
the until number is: 4
the until number is: 5
the until number is: 6
[root@squid-2 script]#

5,shell选择语句
5.1,使用case选择语句使用(case/esac)
语法结构:
 

case $arg in
pattern | sample) # arg in pattern or sample
;;
pattern1) # arg in pattern1
;;
*) #default
;;
esac

说明:pattern1 是正则表达式,可以用下面字符:

*       任意字串
?       任意字元
[abc]  a, b, 或c三字元其中之一
[a-n]   从a到n的任一字元
|       多重选择

代码脚本:
 

[root@squid-2 script]# cat s51.sh
#!/bin/sh

case $1 in
start | begin)
echo "start something"
;;
stop | end)
echo "stop something"
;;
*)
echo "Ignorant"
;;
esac
[root@squid-2 script]#

执行结果,这里需要带入参数,参数值就在)前面的start、begin、stop、end之内,如果带入别参数,则返回"Ignorant":
 

[root@squid-2 script]# sh s51.sh start
start something
[root@squid-2 script]# sh s51.sh begin
start something
[root@squid-2 script]# sh s51.sh end
stop something
[root@squid-2 script]# sh s51.sh stop
stop something
[root@squid-2 script]# sh s51.sh test1
Ignorant

5.2,select语句使用方法(产生菜单选择)
语法结构:
select 变量name  in seq变量
do
action
done

代码如下:
cat s52.sh
 

#!/bin/sh
select param in "begin""end" "exit"
do
case $param in
"begin")
echo "start something"
;;
"end")
echo "stop something"
;;
"exit")
echo "exit"
break;
;;
*)
echo "Ignorant"
;;
esac
done;

执行结果:
 

[root@squid-2 script]# sh s52.sh begin
1) begin
2) end
3) exit
#? 1
start something
#? 2
stop something
#? 3
exit
[root@squid-2 script]#

执行时,只有输入exit,才能退出来执行小窗口。
说明:select是循环选择,一般与case语句使用。

二,shell语句的比较操作符
1,整数比较
规则说明:
 

-eq      等于 if [ "$a" -eq "$b" ]
-ne      不等于                if [ "$a" -ne "$b" ]
-gt      大于 if [ "$a" -gt "$b" ]
-ge      大于等于              if [ "$a" -ge "$b" ]
-lt      小于   if [ "$a" -lt "$b" ]
-le      小于等于               if [ "$a" -le "$b" ]
<        小于(需要双括号)           (( "$a"< "$b" ))
<=       小于等于(...)                (("$a" <= "$b" ))
>        大于(...)   (("$a" > "$b" ))
>=       大于等于(...)                (("$a" >= "$b" ))
 

备注:小数据比较可使用linuxjishu/13830.html target=_blank class=infotextkey>awk

例子:
 

[root@squid-2 script]# cat com1.sh
a=$1
b=$2
if [ "$a" -eq "$b" ];then
echo"a = b true."
elif [ ! "$a" -gt "$b" ];then
echo"a > b true."
else echo "a < b true."
fi
[root@squid-2 script]#

测试结果:
 

[root@squid-2 script]# sh com1.sh 1 1
a = b true.
[root@squid-2 script]# sh com1.sh 1 2
a > b true.
[root@squid-2 script]# sh com1.sh 1 0
a < b true.
[root@squid-2 script]#

2,字符串比较

2.1,规范以及使用
规则说明:
 

=     等于           if [ "$a"= "$b" ]
==    与=等价
!=    不等于         if [ "$a" ="$b" ]
<     小于,在ASCII字母中的顺序:
if [[ "$a" < "$b" ]]
if [ "$a" < "$b" ]         #需要对<进行转义
>     大于
-z    字符串为null,即长度为0
-n    字符串不为null,即长度不为0

例子:
 

[root@squid-2 script]# cat com2.sh
a=$1
b=$2

# 1 the first method to implement
if [ "$a"x = "$b"x ];then
echo"a = b"
elif [ ! "$a"x  = "$b"x ]; then
echo"a != b"
else echo "others"
fi

# 2 the second method to implement
if [ "$a"x == "$b"x ];then
echo "a = b"
elif [ "$a"x  != "$b"x ]; then
echo "a != b"
else echo "others"
fi

测试结果:
 

[root@squid-2 script]# sh com2.sh ccb aaa
a != b
a != b
[root@squid-2 script]# sh com2.sh ccb ccb
a = b
a = b
[root@squid-2 script]#
[root@squid-2 script]#

注意要点:
比较两个字符串是否相等的办法是:
if [ "$a"x = "b"x ];then

关键有几点:
1、使用单个等号
2、注意到等号两边各有一个空格:这是unix shell的要求
3、注意到"$a"x最后的x,这是特意安排的,因为当$test为空的时候,上面的表达式就变成了x = bx,显然是不相等的。而如果没有这个x,表达式就会报错:[: =: unary operator expected

[[ $a == z* ]]   # 如果$a以"z"开头(模式匹配)那么将为true
[[ $a == "z*" ]] # 如果$a等于z*(字符匹配),那么结果为true
[ $a == z* ]     # File globbing 和word splitting将会发生
[ "$a" == "z*" ] # 如果$a等于z*(字符匹配),那么结果为true

关于File globbing:
一点解释,关于Fileglobbing是一种关于文件的速记法,比如"*.c"就是,再如~也是.
但是file globbing并不是严格的正则表达式,虽然绝大多数情况下结构比较像.

特殊字符的比较:
!= 不等于,如:if [ "$a" != "$b" ]
这个操作符将在[[]]结构中使用模式匹配.
< 小于,在ASCII字母顺序下.如:
if [[ "$a" < "$b" ]]
if [ "$a" < "$b" ]
注意:在[]结构中"<"需要被转义.
> 大于,在ASCII字母顺序下.如:
if [[ "$a" > "$b" ]]
if [ "$a" > "$b" ]
注意:在[]结构中">"需要被转义.

-z 字符串为"null".就是长度为0.
-n 字符串不为"null"

注意:
使用-n在[]结构中测试必须要用""把变量引起来.使用一个未被""的字符串来使用! -z
或者就是未用""引用的字符串本身,放到[]结构中。虽然一般情况下可 以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯。

2.3,判断字符串是否空
例子:
 

[root@squid-2 script]# cat com3.sh
#!/bin/bash
a=$1
if [ -z "$a" ]; then
echo"a is empty."
else echo "a is a object."
fi
[root@squid-2 script]

测试结果:
 

[root@squid-2 script]# sh com3.sh a
a is a object.
[root@squid-2 script]# sh com3.sh
a is empty.
[root@squid-2 script]#

3,文件判断
规则说明:
 

-e  文件存在
-a  文件存在(已被弃用)
-f  被测文件是一个regular文件(正常文件,非目录或设备)
-s  文件长度不为0
-d  被测对象是目录
-b  被测对象是块设备
-c  被测对象是字符设备
-p  被测对象是管道
-h  被测文件是符号连接
-L  被测文件是符号连接
-S(大写)  被测文件是一个socket
-t  关联到一个终端设备的文件描述符。用来检测脚本的stdin[-t0]或[-t1]是一个终端
-r  文件具有读权限,针对运行脚本的用户
-w  文件具有写权限,针对运行脚本的用户
-x  文件具有执行权限,针对运行脚本的用户
-u  set-user-id(suid)标志到文件,即普通用户可以使用的root权限文件,通过chmod +s file实现
-k  设置粘贴位
-O  运行脚本的用户是文件的所有者
-G  文件的group-id和运行脚本的用户相同
-N  从文件最后被阅读到现在,是否被修改
f1-nt f2  文件f1是否比f2新
f1 -ot f2  文件f1是否比f2旧
f1 -ef f2  文件f1和f2是否硬连接到同一个文件

例子:
 

[root@squid-2 script]# cat com4.sh
#!/bin/bash
a=$1
file=$2
if [ -d $a ]; then
echo"it is a directory."
elif [ -f "$a" ]; then
echo"it is a file."
else echo "no parameter."
fi

测试结果:
 

[root@squid-2 script]# sh com4.sh log1
it is a file.
[root@squid-2 script]# sh com4.sh old
it is a directory.