您的位置:首页 > 运维架构 > Shell

shell脚本之if语句

2018-01-26 17:25 615 查看
1.
if
的简单判断

[root@master shell]# cat 1.sh
#!/bin/bash
#testing the if statement

if date;then
echo "it work"
fi
[root@master shell]# sh 1.sh
Fri Jan 26 15:54:35 CST 2018
it work


2.
if
中调用变量

[root@master shell]# cat 2.sh
#!/bin/bash
testuser=student
if grep $testuser /etc/passwd;then
echo "The bash files for user $testuser are:"
ls -a /home/$testuser
else
echo "The user name $testuser does not exits on this "
fi
[root@master shell]# sh 2.sh
student:x:1000:1000:student:/home/student:/bin/bash
The bash files for user student are:
.  ..  .bash_logout  .bash_profile  .bashrc  hx1  out.txt


3.多个
if
条件

[root@master shell]# cat 3.sh
#!/bin/bash
#using numerci test comparisons
VAL1=10
VAL2=11

if [ $VAL1 -gt 5 ];then
echo "The test value $VAL1 is greate than 5"
fi

if [ $VAL1 -eq $VAL2 ];then
echo "The values are equal"
else
echo "The values that $VAL1 and $VAL2 are different"
fi
[root@master shell]# sh 3.sh
The test value 10 is greate than 5
The values that 10 and 11 are different

[root@master shell]# cat 4.sh
#!/bin/bash
#testing string equality

testuser=student

if [ $USER = $testuser ];then
echo "Welcome $testuser"
else
echo "Welcome $USER"
fi
[root@master shell]# sh 4.sh
Welcome root


4.注意转义,比较字符串

[root@master shell]# cat 5.sh

bc95
#!/bin/bash
#mis-using string comparisons

VAL1=baseball
VAL2=hockey

if [ $VAL1 \> $VAL2 ];then
echo "$VAL1 is greater than $VAL2"
else
echo "$VAL1 is less than $VAL2"
fi
[root@master shell]# sh 5.sh
baseball is less than hockey


5.定义空,可以是等于号后面什么都没有,或者是两个单引号

[root@master shell]# cat 6.sh
#!/bin/bash
#testing string length

VAL1=testing
VAL2=''

if [ -n "$VAL1" ];then
echo "The string '$VAL1' is not empty"
else
echo "The string '$VAL1' is empty"
fi

if [ -n "$VAL2" ];then
echo "The string '$VAL2' is not empty"
else
echo "The string '$VAL2' is empty"
fi

[root@master shell]# sh 6.sh
The string 'testing' is not empty
The string '' is empty


6.
-d
是否是目录

[root@master shell]# cat 7.sh
#!/bin/bash
#look before you leap

if [ -d $HOME ];then
echo "your HOME directory exists"
cd $HOME
ls -a
else
echo "There is a problem with your HOME directory"
fi
[root@master shell]# sh 7.sh
your HOME directory exists
.        .bash_logout   .mysql_history          .ssh
..       .bash_profile  .pki                .tcshrc
admin-openrc     .bashrc    python              .viminfo
anaconda-ks.cfg  .cshrc     rhel-server-7.2-x86_64-dvd.iso
.bash_history    demo-openrc    .rnd


7.
-e
是否存在,
-f
是否为文件

[root@master shell]# cat 8.sh
#!/bin/bash
#check if a file
if [ -e $HOME ];then
echo "The object exists,is it a file?"
if [ -f $HOME ];then
echo "Yes,it is a file!"
else
echo "No,it is not a file!"
fi
else
echo "Sorry,the object does not exists!"
fi
[root@master shell]# sh 8.sh
The object exists,is it a file?
No,it is not a file!


8.
&&
同时满足

[root@master shell]# cat 8.sh
#!/bin/bash
#check if a file
if [ -e $HOME ];then
echo "The object exists,is it a file?"
if [ -f $HOME ];then
echo "Yes,it is a file!"
else
echo "No,it is not a file!"
fi
else
echo "Sorry,the object does not exists!"
fi
[root@master shell]# sh 9.sh
I cannot write to the file.


9.运算的写法
(())


[root@master shell]# cat 10.sh
#!/bin/bash
#using double parenthesis

VAL1=10

if (($VAL1 ** 2 > 90));then
((VAL2 = $VAL1 ** 2))
echo "The square of $VAL1 is $VAL2"
fi
[root@master shell]# sh 10.sh
The square of 10 is 100

[root@master shell]# cat 11.sh
#!/bin/bash
#using pattern matching

if [[ $USER == s* ]];then
echo "Hello $USER"
else
echo "Sorry,I do not know you."
fi
[root@master shell]# sh 11.sh
Sorry,I do not know you.
[root@master shell]# su student
[student@master shell]$ sh 11.sh
Hello student
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: