我尝试打印testfunction返回的值,但是没有显示任何内容。我使用
./filename.sh
来执行脚本,请帮忙。#!/bin/ksh
testfunction()
{
k=5
return $k
}
val=$(testfunction)
echo $val
该函数返回的值存储在$?
中,不会被$()
捕获。
换句话说:
testFunction()
{
k=5
echo 3
return $k
}
val=$(testFunction)
echo $? # prints 5
echo $val # prints 3
val=$(testFunction)
这个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