linux - shell脚本中的命令返回

标签 linux sh openwrt

我在 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/

相关文章:

linux - umount -l 后删除挂载目录

python - 设计多进程守护进程

linux - 我不清楚一个重定向运算符,那就是 <<

linux - 如何重命名共享库以避免同名冲突?

linux - 在创建 tar 文件时,整个路径被创建为 tar 文件

bash - shell 更新后在 .bashrc 中找不到 shopt 命令

linux - 如何用mjpg_streamer拍照

linux - 多次调用后 `mprotect` 失败

jquery - jq 命令检查 json 数组响应是否具有使用 shell 脚本批准的特定用户名和状态

linux - mipsel-linux-uclibc/bin/ld : cannot find -lcurl