這篇文章將為大家詳細(xì)講解有關(guān)Linux 中怎么使用Shell函數(shù)返回值,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

1) return 語句
shell函數(shù)的返回值,可以和其他語言的返回值一樣,通過return語句返回。
示例:
#!/bin/bash -
function mytest()
{
echo "arg1 = $1"
if [ $1 = "1" ] ;then
return 1
else
return 0
fi
}
echo
echo "mytest 1"
mytest 1
echo $? # print return result
echo
echo "mytest 0"
mytest 0
echo $? # print return result
echo
echo "mytest 2"
mytest 2
echo $? # print return result
echo
echo "mytest 1 = "`mytest 1`
if mytest 1 ; then
echo "mytest 1"
fi
echo
echo "mytest 0 = "`mytest 0`
if mytest 0 ; then
echo "mytest 0"
fi
echo
echo "if fasle" # if 0 is error
if false; then
echo "mytest 0"
fi
echo
mytest 1
res=`echo $?` # get return result
if [ $res = "1" ]; then
echo "mytest 1"
fi
echo
mytest 0
res=`echo $?` # get return result
if [ $res = "0" ]; then
echo "mytest 0"
fi
echo
echo "end"結(jié)果:
mytest 1
arg1 = 1
1
mytest 0
arg1 = 0
0
mytest 2
arg1 = 2
0
mytest 1 = arg1 = 1
arg1 = 1
mytest 0 = arg1 = 0
arg1 = 0
mytest 0
if fasle
arg1 = 1
mytest 1
arg1 = 0
mytest 0
end
先定義了一個函數(shù)mytest,根據(jù)它輸入的參數(shù)是否為1來return 1或者return 0.
獲取函數(shù)的返回值通過調(diào)用函數(shù),或者最后執(zhí)行的值獲得。
另外,可以直接用函數(shù)的返回值用作if的判斷。
注意:return只能用來返回整數(shù)值,且和c的區(qū)別是返回為正確,其他的值為錯誤。
2) argv全局變量
這種就類似于C語言中的全局變量(或環(huán)境變量)。
示例:
#!/bin/bash -
g_var=
function mytest2()
{
echo "mytest2"
echo "args $1"
g_var=$1
return 0
}
mytest2 1
echo "return $?"
echo
echo "g_var=$g_var"結(jié)果:
mytest2
args 1
return 0
g_var=1
函數(shù)mytest2通過修改全局變量的值,來返回結(jié)果。
注: 以上兩個方法失效的時候
以上介紹的這兩種方法在一般情況下都是好使的,但也有例外。
示例:
#!/bin/bash -
function mytest3()
{
grep "123" test.txt | awk -F: '{print $2}' | while read line ;do
echo "$line"
if [ $line = "yxb" ]; then
return 0 # return to pipe only
fi
done
echo "mytest3 here "
return 1 # return to main process
}
g_var=
function mytest4()
{
grep "123" test.txt | awk -F: '{print $2}' | while read line ;do
echo "$line"
if [ $line = "yxb" ]; then
g_var=0
echo "g_var=0"
return 0 # return to pipe only
fi
done
echo "mytest4 here "
return 1
}
mytest3
echo $?
echo
mytest4
echo $?
echo
echo "g_var=$g_var"其中,test.txt 文件中的內(nèi)容如下:
456:kkk
123:yxb
123:test
結(jié)果:
yxb
mytest3 here
1
yxb
g_var=0
mytest4 here
1
g_var=
可以看到mytest3在return了以后其實沒有直接返回,而是執(zhí)行了循環(huán)體后的語句,同時看到mytest4中也是一樣,同時,在mytest4中,對全局變量的修改也無濟(jì)于事,全局變量的值根本就沒有改變。這個是什么原因那?
筆者認(rèn)為,之所以return語句沒有直接返回,是因為return語句是在管道中執(zhí)行的,管道其實是另一個子進(jìn)程,而return只是從子進(jìn)程中返回而已,只是while語句結(jié)束了。而函數(shù)體之后的語句會繼續(xù)執(zhí)行。
同理,全局變量在子進(jìn)程中進(jìn)行了修改,但是子進(jìn)程的修改沒有辦法反應(yīng)到父進(jìn)程中,全局變量只是作為一個環(huán)境變量傳入子進(jìn)程,子進(jìn)程修改自己的環(huán)境變量,不會影響到父進(jìn)程。
因此在寫shell函數(shù)的時候,用到管道(cmd &后臺進(jìn)程也一樣)的時候一定要清楚此刻是從什么地方返回。
3) echo 返回值
其實在shell中,函數(shù)的返回值有一個非常安全的返回方式,即通過輸出到標(biāo)準(zhǔn)輸出返回。因為子進(jìn)程會繼承父進(jìn)程的標(biāo)準(zhǔn)輸出,因此,子進(jìn)程的輸出也就直接反應(yīng)到父進(jìn)程。因此不存在上面提到的由于管道導(dǎo)致返回值失效的情況。
在外邊只需要獲取函數(shù)的返回值即可。
示例:
#!/bin/bash
##############################################
# Author : IT-Homer
# Date : 2012-09-06
# Blog : /tupian/20230522/ithomer
##############################################
function mytest5()
{
grep "123" test.txt | awk -F: '{print $2}' | while read line; do
if [ $line = "yxb" ]; then
echo "0" # value returned first by this function
return 0
fi
done
return 1
}
echo '$? = '"$?"
result=$(mytest5)
echo "result = $result"
echo
if [ -z $result ] # string is null
then
echo "no yxb. result is empyt"
else
echo "have yxb, result is $result"
fi結(jié)果:
$? = 0
result = 0
have yxb, result is 0
這個方式雖然好使,但是有一點(diǎn)一定要注意,不能向標(biāo)準(zhǔn)輸出一些不是結(jié)果的東西,比如調(diào)試信息,這些信息可以重定向到一個文件中解決,特別要注意的是,用到比如grep這樣的命令的時候,一定要記得1>/dev/null 2>&1來避免這些命令的輸出。
關(guān)于Linux 中怎么使用Shell函數(shù)返回值就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
新聞名稱:Linux中怎么使用Shell函數(shù)返回值-創(chuàng)新互聯(lián)
本文來源:http://www.chinadenli.net/article18/ccscgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站策劃、服務(wù)器托管、網(wǎng)站維護(hù)、網(wǎng)站改版、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容