source的威力
[root@moban scripts]# cat test.sh echo $OLDLANNY [root@moban scripts]# cat zhixing.sh source /server/scripts/test.sh #调用 echo $OLDLANNY [root@moban scripts]# sh zhixing.sh 724 724
对比sh执行方式:
[root@moban scripts]# cat test.sh user='lanny' [root@moban scripts]# cat sys_diaoyong.sh /bin/sh test.sh echo $user 执行无任何结果: [root@moban scripts]# sh sys_diaoyong.sh [root@moban scripts]#
调用系统函数库:
. /etc/init.d/functions 例: [root@moban scripts]# cat sys_func.sh source /etc/init.d/functions #调用系统函数库 action "lanny is a IT engineer" /bin/true [root@moban scripts]# sh sys_func.sh lanny is a IT engineer [ OK ]
自定义
cat /etc/init.d/functions #by lanny oldlanny(){ echo "this is oldlanny" } [root@moban scripts]# cat sys_diaoyong.sh source /etc/init.d/functions oldlanny [root@moban scripts]# sh sys_diaoyong.sh this is oldlanny
传参
cat /etc/init.d/functions #by lanny oldlanny(){ echo "this is $1" #改成参数 } [root@moban scripts]# cat sys_diaoyong.sh source /etc/init.d/functions oldlanny 'lanny' [root@moban scripts]# sh sys_diaoyong.sh this is lanny
优化输入
[root@moban scripts]# cat sys_diaoyong.sh source /etc/init.d/functions oldlanny $1 [root@moban scripts]# sh sys_diaoyong.sh ma this is ma