shell函數與數組
阿新 • • 發佈:2017-10-18
$* 隱藏 pre shel 多次 url 括號 展示 位置參數
shell函數與數組
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.為什麽要使用shell函數 簡單的說函數的作用就是把程序裏多次調用的相同的代碼部分定義成一份,然後起個名字,所有的調用都只用這個名字就可以了。修改代碼時,只需要改變函數體內的代碼即可
1 a>.把相同的代碼定義成函數,可以減少程序代碼量;
2 b>.增加程序的可讀,易讀性;
3 c>.實現程序的功能模塊化;
二.shell語法格式
1 function 函數名() {
2 源代碼
3 return
4 }
三.shell函數的執行
1 調用函數
2 1>.直接執行函數名即可
3 函數名
4 註意:
5 a>.執行函數時,不要帶小括號了;
6 b>.函數定義及函數體必須在要執行的函數名的前面定義,shell的執行從上到下按行執行的;
7 2>.帶參數的函數執行方法
8 函數名 參數1 參數2 .....
9 註意:
10 a>.在函數體中位置參數($1,$2,$3,...,$#,$*,$?以及[email protected])都可以是函數的參數;
11 b>.父腳本的參數則臨時的被函數參數所掩蓋或隱藏;
12 c>.$0比較特殊,他仍然是父腳本的名稱;
13 d>.當函數完成時,原來的命令行參數會恢復;
14 e>.在shell函數裏面。return命令的功能與工作方式與exit相同,用於跳出函數;
15 f>.在shell函數體裏面使用exit會終止整個shell腳本;
16 g>.return語句會返回一個退出值給調用的程序;
四.案例展示
1.調用函數 1 [[email protected] shell]# more function1.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:[email protected]
6
7 function yinzhengjie(){
8 echo "尹正傑到此一遊!"
9 }
10 yinzhengjie
11 [[email protected] shell]#
12 [[email protected] shell]# sh function1.sh
13 尹正傑到此一遊!
14 [[email protected] shell]#
2.導入函數
1 [[email protected] shell]# pwd
2 /root/yinzhengjie/shell
3 [[email protected] shell]#
4 [[email protected] shell]# more function1.sh
5 #!/bin/bash
6 #@author :yinzhengjie
7 #blog:http://www.cnblogs.com/yinzhengjie
8 #EMAIL:[email protected]
9
10 function yinzhengjie(){
11 echo "尹正傑到此一遊!"
12 }
13 yinzhengjie
14 [[email protected] shell]#
15 [[email protected] shell]# more function2.sh
16 #!/bin/bash
17 #@author :yinzhengjie
18 #blog:http://www.cnblogs.com/yinzhengjie
19 #EMAIL:[email protected]
20
21 #如果存在“function1.sh”這個文件,就導入它,註意導入可以用"."
22 [ -f /root/yinzhengjie/shell/function1.sh ]&& . /root/yinzhengjie/shell/function1.sh || exit 100
23
24 yinzhengjie
25 [[email protected] shell]#
26 [[email protected] shell]# sh function2.sh
27 尹正傑到此一遊!
28 尹正傑到此一遊!
29 [[email protected] shell]#
3.函數傳參
1 [[email protected] shell]# more function3.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:[email protected]
6
7 function yinzhengjie(){
8 echo "Welcome to beijing,[$1]."
9 }
10 [[email protected] shell]#
11 [[email protected] shell]#
12 [[email protected] shell]#
13 [[email protected] shell]# more function4.sh
14 #!/bin/bash
15 #@author :yinzhengjie
16 #blog:http://www.cnblogs.com/yinzhengjie
17 #EMAIL:[email protected]
18
19 #導入函數
20 . /root/yinzhengjie/shell/function3.sh
21 yinzhengjie 尹正傑
22
23 [[email protected] shell]#
24 [[email protected] shell]#
25 [[email protected] shell]# sh function4.sh
26 Welcome to beijing,[尹正傑].
27 [[email protected] shell]#
28 [[email protected] shell]#
4.應用案例
1 [[email protected] shell]# more CheckWeb.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:[email protected]
6
7 function CheckUrl(){
8 curl -I -s $1 | head -1 && return 0 || return 100
9 }
10
11 CheckUrl $1
12 [[email protected] shell]#
13 [[email protected] shell]# sh CheckWeb.sh www.baidu.com
14 HTTP/1.1 200 OK
15 [[email protected] shell]#
16 [[email protected] shell]#
五.數組介紹
簡單的說,數組就是相同數據類型的元素按一定順序排列的集合。數組就是把有限個類型相同的變量用一個名字命名,然後用編號區分他們的變量的集合。這個名字成為數組名,編號成為數組下標。組成數組的各個變量成為數組的分量,也成為數組的元素,有時也成為下標變量。 六.數組的定義以及基本使用 1>.聲明數組 1 [[email protected] ~]# yinzhengjie=(100 200 300) #定義數組
2 [[email protected] ~]# echo ${#yinzhengjie[@]} #查看數組長度方式一
3 3
4 [[email protected] ~]# echo ${#yinzhengjie[*]} #查看數組長度方式二
5 3
6 [[email protected] ~]#
7 [[email protected] ~]# echo ${yinzhengjie[0]} #查看某個數組
8 100
9 [[email protected] ~]# echo ${yinzhengjie[*]} #查看整個數組的元素
10 100 200 300
11 [[email protected] ~]#
2>.數組添加新元素
1 [[email protected] ~]# echo ${#yinzhengjie[*]}
2 3
3 [[email protected] ~]#
4 [[email protected] ~]# echo ${yinzhengjie[*]}
5 100 200 300
6 [[email protected] ~]#
7 [[email protected] ~]# yinzhengjie[3]=jie
8 [[email protected] ~]#
9 [[email protected] ~]# echo ${yinzhengjie[*]}
10 100 200 300 jie
11 [[email protected] ~]#
3>.數組的修改
a>.基於下標的形式修改;
1 [[email protected] ~]# echo ${#yinzhengjie[*]}
2 4
3 [[email protected] ~]# echo ${yinzhengjie[*]}
4 100 200 300 jie
5 [[email protected] ~]# yinzhengjie[0]=yin
6 [[email protected] ~]# yinzhengjie[1]=zheng
7 [[email protected] ~]#
8 [[email protected] ~]# echo ${yinzhengjie[*]}
9 yin zheng 300 jie
10 [[email protected] ~]#
b>.重新賦值方式修改
1 [[email protected] ~]# yinzhengjie=(100 200 300 400 500 600)
2 [[email protected] ~]# echo ${#yinzhengjie[@]}
3 6
4 [[email protected] ~]# echo ${yinzhengjie[@]}
5 100 200 300 400 500 600
6 [[email protected] ~]#
7 [[email protected] ~]# a=(${yinzhengjie[@]/500/5})
8 [[email protected] ~]# echo ${a[@]}
9 100 200 300 400 5 600
10 [[email protected] ~]# echo ${yinzhengjie[@]}
11 100 200 300 400 500 600
12 [[email protected] ~]#
4>.數組的刪除
1 [[email protected] ~]# echo ${#yinzhengjie[*]}
2 4
3 [[email protected] ~]#
4 [[email protected] ~]# echo ${yinzhengjie[*]}
5 yin zheng 300 jie
6 [[email protected] ~]# unset yinzhengjie[2]
7 [[email protected] ~]#
8 [[email protected] ~]# echo ${#yinzhengjie[*]}
9 3
10 [[email protected] ~]# echo ${yinzhengjie[*]}
11 yin zheng jie
12 [[email protected] ~]#
13 [[email protected] ~]# unset yinzhengjie
14 [[email protected] ~]# echo ${yinzhengjie[*]}
15
16 [[email protected] ~]#
5>.數組的截取
1 [[email protected] ~]# yinzhengjie=(100 200 300 400 500 600)
2 [[email protected] ~]# echo ${#yinzhengjie[@]}
3 6
4 [[email protected] ~]# echo ${yinzhengjie[@]}
5 100 200 300 400 500 600
6 [[email protected] ~]# echo ${yinzhengjie[@]:1:3}
7 200 300 400
8 [[email protected] ~]# echo ${yinzhengjie[@]:3:2}
9 400 500
10 [[email protected] ~]#
七.應用案例 1.案例一:把命令結果作為數組元素。
1 [[email protected] shell]# yinzhengjie=($(ls))
2 [[email protected] shell]# ls | wc -l
3 17
4 [[email protected] shell]# for((i=0;i<${#yinzhengjie[@]};i++));do echo ${yinzhengjie[$i]};done
5 argv1.sh
6 argv2.sh
7 argv3.sh
8 CheckWeb.sh
9 file_backup.sh
10 function1.sh
11 function2.sh
12 function3.sh
13 function4.sh
14 ls.log
15 partitions.sh
16 read.sh
17 res.txt
18 Tetris.sh
19 until.sh
20 while.sh
21 yinzhengjie.sh
22 [[email protected] shell]#
23 [[email protected] shell]# for file in ${yinzhengjie[@]};do echo $file;done
24 argv1.sh
25 argv2.sh
26 argv3.sh
27 CheckWeb.sh
28 file_backup.sh
29 function1.sh
30 function2.sh
31 function3.sh
32 function4.sh
33 ls.log
34 partitions.sh
35 read.sh
36 res.txt
37 Tetris.sh
38 until.sh
39 while.sh
40 yinzhengjie.sh
41 [[email protected] shell]#
2.用法展示
1 [[email protected] shell]# more array.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:[email protected]
6
7 array=(
8 yinzhengjie
9 bingan
10 alex
11 mage
12 oldboy
13 brother
14 )
15
16
17 for ((i=0;i<${#array[*]};i++))
18 do
19 echo "This is num [$i],then content is ${array[$i]}"
20 done
21
22
23 echo -----------------line------------------------
24
25
26 for name in ${array[@]}
27 do
28 echo "The boy is [$name]"
29 done
30 [[email protected] shell]#
shell函數與數組