SuperMan-Lfj / blog

Apache License 2.0
1 stars 0 forks source link

shell 的一些解决方法 #64

Open SuperMan-Lfj opened 1 year ago

SuperMan-Lfj commented 1 year ago

函数返回字符串

shell的函数默认是只能返回numric类型,数字类型。如果想返回字符串,则可以把函数当作命令去调用:

# 函数定义 detect platform, no params
function func_utils_platform_name() {
    platform="win"
    uname_result=`uname`
    if [ "$uname_result" = "Linux" ]; then
        platform="linux"
    elif [ "$uname_result" = "Darwin" ]; then
        platform="mac"
    fi
    echo $platform   #通过echo将值输出
}

# 函数调用
platform=`func_utils_platform_name` # 通过命令调用接收echo的值
if [ "$platform" = "win" ]; then
   # other codes
fi