Shell executes string commands & processes parameters & functions

1. Execute string commands

cmd="echo 'hello world"
$cmd

2. Processing parameters

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. Functions

a. Local variable - use local, if it is not specified as local, it can be accessed by gloable

b. Pass parameters - use $1, $2, etc. to indicate parameters

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

test "variable 1" "variable 2"

Guess you like

Origin blog.csdn.net/iamanda/article/details/122239235