前言: 实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。
一,shell的流程语句1,条件语句if else if示例代码: [root@squid-2 script]# cat s1.sh #!/bin/bash echo "Please choose project:" echo "1:zhu 2:sha" read project_no if [ $project_no = "1" ];then echo "111111" elif [ $project_no = "2" ];then echo "222222" else echo "error" fi [root@squid-2 script]#
执行过程如下: [root@squid-2 script]# sh s1.sh Please choose project: 1:zhu 2:sha 1 111111 [root@squid-2 script]# sh s1.sh Please choose project: 1:zhu 2:sha 2 222222 [root@squid-2 script]# sh s1.sh Please choose project: 1:zhu 2:sha 3 error [root@squid-2 script]#
2,for 循环2.1 for i in脚本如下: [root@squid-2 script]# cat host_list.txt 192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 [root@squid-2 script]#
测试执行结果: [root@squid-2 script]# sh s21.sh the host ip address is: 192.168.1.10 the host ip address is: 192.168.1.11 the host ip address is: 192.168.1.12 the host ip address is: 192.168.1.13 [root@squid-2 script]#
2.2 for((赋值;条件;运算语句))脚本代码: [root@squid-2 script]# cat s22.sh for((i=1;i<=10;i++));do echo "the loop number i: $i"; done; [root@squid-2 script]#
执行结果: [root@squid-2 script]# sh s22.sh the loop number i: 1 the loop number i: 2 the loop number i: 3 the loop number i: 4 the loop number i: 5 the loop number i: 6 the loop number i: 7 the loop number i: 8 the loop number i: 9 the loop number i: 10 [root@squid-2 script]#
3,while循环使用条件语句结构: while do action done;
测试脚本: [root@squid-2 script]# cat s3.sh #!/bin/sh i=10; while [[ $i -gt 5 ]];do echo"the loop number of while case is: $i"; ((i--)); done; [root@squid-2 script]
执行结果: [root@squid-2 script]# sh s3.sh the loop number of while case is: 10 the loop number of while case is: 9 the loop number of while case is: 8 the loop number of while case is: 7 the loop number of while case is: 6 [root@squid-2 script]#
4,until循环语句示例脚本: [root@squid-2 script]# cat s4.sh #!/bin/sh a=1; until [[ $a -gt 6 ]];do echo"the until number is: $a"; ((a++)); done; [root@squid-2 script]#
执行结果: [root@squid-2 script]# sh s4.sh the until number is: 1 the until number is: 2 the until number is: 3 the until number is: 4 the until number is: 5 the until number is: 6 [root@squid-2 script]#
5,shell选择语句5.1,使用case选择语句使用(case/esac)语法结构: case $arg in pattern | sample) # arg in pattern or sample ;; pattern1) # arg in pattern1 ;; *) #default ;; esac
说明:pattern1 是正则表达式,可以用下面字符:
* 任意字串 ? 任意字元 [abc] a, b, 或c三字元其中之一 [a-n] 从a到n的任一字元 | 多重选择
代码脚本: [root@squid-2 script]# cat s51.sh #!/bin/sh
case $1 in start | begin) echo "start something" ;; stop | end) echo "stop something" ;; *) echo "Ignorant" ;; esac [root@squid-2 script]#
PS:执行结果,这里需要带入参数,参数值就在)前面的start、begin、stop、end之内,如果带入别参数,则返回"Ignorant": [root@squid-2 script]# sh s51.sh start start something [root@squid-2 script]# sh s51.sh begin start something [root@squid-2 script]# sh s51.sh end stop something [root@squid-2 script]# sh s51.sh stop stop something [root@squid-2 script]# sh s51.sh test1 Ignorant
5.2,select语句使用方法(产生菜单选择)语法结构: select 变量name in seq变量 do action done
代码如下: cat s52.sh #!/bin/sh select param in "begin""end" "exit" do case $param in "begin") echo "start something" ;; "end") echo "stop something" ;; "exit") echo "exit" break; ;; *) echo "Ignorant" ;; esac done;
执行结果: [root@squid-2 script]# sh s52.sh begin 1) begin 2) end 3) exit #? 1 start something #? 2 stop something #? 3 exit [root@squid-2 script]# PS:执行的时候,只有输入exit,才能退出来执行小窗口。 说明:select是循环选择,一般与case语句使用。
二,shell语句的比较操作符1,整数比较规则说明:
-eq 等于 if [ "$a" -eq "$b" ] -ne 不等于 if [ "$a" -ne "$b" ] -gt 大于 if [ "$a" -gt "$b" ] -ge 大于等于 if [ "$a" -ge "$b" ] -lt 小于 if [ "$a" -lt "$b" ] -le 小于等于 if [ "$a" -le "$b" ] < 小于(需要双括号) (( "$a"< "$b" )) <= 小于等于(...) (("$a" <= "$b" )) > 大于(...) (("$a" > "$b" )) >= 大于等于(...) (("$a" >= "$b" )) PS:小数据比较可使用AWK
示例代码: [root@squid-2 script]# cat com1.sh a=$1 b=$2 if [ "$a" -eq "$b" ];then echo"a = b true." elif [ ! "$a" -gt "$b" ];then echo"a > b true." else echo "a < b true." fi [root@squid-2 script]#
测试结果如下: [root@squid-2 script]# sh com1.sh 1 1 a = b true. [root@squid-2 script]# sh com1.sh 1 2 a > b true. [root@squid-2 script]# sh com1.sh 1 0 a < b true. [root@squid-2 script]#
2,字符串比较2.1,规范以及使用规则说明: = 等于 if [ "$a"= "$b" ] == 与=等价 != 不等于 if [ "$a" ="$b" ] < 小于,在ASCII字母中的顺序: if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ] #需要对<进行转义 > 大于 -z 字符串为null,即长度为0 -n 字符串不为null,即长度不为0
示例代码: [root@squid-2 script]# cat com2.sh a=$1 b=$2
# 1 the first method to implement if [ "$a"x = "$b"x ];then echo"a = b" elif [ ! "$a"x = "$b"x ]; then echo"a != b" else echo "others" fi
# 2 the second method to implement if [ "$a"x == "$b"x ];then echo "a = b" elif [ "$a"x != "$b"x ]; then echo "a != b" else echo "others" fi
测试执行结果: [root@squid-2 script]# sh com2.sh ccb aaa a != b a != b [root@squid-2 script]# sh com2.sh ccb ccb a = b a = b [root@squid-2 script]# [root@squid-2 script]#
2.2,需要注意的地方相等判断需要注意的地方: 比较两个字符串是否相等的办法是: if [ "$a"x = "b"x ];then 这里的关键有几点: 1 使用单个等号 2 注意到等号两边各有一个空格:这是unix shell的要求 3 注意到"$a"x最后的x,这是特意安排的,因为当$test为空的时候,上面的表达式就变成了x = bx,显然是不相等的。而如果没有这个x,表达式就会报错:[: =: unary operator expected
[[ $a == z* ]] # 如果$a以"z"开头(模式匹配)那么将为true [[ $a == "z*" ]] # 如果$a等于z*(字符匹配),那么结果为true [ $a == z* ] # File globbing 和word splitting将会发生 [ "$a" == "z*" ] # 如果$a等于z*(字符匹配),那么结果为true
关于File globbing: 一点解释,关于Fileglobbing是一种关于文件的速记法,比如"*.c"就是,再如~也是.
特殊字符的比较:
2.3,判断字符串是否空示例代码: [root@squid-2 script]# cat com3.sh #!/bin/bash a=$1 if [ -z "$a" ]; then echo"a is empty." else echo "a is a object." fi [root@squid-2 script]
测试执行结果: [root@squid-2 script]# sh com3.sh a a is a object. [root@squid-2 script]# sh com3.sh a is empty. [root@squid-2 script]#
3,文件判断规则说明: -e 文件存在 f1-nt f2 文件f1是否比f2新
示例脚本代码: [root@squid-2 script]# cat com4.sh #!/bin/bash a=$1 file=$2 if [ -d $a ]; then echo"it is a directory." elif [ -f "$a" ]; then echo"it is a file." else echo "no parameter." fi
测试运行结果如下: [root@squid-2 script]# sh com4.sh log1 it is a file. [root@squid-2 script]# sh com4.sh old it is a directory. [root@squid-2 script]#
参考资料: http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html http://blog.csdn.net/yf210yf/article/category/1475895 |
|
来自: jackeyqing > 《学习整理360doc》