shell学习-13-if

用途说明

Shell中的条件判断语句,与其他编程语言类似。

如果需要知道有哪些条件判断方式,通过man test就可以得到帮助。

常用格式

格式一

if 条件; then

    语句

fi

格式二

if 条件; then

    语句

else

    语句

fi

格式三

if 条件; then

    语句

elif 条件; then

    语句

fi

格式四

if 条件; then

    语句

elif 条件; then

    语句

else

    语句

fi

使用示例

示例一

Bash代码   收藏代码
  1. if [ "foo" = "foo" ]; then  
  2.     echo expression evaluated as true  
  3. fi  

 

[root@jfht ~]# if [ "foo" = "foo" ]; then 
>     echo expression evaluated as true 
fi 
expression evaluated as true
[root@jfht ~]#

示例二

Bash代码   收藏代码
  1. if [ "foo" = "foo" ]; then  
  2.     echo expression evaluated as true  
  3. else  
  4.     echo expression evaluated as false  
  5. fi  

 

[root@jfht ~]# if [ "foo" = "foo" ]; then 
>     echo expression evaluated as true 
else 
>     echo expression evaluated as false 
fi 
expression evaluated as true
[root@jfht ~]#

示例三

Bash代码   收藏代码
  1. T1="foo"  
  2. T2="bar"  
  3. if [ "$T1" = "$T2" ]; then  
  4.     echo expression evaluated as true  
  5. else  
  6.     echo expression evaluated as false  
  7. fi  

 

[root@jfht ~]# T1="foo" 
[root@jfht ~]# T2="bar" 
[root@jfht ~]# if [ "$T1" = "$T2" ]; then 
>     echo expression evaluated as true 
else 
>     echo expression evaluated as false 
fi 
expression evaluated as false
[root@jfht ~]#

示例四 判断命令行参数数量

文件 if_4.sh

Bash代码   收藏代码
  1. #!/bin/sh  
  2.   
  3. if [ "$#" != "1" ]; then  
  4.     echo "usage: $0 <file>"  
  5.     exit 1  
  6. fi  

 

[root@smsgw root]# cat if_4.sh 
#!/bin/sh

if [ "$#" != "1" ]; then
    echo "usage: $0 <file>"
    exit 1
fi

[root@smsgw root]# chmod +x if_4.sh 
[root@smsgw root]# ./if_4.sh 
usage: ./if_4.sh <file>
[root@smsgw root]# ./if_4.sh hello 
[root@smsgw root]#

 

示例五 判断文件中是否包含某个字符串

Bash代码   收藏代码
  1. if grep -q root /etc/passwd; then  
  2.     echo account root exists  
  3. else  
  4.     echo account root not exist  
  5. fi  

 

[root@jfht ~]# if grep -q root /etc/passwd; then 
>     echo account root exists 
else 
>     echo account root not exist 
fi 
account root exists
[root@jfht ~]#

 

示例六 判断文件是否存在

Bash代码   收藏代码
  1. if [ -e myfile ]; then  
  2.     echo myfile exists  
  3. else  
  4.     touch myfile  
  5.     echo myfile created  
  6. fi  

 

[root@jfht ~]# if [ -e myfile ]; then 
>     echo myfile exists 
else 
>     touch myfile 
>     echo myfile created 
fi 
myfile created
[root@jfht ~]# if [ -e myfile ]; then 
>     echo myfile exists 
else 
>     touch myfile 
>     echo myfile created 
fi 
myfile exists
[root@jfht ~]# ls -l myfile 
-rw-r--r-- 1 root root 0 10-09 20:44 myfile

 

示例七 判断两个文件是否相同

Bash代码   收藏代码
  1. echo 1 >file1  
  2. echo 2 >file2  
  3. if ! diff -q file1 file2; then  
  4.     echo file1 file2 diff  
  5. else  
  6.     echo file1 file2 same  
  7. fi   

 

[root@jfht ~]# echo 1 >file1 
[root@jfht ~]# echo 2 >file2 
[root@jfht ~]# if ! diff -q file1 file2; then 
>     echo file1 file2 diff 
else 
>     echo file1 file2 same 
fi 
Files file1 and file2 differ
file1 file2 diff
[root@jfht ~]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值