我在 OpenWrt 中开发了一个脚本 shell,如下所示,我注意到一个奇怪的行为(“返回”),如脚本末尾的注释所示。它返回 43 而不是 9003,返回 45 而不是 9005,您对此行为有什么解释吗?
#!/bin/sh
get_value() {
case "$1" in
aa)
if [ "$2" = "1" ]; then
return 9003
fi
# function fn1 to execute
fn1
return 0
;;
bb)
if [ "$2" = "1" ]; then
return 9003
fi
# function fn2 to execute
fn2
return 0
;;
cc)
if [ "$2" = "1" ]; then
return 9003
fi
# function fn3 to execute
fn3
return 0
;;
esac
return 9005
}
# when a call get_value
get_value aa 0
echo $?
# result 0
get_value aa 1
echo $?
# result 43 (not 9003)
get_value dd 1
echo $?
# result 45 (not 9005)
最佳答案
在大多数 Unix 系统上,返回值被解释为模 256。由于 9003 mod 256 是 43,而 9005 mod 256 等于 45,因此您将分别获得这些返回值。
关于linux - shell脚本中的命令返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13386321/