調用系統函數庫

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章