日期:2014-05-16  浏览次数:20683 次

Unix shell自定义函数的简介及使用

一、无参函数

没有参数的函数,直接调用实现某些功能。

函数编写在脚本中,与其他命令一起存储,但是函数必须定义在脚本的最开始部分;

也就是说,包含函数的脚本中,所有的函数都得定义在脚本的最开始部分;

然后在定义函数之后调用或者在其他脚本中引用这些定义的函数。

实例1、下面是一个简单的自定义函数,求1到10的和:

pg no_param_test
#!/bin/ksh
# 测试无参自定义函数
# author:_yeeXun
# date  :2013-3-4 8:37:29

no_param_test() {
SUM=0
#for i in { 1..10 }
for i in 1 2 3 4 5 6 7 8 9 10
do
  echo $i
  SUM=`expr $SUM + $i`
  i=`expr $i + 1`
  if [ $i -eq 11 ]; then
    echo "Sum:$SUM"
  fi
done
}
no_param_test
# EOF
执行脚本:
sh no_param_test
1
2
3
4
5
6
7
8
9
10
Sum:55

二、有参函数

带参数的自定义函数,在高级语言(如,C,JAVA,C#等)中,参数都是在函数后面的括号中;

而shell中则在函数体内检查参数个数。在调用函数的时候,直接在后面跟着参数即可。

无论参数有多少个,都紧跟在函数名后面。

1、单个参数

实例2、脚本如下:

pg direc_check
#!/bin/ksh

is_directory() {
_DIR_NAME=$1
# check params
if [ $# -lt 1 ]; then
  echo "is_directory:I need a directory name to check"
  return 1
fi

# check
if [ ! -d $_DIR_NAME ]; then
  return 1
else
  return 0
fi
}

error_msg() {
echo "**********"
echo $@
echo "**********"
  return 0
}

echo -n "Enter destination directory:"
read DIR
if is_directory $DIR
then 
  echo "$DIR is a directory and exists in $PWD"
else
  error_msg "$DIR does not exist ... creating it now"
  mkdir $DIR > /dev/null 2>&1
  if [ $? -ne 0 ]; then
    error_msg "Could not create directory::chech it out!"
    exit 1
  else :
  fi
fi

# author:_yeeXun
# date  :2013-3-2 15:27:57
# EOF

这里函数中# check params部分就是在做参数检查,这里$#表示输入的参数个数。

下面是执行这个脚本:

sh direc_check
Enter destination directory:test
**********
test does not exist ... creating it now
**********
再次执行时候,输入同样的名称,则会提示此目录已经存在。
sh direc_check
Enter destination directory:test
test is a directory and exists in /usr/b4nx/user/ytcclb

我们在添加一个目录,通过awk语言查看当前目录下通过此脚本创建的目录:

ls -l | awk '{if($0~/^d/) print $0}'
drwxr-xr-x   2 b4nx     group        512 Mar  2 15:32 call_fun_test
drwxr-xr-x   2 b4nx     group        512 Mar  2 15:04 test

2、多个参数

不管是单个参数,还是多个参数,都通过检查判断,这里$#表示输入的参数个数;

$1表示输入的第一个参数,$2表示第二个,$3表示第四个,以此类推。

下面是一个截取字符串的函数:

实例3、pg chop

#!/bin/ksh
# chop
# to call:chop string how_many_chars_to_chop
chop() {
_STR=$1
_CHOP=$2

# awk's substr starts at 0,wo need to increment by one
CHOP=`expr $_CHOP + 1`

# check we have two params
if [ $# -ne 2 ]; then
  echo "check_length:I need a string and how many characters to chop"
  return 1
fi

# check the length of the string first
_LENGTH=`echo $_STR | awk '{print length($0)}'`

if [ "$_LENGTH" -lt "$_CHOP" ]; then
  echo "The length of the string is short"
  return 1
fi

echo $_STR | awk '{print substr($1,'$_CHOP')}'
}

# call the function
echo -n "Enter the string:"
read STR
echo -n "Enter the start position:"
read LEN
chop $STR $LEN

# author:_yeeXun
# date  :2013-3-2 14:17:34
# EOF

这个脚本中,定义了一个函数chop截取字符串,2个参数,分别用$1,$2表示;

$1表示被截取的字符串,$2表示截取的起始位置。执行如下:

sh chop
Enter the string:string123
Enter the start position:5
ng123

sh chop
Enter the string:_yeexunInCSDN
Enter the start position:10
CSDN

三、函数返回值

函数的返回值表示执行函数的一个状态,

1、return 0:正常返回。

2、return 1:返回错误。

3、return  :最后的命令执行状态决定返回值,可使用$?检测。


四、函数调用
1、同脚本

脚本中,函数必须在脚本一开始就定义,定义之后才可调用。

同一脚本中,在顶部定义函数,定义完后再调用,如实例1、实例2、实例3。

2、脚本间

可以将这些函数定义在同一个脚本中,作为公共使用的函数脚本,然后再需要调用的脚本中引用此脚本即可。

引用方式如下:<dot><space><directory><script>

实例4、函数调用 — 跨脚本

函数脚