[root@master shell]# cat 1.sh #!/bin/bash#testing the if statementif date;thenecho"it work"fi
[root@master shell]# sh 1.sh
Fri Jan 2615:54:35 CST 2018
it work
2.if中调用变量
[root@master shell]# cat 2.sh #!/bin/bash
testuser=student
if grep $testuser /etc/passwd;thenecho"The bash files for user $testuser are:"
ls -a /home/$testuserelseecho"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=11if [ $VAL1-gt5 ];thenecho"The test value $VAL1 is greate than 5"fiif [ $VAL1-eq$VAL2 ];thenecho"The values are equal"elseecho"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 ];thenecho"Welcome $testuser"elseecho"Welcome $USER"fi
[root@master shell]# sh 4.sh
Welcome root
4.注意转义,比较字符串
[root@master shell]# cat 5.sh #!/bin/bash#mis-using string comparisons
VAL1=baseball
VAL2=hockey
if [ $VAL1 \> $VAL2 ];thenecho"$VAL1 is greater than $VAL2"elseecho"$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" ];thenecho"The string '$VAL1' is not empty"elseecho"The string '$VAL1' is empty"fiif [ -n "$VAL2" ];thenecho"The string '$VAL2' is not empty"elseecho"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 leapif [ -d$HOME ];thenecho"your HOME directory exists"cd$HOME
ls -aelseecho"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 fileif [ -e$HOME ];thenecho"The object exists,is it a file?"if [ -f$HOME ];thenecho"Yes,it is a file!"elseecho"No,it is not a file!"fielseecho"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 fileif [ -e$HOME ];thenecho"The object exists,is it a file?"if [ -f$HOME ];thenecho"Yes,it is a file!"elseecho"No,it is not a file!"fielseecho"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=10if (($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 matchingif [[ $USER == s* ]];thenecho"Hello $USER"elseecho"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