简体   繁体   English

检查作为 shell 脚本输入提供的可变数量位置参数的有效性

[英]Check the validity of variable number of positional parameters provided as input to a shell script

For compiling a set of applications for multiple platforms, there are certain build exports which need to be set.对于为多个平台编译一组应用程序,需要设置某些构建导出。

The sample command is as under:示例命令如下:

source exports_file.sh <plaforms> <feature1> <feature2> <feature3>

Here the positional parameter <platform> is mandatory whereas the other parameters such as <feature1>, <feature2>, <feature3> are optional.这里位置参数<platform>是必需的,而其他参数,例如<feature1>, <feature2>, <feature3>是可选的。 These have to be enabled only if the corresponding feature is needed in the build.只有在构建中需要相应的功能时,才必须启用这些功能。

The set of valid command line options are:一组有效的命令行选项是:

source exports_file.sh <plaforms> <feature1> <feature2> <feature3> source exports_file.sh <plaforms> <feature1> <feature2> source exports_file.sh <plaforms> <feature1> source exports_file.sh <plaforms>

Important thing to note is that the script should return an error if:需要注意的重要一点是,如果出现以下情况,脚本应该返回错误:

1) <platform> input param is not provided by the user. 1) <platform>输入参数不是由用户提供的。

2) Value of <platform> input param is not present in the list ie it is other than 1234, 1235 or 1236. 2) <platform>输入参数的值不在列表中,即它不是 1234、1235 或 1236。

3) Any other feature apart from <feature1>, <feature2>, <feature3> are provided as input. 3) 除了<feature1>, <feature2>, <feature3>之外的任何其他特征都作为输入提供。

I have written a script which works fine but I'm not sure if its checking the validity of all the input parameters correctly.我编写了一个运行良好的脚本,但我不确定它是否正确检查所有输入参数的有效性。

   $> cat exports_file.sh


    if [ $# -gt 0 ]; then

    platform=$1

    # Common exports
    unset PLATFORM
    unset ENABLE_FEATURE_1
    unset ENABLE_FEATURE_2
    unset ENABLE_FEATURE_3

    echo "INFO: Setting common exports for $platform"
    if [ $platform == "1234" ]
    then
        export PLATFORM=91234

    elif [ $platform == "1235" ]
    then
        export PLATFORM=91235

    elif [ $platform == "1236" ]
    then
        export PLATFORM=91236

    else
        echo "ERROR: Exports are not defined for $platform."
        exit 1
    fi

    # Check for feature based exports <feature1> <feature2> <feature3>
    for var in "$@"
    do
        if [ $var == "arg2" ]
        then
            export ENABLE_FEATURE_1=Y

        elif [ $var == "arg3" ]
        then
            export ENABLE_FEATURE_2=Y

        elif [ $var == "arg4" ]
        then
            export ENABLE_FEATURE_3=Y
        else
            echo "ERROR: unrecognised argument '$var'";
            exit 1
        fi
    done
else
    echo "ERROR: No inputs parameters provided to the scripts."
    echo "Usage: source exports_file.sh <plaforms> <feature1> <feature2> <feature3>"
fi`

Is there a better way to write this script.有没有更好的方法来编写这个脚本。 Most important thing is to ensure the validity of all the input parameters.最重要的是保证所有输入参数的有效性。

The case statement will simplify your code. case语句将简化您的代码。 First, though, exit as soon as possible when you find an error, so that the rest of you code doesn't need to be indented.不过,首先,当你发现错误时尽快退出,这样你代码的 rest 就不需要缩进了。

if [ $# = 0 ]; then
    echo "ERROR: No inputs parameters provided to the scripts." >&2
    echo "Usage: source exports_file.sh <plaforms> <feature1> <feature2> <feature3>" >&2
    exit 1
fi

platform=$1
shift

echo "INFO: Setting common exports for $platform" >&2
case $platform in 
    1234|1235|1236) export PLATFORM=9$platform ;;
    *) echo "ERROR: Exports are not defined for $platform" >&2
       exit 1 ;;
esac


# Check for feature based exports <feature1> <feature2> <feature3>
for var in "$@"
do
    case $var in
      arg2) export ENABLE_FEATURE_1=Y ;;
      arg3) export ENABLE_FEATURE_2=Y ;;
      arg4) export ENABLE_FEATURE_3=Y ;;
      *) echo "ERROR: unrecognized argument '$var'" >&2
         exit 1;;
    esac
done
  if [ $# -gt 0 ]; then

    platform=$1

    # Common exports
    unset PLATFORM
    unset ENABLE_FEATURE_1
    unset ENABLE_FEATURE_2
    unset ENABLE_FEATURE_3
    echo  input parameter: $@

    echo "INFO: Setting common exports for $platform"
    if [ $platform == "1234" ]
    then
        export PLATFORM=91234
        export ENABLE_FEATURE_1=Y
        # Result
        echo $PLATFORM
        echo $ENABLE_FEATURE_1

    elif [ $platform == "1235" ]
    then
        export PLATFORM=91235
        export ENABLE_FEATURE_2=Y
        # Result
        echo $PLATFORM
        echo $ENABLE_FEATURE_2

    elif [ $platform == "1236" ]
    then
        export PLATFORM=91236
        export ENABLE_FEATURE_3=Y
        # Result
        echo $PLATFORM
        echo $ENABLE_FEATURE_3

    else
       echo "ERROR: Exports are not defined for $platform."
        exit 1
    fi
    fi

    # Check for feature based exports <feature1> <feature2> <feature3>
    #for var in "$@"
    #do
        #if [ $var == "arg2" ]
        #then
            #export ENABLE_FEATURE_1=Y

        #elif [ $var == "arg3" ]
        #then
            #export ENABLE_FEATURE_2=Y

        #elif [ $var == "arg4" ]
        #then
            #export ENABLE_FEATURE_3=Y
        #else
            #echo "ERROR: unrecognised argument '$var'";
            #exit 1
        #fi
    #done
#else
   #echo "ERROR: No inputs parameters provided to the scripts."
    #echo "Usage: source exports_file.sh <plaforms> <feature1> <feature2> <feature3>"
#fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM