shell 执行字符串命令&处理参数&函数

1. 执行字符串命令

cmd="echo 'hello world"
$cmd

2. 处理参数

help_msg()
{
    echo "Usage: `basename $0` -c  -p  -d/-e -r [result]"
}

while getopts 'p:c:r:de' OPT; do
    case $OPT in
    c)
        test_config_file="$OPTARG";;
    p)
        file_path="$OPTARG";;
    r)
        result="$OPTARG";;
    d)
        de=true;;
    e)
        en=true;;
    ?)
        help_msg
        exit 1;;
    esac
done

if (( $OPTIND == 1 )); then
    echo "No argument"
    help_msg
    exit 1
fi

3. 函数

a. 本地变量 - 使用local ,如果不指明为local,即可gloable访问

b. 传参 - 使用$1, $2 等表明参数

test()
{
    local var1=$1
    local var2=$2
    echo "$var1 $var2"
}

test "variable 1" "variable 2"

你可能感兴趣的:(bash,开发语言)