码迷,mamicode.com
首页 > 编程语言 > 详细

shell函数与数组

时间:2017-10-18 02:07:42      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:$?   unset   结果   功能   数据   必须   welcome   function   line   

                            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,...,$#,$*,$?以及$@)都可以是函数的参数;
11         b>.父脚本的参数则临时的被函数参数所掩盖或隐藏;
12         c>.$0比较特殊,他仍然是父脚本的名称;
13         d>.当函数完成时,原来的命令行参数会恢复;
14         e>.在shell函数里面。return命令的功能与工作方式与exit相同,用于跳出函数;
15         f>.在shell函数体里面使用exit会终止整个shell脚本;
16         g>.return语句会返回一个退出值给调用的程序;                            
 
四.案例展示
1.调用函数
 1 [root@yinzhengjie shell]# more function1.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:y1053419035@qq.com
 6 
 7 function yinzhengjie(){
 8         echo "尹正杰到此一游!"
 9 }
10 yinzhengjie
11 [root@yinzhengjie shell]# 
12 [root@yinzhengjie shell]# sh function1.sh 
13 尹正杰到此一游!
14 [root@yinzhengjie shell]# 
 
2.导入函数
 1 [root@yinzhengjie shell]# pwd
 2 /root/yinzhengjie/shell
 3 [root@yinzhengjie shell]# 
 4 [root@yinzhengjie shell]# more function1.sh 
 5 #!/bin/bash
 6 #@author :yinzhengjie
 7 #blog:http://www.cnblogs.com/yinzhengjie
 8 #EMAIL:y1053419035@qq.com
 9 
10 function yinzhengjie(){
11         echo "尹正杰到此一游!"
12 }
13 yinzhengjie
14 [root@yinzhengjie shell]# 
15 [root@yinzhengjie shell]# more function2.sh 
16 #!/bin/bash
17 #@author :yinzhengjie
18 #blog:http://www.cnblogs.com/yinzhengjie
19 #EMAIL:y1053419035@qq.com
20 
21 #如果存在“function1.sh”这个文件,就导入它,注意导入可以用"."
22 [ -f /root/yinzhengjie/shell/function1.sh ]&& . /root/yinzhengjie/shell/function1.sh || exit 100
23 
24 yinzhengjie
25 [root@yinzhengjie shell]# 
26 [root@yinzhengjie shell]# sh function2.sh 
27 尹正杰到此一游!
28 尹正杰到此一游!
29 [root@yinzhengjie shell]# 
 
3.函数传参
 1 [root@yinzhengjie shell]# more function3.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:y1053419035@qq.com
 6 
 7 function yinzhengjie(){
 8     echo "Welcome to beijing,[$1]."
 9 }
10 [root@yinzhengjie shell]# 
11 [root@yinzhengjie shell]# 
12 [root@yinzhengjie shell]# 
13 [root@yinzhengjie shell]# more function4.sh 
14 #!/bin/bash
15 #@author :yinzhengjie
16 #blog:http://www.cnblogs.com/yinzhengjie
17 #EMAIL:y1053419035@qq.com
18 
19 #导入函数
20 . /root/yinzhengjie/shell/function3.sh
21 yinzhengjie 尹正杰
22 
23 [root@yinzhengjie shell]# 
24 [root@yinzhengjie shell]# 
25 [root@yinzhengjie shell]# sh function4.sh 
26 Welcome to beijing,[尹正杰].
27 [root@yinzhengjie shell]# 
28 [root@yinzhengjie shell]# 

 

4.应用案例
 1 [root@yinzhengjie shell]# more CheckWeb.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:y1053419035@qq.com
 6 
 7 function CheckUrl(){
 8     curl -I -s $1  | head -1 && return 0 || return 100
 9 }
10 
11 CheckUrl $1
12 [root@yinzhengjie shell]# 
13 [root@yinzhengjie shell]# sh CheckWeb.sh www.baidu.com
14 HTTP/1.1 200 OK
15 [root@yinzhengjie shell]# 
16 [root@yinzhengjie shell]# 

 

五.数组介绍

  简单的说,数组就是相同数据类型的元素按一定顺序排列的集合。数组就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合。这个名字成为数组名,编号成为数组下标。组成数组的各个变量成为数组的分量,也成为数组的元素,有时也成为下标变量。
 
六.数组的定义以及基本使用
1>.声明数组
 1 [root@yinzhengjie ~]# yinzhengjie=(100 200 300)           #定义数组
 2 [root@yinzhengjie ~]# echo ${#yinzhengjie[@]}            #查看数组长度方式一
 3 3
 4 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}                #查看数组长度方式二
 5 3
 6 [root@yinzhengjie ~]# 
 7 [root@yinzhengjie ~]# echo ${yinzhengjie[0]}                #查看某个数组
 8 100
 9 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}                #查看整个数组的元素
10 100 200 300
11 [root@yinzhengjie ~]#
2>.数组添加新元素
 1 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
 2 3
 3 [root@yinzhengjie ~]# 
 4 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
 5 100 200 300
 6 [root@yinzhengjie ~]# 
 7 [root@yinzhengjie ~]# yinzhengjie[3]=jie
 8 [root@yinzhengjie ~]# 
 9 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
10 100 200 300 jie
11 [root@yinzhengjie ~]#
 
3>.数组的修改
a>.基于下标的形式修改;
 1 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
 2 4
 3 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
 4 100 200 300 jie
 5 [root@yinzhengjie ~]# yinzhengjie[0]=yin
 6 [root@yinzhengjie ~]# yinzhengjie[1]=zheng
 7 [root@yinzhengjie ~]# 
 8 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
 9 yin zheng 300 jie
10 [root@yinzhengjie ~]# 
b>.重新赋值方式修改
 1 [root@yinzhengjie ~]# yinzhengjie=(100 200 300 400 500 600)
 2 [root@yinzhengjie ~]# echo ${#yinzhengjie[@]}
 3 6
 4 [root@yinzhengjie ~]# echo ${yinzhengjie[@]}
 5 100 200 300 400 500 600
 6 [root@yinzhengjie ~]# 
 7 [root@yinzhengjie ~]# a=(${yinzhengjie[@]/500/5})
 8 [root@yinzhengjie ~]# echo ${a[@]}
 9 100 200 300 400 5 600
10 [root@yinzhengjie ~]# echo ${yinzhengjie[@]}
11 100 200 300 400 500 600
12 [root@yinzhengjie ~]# 
 
4>.数组的删除
 1 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
 2 4
 3 [root@yinzhengjie ~]# 
 4 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
 5 yin zheng 300 jie
 6 [root@yinzhengjie ~]# unset yinzhengjie[2]
 7 [root@yinzhengjie ~]# 
 8 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
 9 3
10 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
11 yin zheng jie
12 [root@yinzhengjie ~]# 
13 [root@yinzhengjie ~]# unset yinzhengjie
14 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
15 
16 [root@yinzhengjie ~]# 

 

5>.数组的截取
 1 [root@yinzhengjie ~]# yinzhengjie=(100 200 300 400 500 600)
 2 [root@yinzhengjie ~]# echo ${#yinzhengjie[@]}
 3 6
 4 [root@yinzhengjie ~]# echo ${yinzhengjie[@]}
 5 100 200 300 400 500 600
 6 [root@yinzhengjie ~]# echo ${yinzhengjie[@]:1:3}
 7 200 300 400
 8 [root@yinzhengjie ~]# echo ${yinzhengjie[@]:3:2}
 9 400 500
10 [root@yinzhengjie ~]# 

 

七.应用案例
1.案例一:把命令结果作为数组元素。
 1 [root@yinzhengjie shell]# yinzhengjie=($(ls))
 2 [root@yinzhengjie shell]# ls | wc -l
 3 17
 4 [root@yinzhengjie 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 [root@yinzhengjie shell]# 
23 [root@yinzhengjie 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 [root@yinzhengjie shell]# 

 

2.用法展示
 1 [root@yinzhengjie shell]# more array.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:y1053419035@qq.com
 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 [root@yinzhengjie shell]# 

 

 
 

shell函数与数组

标签:$?   unset   结果   功能   数据   必须   welcome   function   line   

原文地址:http://www.cnblogs.com/yinzhengjie/p/7684782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!