语法:
[ function ] funname [()]
{
action;
[return int;]
}
说明:
1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255)
实例(fun_sum_and_call.sh):
#!/bin/sh
#fSum 3 2;
function fSum()
{
echo $1,$2;
return $(($1+$2));
}
fSum 5 7;
total=$(fSum 3 2);
echo $total,$?;
上面这个例子可以得到几点结论:
1、必须在调用函数地方之前,声明函数,shell脚本是逐行运行;不会像其它语言一样先预编译。
2、total=$(fSum 3 2); 通过这种调用方法,我们清楚知道,在shell中单括号里面,可以是命令语句。 因此,我们可以将shell中函数,看作是定义一个新的命令,它是命令,因此 各个输入参数直接用空格分隔。 一次命令里面获得参数方法可以通过:$0…$n得到。 $0代表函数本身。
3、函数返回值,只能通过$? 系统变量获得,直接通过=,获得是空值。其实,我们按照上面一条理解,知道函数是一个命令,在shell获得命令返回值,都需要通过$?获得。
样例:
calculator.fc
[hs@master public-function]$ cat calculator.fc
#!/bin/bash
usage="Usage: `basename $0` (add|sub|mul|div|all) parameter1 parameter2"
command=$1
first=$2
second=$3
function add() {
ans=$(($first + $second))
echo $ans
}
function sub() {
ans=$(($first - $second))
echo $ans
}
function mul() {
ans=$(($first * $second))
echo $ans
}
function div() {
ans=$(($first / $second))
echo $ans
}
case $command in
(add)
add
;;
(sub)
sub
;;
(mul)
mul
;;
(div)
div
;;
(all)
add
sub
mul
div
;;
(*)
echo "Error command"
echo "$usage"
;;
esac
getsrcinfo.fc
[hs@master public-function]$ cat getsrcinfo.fc
#!/bin/bash
#usage and parameter definition ...
usage="Usage: `basename $0` (dbname|dbip|rdstabname|loadtype|user|pass|all) tabname"
command=$1
first=$2
#function info ...
function dbname() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/batch_table_userpass.list|grep ^$first\| |awk -F "|" 'NR==1 {print $2}')
echo $result
}
function dbip() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/batch_table_userpass.list|grep ^$first\| |awk -F "|" 'NR==1 {print $3}')
echo $result
}
function rdstabname() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/batch_table_userpass.list|grep ^$first\| |awk -F "|" 'NR==1 {print $4}')
echo $result
}
function loadtype() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/batch_table_userpass.list|grep ^$first\| |awk -F "|" 'NR==1 {print $5}')
echo $result
}
function user() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/batch_table_userpass.list|grep ^$first\| |awk -F "|" 'NR==1 {print $6}')
echo $result
}
function pass() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/batch_table_userpass.list|grep ^$first\| |awk -F "|" 'NR==1 {print $7}')
echo $result
}
#function call ...
case $command in
(dbname)
dbname
;;
(dbip)
dbip
;;
(rdstabname)
rdstabname
;;
(loadtype)
loadtype
;;
(user)
user
;;
(pass)
pass
;;
(all)
dbname
dbip
rdstabname
loadtype
user
pass
;;
(*)
echo "Error command"
echo "$usage"
;;
esac
getdwinfo.fc
[hs@master public-function]$ cat getdwinfo.fc
#!/bin/bash
#usage and parameter definition ...
usage="Usage: `basename $0` (dbip|user|pass|all) dbname"
command=$1
first=$2
#function info ...
function dbip() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/dw_dbuserpass_info.list|grep $first\$ |awk -F "|" 'NR==1 {print $1}')
echo $result
}
function user() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/dw_dbuserpass_info.list|grep $first\$ |awk -F "|" 'NR==1 {print $2}')
echo $result
}
function pass() {
result=$(cat /home/hs/opt/dw-etl/script_generate/script_dir/list_dir/dw_dbuserpass_info.list|grep $first\$ |awk -F "|" 'NR==1 {print $3}')
echo $result
}
#function call ...
case $command in
(dbip)
dbip
;;
(user)
user
;;
(pass)
pass
;;
(all)
dbip
user
pass
;;
(*)
echo "Error command"
echo "$usage"
;;
esac
其中后面两个示例中所用到的文件内容如下:
/home/hs/opt/dw-etl/script_generate/script_dir/list_dir/dw_dbuserpass_info.list
rdsip.mysql.rds.aliyuncs.com|datauser|passwordchard|sor
rdsip.mysql.rds.aliyuncs.com|datauser|passwordchard|qrt
/home/hs/opt/dw-etl/script_generate/script_dir/list_dir/batch_table_userpass.list
x_shop_repo|db99shop|rr-ip.mysql.rds.aliyuncs.com|h_x_shop_repo|total|dbreader|passwdchar
x_shop_site|db99shop|rr-ip.mysql.rds.aliyuncs.com|h_x_shop_site|total|dbreader|passwdchar