shell脚本

[0],简介:用于把一连串shell命令写进一个脚本文档里面执行,节省每次重复输入的麻烦。

 

[1],一个最简单的shell脚本:baseuse.sh文件,打印hello world

1 #!/bin/bash                #shell脚本的固定的语句开头#!,/bin/bash指定的是以bash来解析下面的脚本
2 echo "hello world"      #输出hello world语句    
1 oee@copener:~/workspace/test/shell$ chmod u+x baseuse.sh 
2 oee@copener:~/workspace/test/shell$ ./baseuse.sh 
3 hello world

  

[2],自定义变量,变量都以$开头,变量在使用时并不需要提前定义。变量以字符中形式存储在内存中,若变量中只有数字则解析为整形数,若变量中有非数字字符则解析为字符串。

  [baseuse.sh]  

 1 #!/bin/bash
 2 a=1 ; b=2
 3 sum=$((a+b))
 4 echo "result is $sum"
 5 
 6 version=""
 7 echo linux version is: ${version:- ubuntu14.04}

[终端运行]

1 oee@copener:~/workspace/test/shell$ ./baseuse.sh 2 result is 3
3 linux version is: ubuntu14.04

引用变量三种方法:"$var"  ${var}  $var  例如上面第7行中的$sum  

  设置变量默认值${var:-defaultvalue},在变量值不为空的时候进行替代,例如上面第10行的version变量

  

  shell可以向脚本传递命令行参数,通过$1~$9来调用,$0表示当前执行进程的文件名。通过shift n可以将所有的位置参数向左移动n个位。

  [locatever.sh]

 1 #!/bin/bash 
 2 echo n0: $0
 3 echo n1: $1
 4 echo n2: $2
 5 echo n3: $3
 6 echo n4: $4
 7 echo n5: $5
 8 echo n6: $6
 9 echo n7: $7
10 echo n8: $8
11 echo n9: $9
12 shift 1 
13 echo n0: $0
14 echo n1: $1
15 echo n2: $2
16 echo n3: $3
17 echo n4: $4
18 echo n5: $5
19 echo n6: $6
20 echo n7: $7
21 echo n8: $8
22 echo n9: $9

[终端运行]   通过shift 1将所有的位置参数向左移动1位,原来的n1=1变为n1=2,原来的n9=9变为n9为空

 1 oee@copener:~/workspace/test/shell$ ./locatever.sh 1 2 3 4 5 6 7 8 9
 2 n0: ./locatever.sh
 3 n1: 1
 4 n2: 2
 5 n3: 3
 6 n4: 4
 7 n5: 5
 8 n6: 6
 9 n7: 7
10 n8: 8
11 n9: 9
12 n0: ./locatever.sh
13 n1: 2
14 n2: 3
15 n3: 4
16 n4: 5
17 n5: 6
18 n6: 7
19 n7: 8
20 n8: 9
21 n9: 

 

[3]退出状态,exit命令用于结束一个shell脚本运行,成功执行返回0,否则返回一个非零值。

  [exit.sh]

1 #!/bin/bash
2 echo goodby world
3 exit 0  

  [终端运行] 命令echo $?可以查看Shell终端最后一次运行进程的退出状态码

1 oee@copener:~/workspace/test/shell$ ./exit.sh 
2 goodby world
3 oee@copener:~/workspace/test/shell$ echo $?
4 0 

 

[4]条件测试

  [4-1]文件测试

  [conditiontest.sh]  

 1 #!/bin/bash
 2 #1.file condition test
 3 [ -d baseuse.sh ]   #baseuse.sh is a dir?
 4 echo $?
 5 [ -s baseuse.sh ]   #baseuse.sh length>0?
 6 echo $?
 7 [ -f baseuse.sh ]   #baseuse.sh is a normal file?
 8 echo $?
 9 [ -L baseuse.sh ]   #baseuse.sh is a simble link?
10 echo $?
11 [ -u baseuse.sh ]   #baseuse.sh have set suid?
12 echo $?
13 [ -r baseuse.sh ]   #baseuse.sh is read enable?
14 echo $?
15 [ -w baseuse.sh ]   #baseuse.sh is write enable?
16 echo $?
17 [ -x baseuse.sh ]   #baseuse.sh is executed enable?
18 echo $?
19 
20 exit 0

  [终端运行] 对应返回值为0时条件命中,非0不命中,如第4行终端输出对应[ -d baseuse.sh ]的测试输出,因为baseuse.sh不是目录,所以未命中,echo $?输出为1.。 

 1 oee@copener:~/workspace/test/shell$ ls -al
 2 -rwxrw-r-- 1 oee oee  422  5月 18 16:01 baseuse.sh
 3 oee@copener:~/workspace/test/shell$ ./conditiontest.sh 
 4 1
 5 0
 6 0
 7 1
 8 1
 9 0
10 0
11 0

   附文件测试参数表:  

技术分享
 1 备注:
 2 -e                          文件存在
 3 -a                          文件存在(已被弃用)
 4 -f                          被测文件是一个regular文件(正常文件,非目录或设备)
 5 -s                          文件长度不为0
 6 -d                          被测对象是目录
 7 -b                          被测对象是块设备
 8 -c                          被测对象是字符设备
 9 -p                          被测对象是管道
10 -h                          被测文件是符号连接
11 -L                          被测文件是符号连接
12 -S(大写)                     被测文件是一个socket
13 -t                          关联到一个终端设备的文件描述符。用来检测脚本的stdin[-t0]或[-t1]是一个终端
14 -r                          文件具有读权限,针对运行脚本的用户
15 -w                          文件具有写权限,针对运行脚本的用户
16 -x                          文件具有执行权限,针对运行脚本的用户
17 -u                          set-user-id(suid)标志到文件,即普通用户可以使用的root权限文件,通过chmod +s file实现
18 -k                          设置粘贴位
19 -O                          运行脚本的用户是文件的所有者
20 -G                          文件的group-id和运行脚本的用户相同
21 -N                          从文件最后被阅读到现在,是否被修改
22 f1 -nt f2                   文件f1是否比f2新
23 f1 -ot f2                   文件f1是否比f2旧
24 f1 -ef f2                   文件f1和f2是否硬连接到同一个文件
View Code

 

  [4-2]测试时使用逻辑操作符:-a逻辑与;-o逻辑或;!逻辑非  

  [conditiontest.sh]

1 #!/bin/bash
2 #2.logic condition test 
3 [ -r baseuse.sh -a -w baseuse.sh ]  #-a means && in c++
4 echo $?
5 [ -d baseuse.sh -o -L baseuse.sh ]  #-o means || in c++
6 echo $?
7 [ ! -L baseuse.sh ]                 #!  means !  in C++
8 echo $?

[终端运行]

1 oee@copener:~/workspace/test/shell$ ls -al
2 -rwxrw-r-- 1 oee oee  422  5月 18 16:01 baseuse.sh
3 oee@copener:~/workspace/test/shell$ ./conditiontest.sh 
4 0
5 1
6 0

 

  [4-3]字符串测试

  [conditiontest.sh]

 1 #!/bin/bash
 2 #3.string test
 3 # #test operator "string"    #[ operator "string" ] 
 4 # #= (==?)    #!= (!=?)   #-z (=0?)     #-n (!=0?) 
 5 str1="str1"
 6 str2="str2"
 7 str3="str3"
 8 test ${str1}x = ${str2}x    #is str1==str2?
 9 echo [$?] str1==str2?
10 test ${str1}x != ${str2}x   #is str1!=str2?
11 echo [$?] str1!=str2?
12 [ ${str1}x = ${str2}x ]     #is str1==str2?
13 echo [$?] str1==str2?
14 [ ${str1}x != ${str2}x ]    #is str1!=str2?
15 echo [$?] str1!=str2?
16 test -z ${str3}             #is str3 is empty?
17 echo [$?] str3 empty?
18 test -n ${str3}             #is str3 is not empty?
19 echo [$?] str3 not empty?
20 [ -z ${str1} ]              #is str1 is empty?
21 echo [$?] str1 empty?
22 [ -n ${srt1} ]              #is str1 is not empty?
23 echo [$?] str1 not empty?

[终端运行]

1 oee@copener:~/workspace/test/shell$ ./conditiontest.sh
2 [1] str1==str2?
3 [0] str1!=str2?
4 [1] str1==str2?
5 [0] str1!=str2?
6 [1] str3 empty?
7 [0] str3 not empty?
8 [1] str1 empty?
9 [0] str1 not empty?

   附字符串比较参数

技术分享
1 -z                        //字符串为null,即长度为0
2 -n                        //字符串不为null,即长度不为0
3 =                         //等于            if [ "$a" = "$b" ]
4 ==                       //与=等价
5 !=                        //不等于         if [ "$a" = "$b" ]
6 >                         //大于
7 <                         //小于,在ASCII字母中的顺序:
8                            //if [[ "$a" < "$b" ]]
9                            //if [ "$a" \< "$b" ]         #需要对<进行转义
View Code

 

   

  [4-4]数值测试

   [conditiontest.sh]  

 1 #!/bin/bash
 2 #4.value test
 3 # #"number" operator "number"   #[ "number" operator "number" ]
 4 # #-eq (==) #-ne (!=)   #-gt (first>second)   #-lt (first<second)   #-le (first<=second)   #-ge (first>=second)   
 5 test "1" -eq "2"
 6 echo [$?] 1==2?
 7 test "1" -ne "2"
 8 echo [$?] 1!=2?
 9 test "1" -gt "2"
10 echo [$?] 1\>2
11 test "1" -lt "2"
12 echo [$?] 1\<2
13 test "1" -le "2"
14 echo [$?] 1\<\=2
15 test "1" -ge "2"
16 echo [$?] 1\>\=2  

  [终端运行]  

1 oee@copener:~/workspace/test/shell$ ./conditiontest.sh
2 [1] 1==2?
3 [0] 1!=2?
4 [1] 1>2
5 [0] 1<2
6 [0] 1<=2
7 [1] 1>=2

  附数值比较参数  

技术分享
 1 -eq                      //等于              if [ "$a" -eq "$b" ]
 2 -ne                      //不等于           if [ "$a" -ne "$b" ]
 3 -gt                       //大于               if [ "$a" -gt "$b" ]
 4 -ge                      //大于等于        if [ "$a" -ge "$b" ]
 5 -lt                        //小于               if [ "$a"  -lt  "$b" ]
 6 -le                       //小于等于        if [ "$a"  -le "$b" ]
 7 <                         //小于(需要双括号)   (( "$a"  <  "$b" ))
 8 <=                       //小于等于(...)                (( "$a" <= "$b" ))
 9 >                         //大于(...)                       (( "$a"  >  "$b" ))
10 >=                       //大于等于(...)                (( "$a" >= "$b" ))
View Code

 

[5]控制结构  

 1 #########################################################################
 2 # File Name: control.sh
 3 # Author: copener
 4 # mail: [email protected]
 5 # Created Time: 2015年05月19日 星期二 16时13分35秒
 6 #########################################################################
 7 #!/bin/bash
 8 #1,if elif fi useage
 9 a=1 ; b=2                   #";"分号用于两个语句的分隔符
10 if [ ${a} -gt ${b} ]
11 then
12     echo "a>b"
13 elif [ ${a} -le ${b} ]
14 then
15     echo "a<=b"
16 fi
17 
18 #2,case
19 echo -e "1 save(S/s)\n2 exit(E/e)\nplease choose:"   #"-e"参数使能换行符\n
20 read chioce                 #读取终端输入
21 case ${chioce} in
22     1 | S | s)
23         echo "save";;       #每个case以";;"分隔
24     2 | E | e)
25         echo "exit";;
26     *)
27         echo "invalid choice"
28         exit 1;
29 esac                        #是case的反序字符串,表明case结束
30 
31 #3,while
32 i=1
33 
34 while [ $i -le 5 ]
35 do
36     echo "while result :${i}"
37     let "i=${i}+2"
38 #    ((i=i+2))
39 #    i=$[ $i+2 ]
40 #    ((i++)) 
41 #    let "i++"
42 #    :$((i=$i+2))
43 #    :$[ i++ ] 
44 #    :$[ i=$i+1 ]
45 done
46 
47 #4,until
48 until [ $i -gt 10 ]
49 do 
50     echo "until result :${i}"
51     let "i=${i}+2"
52 done
53 
54 #5,for
55 for i in 10 11 12
56 do
57     echo "for   result :${i}"
58     let "i=${i}+2"
59 done
60 
61 #6,break and continue
62 while true
63 do
64     echo "please input to continue(y) or cancel(n):" 
65     read chioce
66     case $chioce in
67         y | Y | 1)
68             echo "continue ing..."
69             continue;;
70         n | N | 2)
71             echo "cancel ing ..."
72             break;;
73         *)
74             echo "invalid choice";;
75     esac
76 done
77 
78 #7,function
79 sayhello()                      #定义一个函数
80 {
81    echo "hello everybody";
82 }
83 sayhello                        #调用一个函数
84 
85 exit 0

  [终端运行]

 1 oee@copener:~/workspace/test/shell$ ./control.sh
 2 a<=b
 3 1 save(S/s)
 4 2 exit(E/e)
 5 please choose:
 6 1                                                   #此处输入了1
 7 save
 8 while result :1
 9 while result :3
10 while result :5
11 until result :7
12 until result :9
13 for   result :10
14 for   result :11
15 for   result :12
16 please input to continue(y) or cancel(n):
17 2                                                   #此处输入了2 
18 cancel ing ...
19 hello everybody

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。