Sample Header Ad - 728x90

How can I take multiple inputs from a user and convert to a string that looks up values and adds them to a variable?

0 votes
1 answer
2046 views
I have a script that takes input from a user (options 1, 2, 3, etc.) to choose files to deploy to a remote server. Instead of the user running this script multiple times, I would like for them to be able to input multiple options (ex. 2,3) and as a result, those entries would add text to a variable that is then referenced in the main function. I have tried summing these and having input match the sum, however, if we have many artifacts to put in this list, option 5 would deploy one thing and options 2,3 would deploy differently, but added together would deploy the same as option 5 which is not acceptable. For instance this is what I have currently setup I am just matching the string input from the user (after dropping spaces, if any) and running that option. This is OK for only 2 or 3 artifacts, however, when the list gets long the choices get exponentially greater. #!/bin/sh export alltags="all" export tag1="artifact1" export tag2="artifact2" export tag3="artifact3" deployment_tag="" function updateArtifacts { cp -r /path/to/artifact/artifact.zip --tags "\"${deployment_tag}"\" } echo "Enter the number of the artifacts you would like to deploy" "1. artifact1" "2. artifact2" "3. artifact3" read -p " " num trimNum=echo $num | sed 's/ //g' if [ "$trimNum" == "1" ]; then $deployment_tag+="$alltags" echo "Updating all artifacts" updateArtifacts elif [ "$trimNum" == "2" ]; then $deployment_tag+="$tag1" echo "Updating artifact 1" updateArtifacts elif [ "$trimNum" == "2,3" ]; then $deployment_tag+="$tag1,$tag2" echo "Updating artifact 1 and 2" updateArtifacts else echo "aborted, please enter a valid selection" fi I know I am missing options, but this is just to give a brief example. I know this is long winded, I appreciate any input.
Asked by oakenshield (3 rep)
Jul 1, 2021, 02:22 PM
Last activity: Jul 2, 2021, 04:53 AM