散仙本篇主要讲在shell里面的流程控制,流程控制是所有的编程语言里面必不可少的一部分,通过流程控制,可以使我们的程序更加灵活。
下面我们来看看如何在shell里面使用if else流程控制语句,shell里面的流程控制语句比较特殊的其他的编程语言里,比如JAVA,都是通过一个boolean的值,来判断是否通过某个流程,在shell里面,却是通过shell执行命令的状态码来识别的,返回为0的状态码,代表为true。
[code="java"]# cat test.sh
if date
then
echo "success!"
fi
# sh test.sh
2014年 08月 09日 星期六 04:13:47 CST
success!
# [/code]
if then还可以不用换行写,但需要加个分号如下:
[code="java"]# cat test.sh
if date ;then
echo "success!"
fi
# sh test.sh
2014年 08月 09日 星期六 04:15:42 CST
success!
# [/code]
下面看下if-then-else语句的使用:
[code="java"]# cat test.sh
if date ;then
echo "success!"
fi
# sh test.sh
2014年 08月 09日 星期六 04:15:42 CST
success!
# [/code]
下面看下多重if-else的使用:
[code="java"]# cat c.sh
if (( 3 < 2 )) ; then
echo "3<2"
elif (( 4 < 3 )); then
echo "4<3"
elif (( 90 < 40 )); then
echo "90<100"
else
echo "all is error!"
fi
# sh c.sh
all is error!
# [/code]
下面说下test命令,test命令提供了在if-then语句中测试不同条件的途径,如果test中的条件成立,test命令的就会退出状态码为0,如果不成立,就会返回状态码1,
命令格式:
if test condition
then
commands
fi
这个等同于下面的写法在bash shell里面的写法:
if [ condition ]
then
commands
fi
方括号定义了test命令用到的条件,注意括号两侧都需要加一个空格,否则会报错。
test命令,可以判断3类条件:
数值比较;
字符串比较;
文件比较;
数值比较:
n1 -eq n2 判断n1是否和n2相等
n1 -ge n2 判断是否大于或等于n2
n1 -gt n2 检查n1是否大于n2
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2
[code="java"]# cat d.sh
if [ 10 -eq 12 ] ; then
echo "10==12"
else
echo "10!=12"
fi
if [ 3 -ge 1 ] ; then
echo "3 > 1"
else
echo "3 < 1"
fi
# sh d.sh
10!=12
3 > 1
# [/code]
下面给个字符串比较的例子:
[code="java"]# cat e.sh
if [ "abc" = "abc" ];then
echo "abc=abc"
else
echo "abc!=abc"
fi
echo "======================="
if [ 'abc' = "abc" ]; then
echo "abc=abc"
fi
#数值类型的等号比较
if [ 6 = 7 ] ;then
echo "6=7"
else
echo "6!=7"
fi
# sh e.sh
abc=abc
=======================
abc=abc
6!=7
# [/code]
注意数值相等比较用=号,方括号左右都必须有一个空格
字符串比较的一些用法:
(注意在方括号内的>,<符号,比较字符串需要转义\>)
str1 = str2 字符串相等比较
str1 != str2 字符串不相等比较
str1 < str2 检查str1是否小于str2(按字典顺序)
str1 > str2 检查str1是否大于str2(按字典顺序)
-n str 检查字符串str的长度是否非0
-z str 检查字符串str的长度是否为0
[code="java"]# cat aa.sh
str="china"
str2=""
if [ -n $str ] ; then
echo "str is not empty"
else
echo "str is empty"
fi
if [ -z $str2 ] ; then
echo "str2 is empty"
else
echo "str2 is not empty"
fi
time=`date`
echo "当前时间: $time"
# sh aa.sh
str is not empty
str2 is empty
当前时间: 2014年 08月 09日 星期六 04:58:43 CST
# [/code]
最后看下文件比较
-d file 检查file是否存在并是否为一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并是否非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属于当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
[code="java"]# cat bb.sh
#测试是否为目录
if [ -d $HOME ] ; then
echo "it is a dir"
cd $HOME
ls -la
else
echo "it is not a dir"
fi
echo "=========================="
#测试是否为文件
if [ -f /etc/profile ] ; then
echo "it is a file"
else
echo "it is not a file"
fi
# sh bb.sh
it is a dir
总用量 329652
dr-xr-x---. 13 root root 4096 8月 9 05:07 .
dr-xr-xr-x. 22 root root 4096 8月 8 19:34 ..
-rw-r--r-- 1 root root 143775368 7月 28 19:30 abc1.txt
-rw-------. 1 root root 1087 6月 13 19:06 anaconda-ks.cfg
-rw-r--r-- 1 root root 65 8月 8 04:11 a.sh
-rw-------. 1 root root 10267 8月 9 04:22 .bash_history
-rw-r--r--. 1 root root 18 5月 20 2009 .bash_logout
-rw-r--r--. 1 root root 176 5月 20 2009 .bash_profile
-rw-r--r--. 1 root root 119 6月 16 21:12 .bashrc
-rw-r--r-- 1 root root 75 8月 8 05:07 bc.sh
-rw-r--r-- 1 root root 90 8月 8 04:14 b.sh
-rw-r--r-- 1 root root 141 8月 8 05:12 cc.sh
-rw-r--r-- 1 root root 52 7月 31 21:29 count2.txt
-rw-r--r-- 1 root root 52 7月 31 19:46 count.txt
-rw-r--r-- 1 root root 127 8月 8 04:20 c.sh
-rw-r--r--. 1 root root 100 9月 23 2004 .cshrc
-rw-r--r--. 1 root root 96183833 6月 9 17:27 hadoop-2.2.0.tar.gz
-rw-r--r-- 1 root root 1 7月 31 21:25 hh.txt
drwxr-xr-x 3 root root 4096 7月 29 04:47 hivesrc
-rw-r--r--. 1 root root 2111 6月 16 13:10 initserver.sh
-rw-r--r--. 1 root root 7995 6月 13 19:06 install.log
-rw-r--r--. 1 root root 3384 6月 13 19:06 install.log.syslog
drwxr-xr-x 2 root root 4096 7月 31 21:19 intest
lrwxrwxrwx 1 root root 12 7月 31 21:45 jdk -> jdk1.7.0_25/
drwxr-xr-x. 8 search 143 4096 6月 6 2013 jdk1.7.0_25
-rwx------. 1 root root 96316511 11月 20 2013 jdk-7u25-linux-x64.gz
drwxr-xr-x 3 root root 4096 7月 31 21:33 li
drwxr-xr-x 3 root root 4096 7月 9 04:08 lo
-rw-r--r-- 1 root root 25 8月 8 04:20 log.140808
drwxr-xr-x 3 root root 4096 7月 9 04:08 login
drwxr-xr-x 3 root root 4096 7月 29 04:11 .m2
-rw------- 1 root root 727 7月 29 01:44 .mysql_history
-rw-r--r-- 1 root root 1048 6月 19 03:31 setlimit.sh
drwx------. 2 root root 4096 6月 16 21:00 .ssh
-rw-r--r--. 1 root root 129 12月 4 2004 .tcshrc
drwxr-xr-x 2 root root 4096 8月 9 05:07 test
drwxr-sr-x 2 root abc 4096 8月 6 01:53 testidr
-rwxr--r-- 1 root root 46 8月 8 03:43 test.sh
drwxr-xr-x 3 root root 4096 6月 20 02:51 tsethadoop
-rw-r--r-- 1 root root 87 8月 8 05:29 t.sh
-rw------- 1 root root 5191 8月 9 05:07 .viminfo
-rw-r--r-- 1 root root 1110849 4月 7 17:51 歌曲.mp3
==========================
it is a file
# [/code]
比较新旧文件的命令:
[code="java"]# ll
总用量 36
-rw-r--r-- 1 root root 228 8月 9 04:58 aa.sh
-rw-r--r-- 1 root root 23 8月 5 01:44 a.sh
-rw-r--r-- 1 root root 275 8月 9 05:07 bb.sh
-rw-r--r-- 1 root root 54 8月 9 04:19 b.sh
-rw-r--r-- 1 root root 90 8月 9 05:11 cc.sh
-rw-r--r-- 1 root root 145 8月 9 04:25 c.sh
-rw-r--r-- 1 root root 128 8月 9 04:37 d.sh
-rw-r--r-- 1 root root 236 8月 9 04:43 e.sh
-rw-r--r-- 1 root root 38 8月 9 04:15 test.sh
# cat cc.sh
if [ ./a.sh -nt ./b.sh ] ; then
echo "a.sh is new "
else
echo "b.sh is new"
fi;
# sh cc.sh
b.sh is new
# [/code]
多个条件组合测试:
&&两个条件同时满足
||满足其中一个即可
[code="java"]# cat dd.sh
if [ 3 \> 2 ] && [ 4 \> 5 ] ; then
echo "3>2 and 4>5"
else
echo "condition is not fit"
fi
echo "=============================================="
if [ 3 \> 2 ] && [ 9 \> 5 ] ; then
echo "3>2 and 9>5"
else
echo "condition is not fit"
fi
# sh dd.sh
condition is not fit
==============================================
3>2 and 9>5
# [/code]
上面的表达式比较繁琐,shell里面特意提供了针对数学运算的((exprssion))双圆括号的命令和针对字符串的双方括号的命令:
[code="java"]# cat z.sh
if (( 4 > 6 )) ; then
echo "4 > 6"
else
echo " 4 > 6"
fi
# sh z.sh
4 > 6
# [/code]
双方括号提供了,更高级的字符串操作命令,利用了正则的用法:
[code="java"]# cat h.sh
if [[ "sanxian" == s* ]] ; then
echo "begin with s"
else
echo "begin is not s"
fi
# sh h.sh
begin with s
# [/code]
注意在==号右边的正则式不能用双引号括起来
最后再来看下多重if-else的替代命令case命令的用法:
语法格式:
case var in
pattern1 | pattern2 ) commands1::
pattern3) commands2::
*) default commands::
esac
例子如下:
[code="java"]# cat case.sh
case "1" in
"1" | "11") echo "execute this 1" ;;
"2") echo "execute this 2" ;;
*) echo "this is all default" ;;
esac
# sh case.sh
execute this 1
# [/code]
注意最后的结束符,是由两个挨着的分号构成的,通过上面的例子,我们就会发现shell里面的case语句非常强大和灵活,尽可能的使用case语句,来使我们的流程更加清晰。
下面我们来看看如何在shell里面使用if else流程控制语句,shell里面的流程控制语句比较特殊的其他的编程语言里,比如JAVA,都是通过一个boolean的值,来判断是否通过某个流程,在shell里面,却是通过shell执行命令的状态码来识别的,返回为0的状态码,代表为true。
[code="java"]# cat test.sh
if date
then
echo "success!"
fi
# sh test.sh
2014年 08月 09日 星期六 04:13:47 CST
success!
# [/code]
if then还可以不用换行写,但需要加个分号如下:
[code="java"]# cat test.sh
if date ;then
echo "success!"
fi
# sh test.sh
2014年 08月 09日 星期六 04:15:42 CST
success!
# [/code]
下面看下if-then-else语句的使用:
[code="java"]# cat test.sh
if date ;then
echo "success!"
fi
# sh test.sh
2014年 08月 09日 星期六 04:15:42 CST
success!
# [/code]
下面看下多重if-else的使用:
[code="java"]# cat c.sh
if (( 3 < 2 )) ; then
echo "3<2"
elif (( 4 < 3 )); then
echo "4<3"
elif (( 90 < 40 )); then
echo "90<100"
else
echo "all is error!"
fi
# sh c.sh
all is error!
# [/code]
下面说下test命令,test命令提供了在if-then语句中测试不同条件的途径,如果test中的条件成立,test命令的就会退出状态码为0,如果不成立,就会返回状态码1,
命令格式:
if test condition
then
commands
fi
这个等同于下面的写法在bash shell里面的写法:
if [ condition ]
then
commands
fi
方括号定义了test命令用到的条件,注意括号两侧都需要加一个空格,否则会报错。
test命令,可以判断3类条件:
数值比较;
字符串比较;
文件比较;
数值比较:
n1 -eq n2 判断n1是否和n2相等
n1 -ge n2 判断是否大于或等于n2
n1 -gt n2 检查n1是否大于n2
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2
[code="java"]# cat d.sh
if [ 10 -eq 12 ] ; then
echo "10==12"
else
echo "10!=12"
fi
if [ 3 -ge 1 ] ; then
echo "3 > 1"
else
echo "3 < 1"
fi
# sh d.sh
10!=12
3 > 1
# [/code]
下面给个字符串比较的例子:
[code="java"]# cat e.sh
if [ "abc" = "abc" ];then
echo "abc=abc"
else
echo "abc!=abc"
fi
echo "======================="
if [ 'abc' = "abc" ]; then
echo "abc=abc"
fi
#数值类型的等号比较
if [ 6 = 7 ] ;then
echo "6=7"
else
echo "6!=7"
fi
# sh e.sh
abc=abc
=======================
abc=abc
6!=7
# [/code]
注意数值相等比较用=号,方括号左右都必须有一个空格
字符串比较的一些用法:
(注意在方括号内的>,<符号,比较字符串需要转义\>)
str1 = str2 字符串相等比较
str1 != str2 字符串不相等比较
str1 < str2 检查str1是否小于str2(按字典顺序)
str1 > str2 检查str1是否大于str2(按字典顺序)
-n str 检查字符串str的长度是否非0
-z str 检查字符串str的长度是否为0
[code="java"]# cat aa.sh
str="china"
str2=""
if [ -n $str ] ; then
echo "str is not empty"
else
echo "str is empty"
fi
if [ -z $str2 ] ; then
echo "str2 is empty"
else
echo "str2 is not empty"
fi
time=`date`
echo "当前时间: $time"
# sh aa.sh
str is not empty
str2 is empty
当前时间: 2014年 08月 09日 星期六 04:58:43 CST
# [/code]
最后看下文件比较
-d file 检查file是否存在并是否为一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并是否非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属于当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
[code="java"]# cat bb.sh
#测试是否为目录
if [ -d $HOME ] ; then
echo "it is a dir"
cd $HOME
ls -la
else
echo "it is not a dir"
fi
echo "=========================="
#测试是否为文件
if [ -f /etc/profile ] ; then
echo "it is a file"
else
echo "it is not a file"
fi
# sh bb.sh
it is a dir
总用量 329652
dr-xr-x---. 13 root root 4096 8月 9 05:07 .
dr-xr-xr-x. 22 root root 4096 8月 8 19:34 ..
-rw-r--r-- 1 root root 143775368 7月 28 19:30 abc1.txt
-rw-------. 1 root root 1087 6月 13 19:06 anaconda-ks.cfg
-rw-r--r-- 1 root root 65 8月 8 04:11 a.sh
-rw-------. 1 root root 10267 8月 9 04:22 .bash_history
-rw-r--r--. 1 root root 18 5月 20 2009 .bash_logout
-rw-r--r--. 1 root root 176 5月 20 2009 .bash_profile
-rw-r--r--. 1 root root 119 6月 16 21:12 .bashrc
-rw-r--r-- 1 root root 75 8月 8 05:07 bc.sh
-rw-r--r-- 1 root root 90 8月 8 04:14 b.sh
-rw-r--r-- 1 root root 141 8月 8 05:12 cc.sh
-rw-r--r-- 1 root root 52 7月 31 21:29 count2.txt
-rw-r--r-- 1 root root 52 7月 31 19:46 count.txt
-rw-r--r-- 1 root root 127 8月 8 04:20 c.sh
-rw-r--r--. 1 root root 100 9月 23 2004 .cshrc
-rw-r--r--. 1 root root 96183833 6月 9 17:27 hadoop-2.2.0.tar.gz
-rw-r--r-- 1 root root 1 7月 31 21:25 hh.txt
drwxr-xr-x 3 root root 4096 7月 29 04:47 hivesrc
-rw-r--r--. 1 root root 2111 6月 16 13:10 initserver.sh
-rw-r--r--. 1 root root 7995 6月 13 19:06 install.log
-rw-r--r--. 1 root root 3384 6月 13 19:06 install.log.syslog
drwxr-xr-x 2 root root 4096 7月 31 21:19 intest
lrwxrwxrwx 1 root root 12 7月 31 21:45 jdk -> jdk1.7.0_25/
drwxr-xr-x. 8 search 143 4096 6月 6 2013 jdk1.7.0_25
-rwx------. 1 root root 96316511 11月 20 2013 jdk-7u25-linux-x64.gz
drwxr-xr-x 3 root root 4096 7月 31 21:33 li
drwxr-xr-x 3 root root 4096 7月 9 04:08 lo
-rw-r--r-- 1 root root 25 8月 8 04:20 log.140808
drwxr-xr-x 3 root root 4096 7月 9 04:08 login
drwxr-xr-x 3 root root 4096 7月 29 04:11 .m2
-rw------- 1 root root 727 7月 29 01:44 .mysql_history
-rw-r--r-- 1 root root 1048 6月 19 03:31 setlimit.sh
drwx------. 2 root root 4096 6月 16 21:00 .ssh
-rw-r--r--. 1 root root 129 12月 4 2004 .tcshrc
drwxr-xr-x 2 root root 4096 8月 9 05:07 test
drwxr-sr-x 2 root abc 4096 8月 6 01:53 testidr
-rwxr--r-- 1 root root 46 8月 8 03:43 test.sh
drwxr-xr-x 3 root root 4096 6月 20 02:51 tsethadoop
-rw-r--r-- 1 root root 87 8月 8 05:29 t.sh
-rw------- 1 root root 5191 8月 9 05:07 .viminfo
-rw-r--r-- 1 root root 1110849 4月 7 17:51 歌曲.mp3
==========================
it is a file
# [/code]
比较新旧文件的命令:
[code="java"]# ll
总用量 36
-rw-r--r-- 1 root root 228 8月 9 04:58 aa.sh
-rw-r--r-- 1 root root 23 8月 5 01:44 a.sh
-rw-r--r-- 1 root root 275 8月 9 05:07 bb.sh
-rw-r--r-- 1 root root 54 8月 9 04:19 b.sh
-rw-r--r-- 1 root root 90 8月 9 05:11 cc.sh
-rw-r--r-- 1 root root 145 8月 9 04:25 c.sh
-rw-r--r-- 1 root root 128 8月 9 04:37 d.sh
-rw-r--r-- 1 root root 236 8月 9 04:43 e.sh
-rw-r--r-- 1 root root 38 8月 9 04:15 test.sh
# cat cc.sh
if [ ./a.sh -nt ./b.sh ] ; then
echo "a.sh is new "
else
echo "b.sh is new"
fi;
# sh cc.sh
b.sh is new
# [/code]
多个条件组合测试:
&&两个条件同时满足
||满足其中一个即可
[code="java"]# cat dd.sh
if [ 3 \> 2 ] && [ 4 \> 5 ] ; then
echo "3>2 and 4>5"
else
echo "condition is not fit"
fi
echo "=============================================="
if [ 3 \> 2 ] && [ 9 \> 5 ] ; then
echo "3>2 and 9>5"
else
echo "condition is not fit"
fi
# sh dd.sh
condition is not fit
==============================================
3>2 and 9>5
# [/code]
上面的表达式比较繁琐,shell里面特意提供了针对数学运算的((exprssion))双圆括号的命令和针对字符串的双方括号的命令:
[code="java"]# cat z.sh
if (( 4 > 6 )) ; then
echo "4 > 6"
else
echo " 4 > 6"
fi
# sh z.sh
4 > 6
# [/code]
双方括号提供了,更高级的字符串操作命令,利用了正则的用法:
[code="java"]# cat h.sh
if [[ "sanxian" == s* ]] ; then
echo "begin with s"
else
echo "begin is not s"
fi
# sh h.sh
begin with s
# [/code]
注意在==号右边的正则式不能用双引号括起来
最后再来看下多重if-else的替代命令case命令的用法:
语法格式:
case var in
pattern1 | pattern2 ) commands1::
pattern3) commands2::
*) default commands::
esac
例子如下:
[code="java"]# cat case.sh
case "1" in
"1" | "11") echo "execute this 1" ;;
"2") echo "execute this 2" ;;
*) echo "this is all default" ;;
esac
# sh case.sh
execute this 1
# [/code]
注意最后的结束符,是由两个挨着的分号构成的,通过上面的例子,我们就会发现shell里面的case语句非常强大和灵活,尽可能的使用case语句,来使我们的流程更加清晰。