Linux Shell函式返回值 .
阿新 • • 發佈:2019-02-01
Shell函式返回值,一般有3種方式:return,argv,echo
1) return 語句shell函式的返回值,可以和其他語言的返回值一樣,通過return語句返回。
示例:
[javascript] view plaincopyprint?
- #!/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
- 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
- iffalse; 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"
#!/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"
結果:
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
獲取函式的返回值通過呼叫函式,或者最後執行的值獲得。
另外,可以直接用函式的返回值用作if的判斷。
注意:return只能用來返回整數值,且和c的區別是返回為正確,其他的值為錯誤。
2) argv全域性變數
這種就類似於C語言中的全域性變數(或環境變數)。
示例:
[javascript] view plaincopyprint?- #!/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"
#!/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"
結果:
mytest2
args 1
return 0
g_var=1
函式mytest2通過修改全域性變數的值,來返回結果。
注: 以上兩個方法失效的時候
以上介紹的這兩種方法在一般情況下都是好使的,但也有例外。示例:
[javascript] view plaincopyprint?
- #!/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"
#!/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 檔案中的內容如下:
456:kkk123:yxb
123:test
結果:
yxbmytest3 here
1
yxb
g_var=0
mytest4 here
1
g_var=
可以看到mytest3在return了以後其實沒有直接返回,而是執行了迴圈體後的語句,同時看到mytest4中也是一樣,同時,在mytest4中,對全域性變數的修改也無濟於事,全域性變數的值根本就沒有改變。這個是什麼原因那?
筆者認為,之所以return語句沒有直接返回,是因為return語句是在管道中執行的,管道其實是另一個子程序,而return只是從子程序中返回而已,只是while語句結束了。而函式體之後的語句會繼續執行。
同理,全域性變數在子程序中進行了修改,但是子程序的修改沒有辦法反應到父程序中,全域性變數只是作為一個環境變數傳入子程序,子程序修改自己的環境變數,不會影響到父程序。
因此在寫shell函式的時候,用到管道(cmd &後臺程序也一樣)的時候一定要清楚此刻是從什麼地方返回。
3) echo 返回值
其實在shell中,函式的返回值有一個非常安全的返回方式,即通過輸出到標準輸出返回。因為子程序會繼承父程序的標準輸出,因此,子程序的輸出也就直接反應到父程序。因此不存在上面提到的由於管道導致返回值失效的情況。在外邊只需要獲取函式的返回值即可。
示例:
[javascript] view plaincopyprint?- #!/bin/bash
- ##############################################
- # Author : IT-Homer
- # Date : 2012-09-06
- # Blog : http://blog.csdn.net/sunboy_2050
- ##############################################
- function mytest5()
- {
- grep "123" test.txt | awk -F: '{print $2}' | while read line; do
- if [ $line = "yxb" ]; then
- echo "0" # value returned first by thisfunction
- 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
#!/bin/bash
##############################################
# Author : IT-Homer
# Date : 2012-09-06
# Blog : http://blog.csdn.net/sunboy_2050
##############################################
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
結果:$? = 0
result = 0
have yxb, result is 0
這個方式雖然好使,但是有一點一定要注意,不能向標準輸出一些不是結果的東西,比如除錯資訊,這些資訊可以重定向到一個檔案中解決,特別要注意的是,用到比如grep這樣的命令的時候,一定要記得1>/dev/null 2>&1來避免這些命令的輸出。