1

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

2) Value of <platform> input param is not present in the list i.e. it is other than 1234, 1235 or 1236.

3) Any other feature apart from <feature1>, <feature2>, <feature3> are provided as input.

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.

Yash
  • 2,944
  • 7
  • 25
  • 43
  • For starters test your script here https://www.shellcheck.net/ – Digvijay S Jun 16 '20 at 11:33
  • Older discussion: [Script parameters in Bash](https://stackoverflow.com/questions/18003370/script-parameters-in-bash) | [How do I parse command line arguments in Bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) – Digvijay S Jun 16 '20 at 11:36
  • Does this answer your question? [How do I parse command line arguments in Bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) – Digvijay S Jun 16 '20 at 11:37
  • Thanks @Digvijay. The links provided by you are really helpful. – Yash Jun 17 '20 at 06:45

2 Answers2

3

The case statement will simplify your code. 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.

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks @chepner, Thanks for your solution. I like your approach to the problem and its a good starting point for me. One thing i missed to mention is that each and every export is different, so I cannot generalize as you have done `export PLATFORM=9$platform`. Also for every case there are more than one exports. But as i said your solution is a good starting point for me. Thanks again! – Yash Jun 17 '20 at 06:49
  • Thanks chepner, i was able to solve my problem. I have accepted your answer! – Yash Jun 18 '20 at 16:25
0
  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
Digvijay S
  • 2,665
  • 1
  • 9
  • 21