シェルは文字列コマンドを実行し、パラメータと関数を処理します

1. 文字列コマンドを実行する

cmd="echo 'hello world"
$cmd

2. 処理パラメータ

help_msg()
{
    echo "Usage: `basename $0` -c <config> -p <path> -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. ローカル変数 - ローカルを使用します。ローカルとして指定されていない場合は、gloable でアクセスできます。

b. パラメータを渡す - $1、$2 などを使用してパラメータを指定します

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

test "variable 1" "variable 2"

おすすめ

転載: blog.csdn.net/iamanda/article/details/122239235