2012-10-16 71 views

回答

26

由函数返回的值存储在$?,而不是由$()抓获。

换句话说:

testFunction() 
{ 
    k=5 
    echo 3 
    return $k 
} 

val=$(testFunction) 
echo $? # prints 5 
echo $val # prints 3 
-2

这KSH功能的工作原理:

IPADDRESS()  # get the IP address of this host 
{ 
    # purpose: to get the IP address of this host 
    #  and return it as a character string 
    # 
    typeset -l IPADDR 
    IPADDR=$(ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{print $2}') 
    print $IPADDR 
} 

IP_Address=$(IPADDRESS) 
echo $IP_Address 
exit 
相关问题