[linux@zeng bin]$ cat ifcondition.sh
#!/bin/bash
#
#This shell script. use for demo if ...then statement.
#
if [ -f ~/bin/math.sh ]#注意这里的空格是怎么留的。
then
echo "math.sh already exist in you home."
else
echo "math.sh no found."
fi
[linux@zeng bin]$ ifcondition.sh
math.sh already exist in you home.
你会发现上面的shell script. 写的很死,没什么扩展性,我们稍作修改成下面的样子。
[linux@zeng bin]$ cat ifcondition.sh
#!/bin/bash
#
#This shell script. use for demo if ...then statement.
#
if test -f $1
then
echo "$1 already exist in you home."
else
echo "$1 no found."
fi
经过修改现在的ifcondition.sh 强大多了。这里要提下脚本的风格
要注意做适当的缩进提高shell script. 的可读性。
[linux@zeng bin]$ ifcondition.sh ~/.bash_history
/home/linux/.bash_history already exist in you home.
[linux@zeng bin]$ ifcondition.sh xxx.sh
xxx.sh no found.
test 命令
test 命令经常和if 语句一起使用用来做true/false 的判断。test 命令有两种形式
第一种是:test expression,第二种是:[ expression ]
当test 命令的执行结果为true 的时候退出状态是0,否则是1.
常用的test 选项有:
用于文件测试或者目录测试的有
-d file 如果file 是一个文件夹。
-e file 如果file 存在为真。
-f file 如果file 存在并且是普通文件为真。
-L file 如果file 存在并且是符号连接为真。
-x file 如果file 对你是可读的为真。
-w file 如果file 对你是读写的为真。
-x file 如果file 对你是可执行的为真。
file1 -nt file2 如果file1 的修改时间比file2 新为真。
file1 -ot file2 如果file2 的修改时间比file2 老为真。
用作算数运算的有
-eq 等于,-ne 不等于,-lt 小于,le 小于或者等于,-gt 大于,-ge 大于等于。
用作逻辑操作的运算符有
! expression 逻辑not
expression1 -a expression2 逻辑and
expression1 -o expression2 逻辑or
用作字符串比较的有
string1 = string2 ,如果string1 等于string2 返回真。
string1 != string2,如果string1 不等于string2 返回真。
string1 ,表示string1 不为空,或者没有定义。
-n string1,表示string1 不为空并且已经存在。
-z string1,表示string1 是空的或者已经存在。
下面这个小 shell script. 用来测试执行脚本用户的身份,并且调用我们
上面写好的ifcondition.sh,身份的判断使用了test 命名中的字符串比较。
[linux@zeng bin]$ id -u
500
[linux@zeng bin]$ cat test_linux.sh
#!/bin/bash
#
#This shell script. for test linux.
#
if [ $(id -u) != "500" ]
then
echo "You must be the linux user to run this script"
exit 1
else
/home/linux/bin/ifcondition.sh $1
fi
使用root用户来执行该脚本试试。
[root@zeng ~]# /home/linux/bin/test_linux.sh ~./bash_history
You must be the linux user to run this script
这里的1 是我们设置的错误退出状态。
嵌套if 语句
如果你觉得if 语句还不够强大,那么可以使用嵌套if 语句来实现更强大的功能。
下面使用嵌套if 语句来实现多分支。
[linux@zeng bin]$ cat best.sh
#!/bin/bash
#
#This shell script. is a demo for nested if statement.
#
os=0
echo "Which operating system you think is best?"
echo "1.Uinx operating system like Solaris,AIX..."
echo "2.Linux operating system like Red Hat linux,Suse linux..."
echo "3.Other,like Windows,MAC os..."
echo -n "Please select [1,2 or 3]?"
read os
if [ $os -eq 1 ]
then
echo "You pick up Unix."
else
if [ $os -eq 2 ]
then
echo "You pick up linux."
else
if [ $os -eq 3 ]
then
echo "You pick up other."
else
echo "Why you don't make a choice?"
fi
fi
fi
接下来我们测试这个脚本。
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?1
You pick up Unix.
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?2
You pick up linux.
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?3
You pick up other.
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?5
Why you don't make a choice?
多级if 语句
bash shell 为我们提供了灵活多样的if 语句,来帮我们实现我们想要的功能。
多级if 语句的语法格式是:
if condition
then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
then
condition1 is zero (true - 0)
execute all commands up to elif statement
elif condition2
then
condition2 is zero (true - 0)
execute all commands up to elif statement
else
None of the above condtion,condtion1,condtion2 are true (i.e.
all of the above nonzero or false)
execute all commands up to fi
fi
下面这个shell script. 用来演示 多级if 语句。
[linux@zeng bin]$ cat multilevelif.sh
#!/bin/bash
#
#This shell script. test if...elif...else statement.
#
read -p "Enter a number:" n
if [ $n -gt 0 ]
then
echo "$n is a positive."
elif [ $n -lt 0 ]
then
echo "$n is a negative."
elif [ $n -eq 0 ]
then
echo "$n is zero number."
else
echo "Oops! $n is not a number."
fi
[linux@zeng bin]$ multilevelif.sh
Enter a number:10
10 is a positive.