shell action函数
action() {
local STRING rc
STRING=$1
echo -n "$STRING "
shift
"$@" && success $"$STRING" || failure $"$STRING"
rc=$?
echo
return $rc
}
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
\$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
#!/bin/bash
. /etc/init.d/functions #通过这个脚本声明函数
hehe(){
rs=0
[ $rs -eq 0 ] && action "Ipvsadm stoped." /bin/true
}
hehe
执行
[root@master2 html]# sh test.sh
Ipvsadm stoped. [ OK ]
改成false
[root@master2 html]# sh test.sh
Ipvsadm stoped. [FAILED]