Structuring a script to return value based on options. (BASH)

Let’s say you want to write a script to return a value or statement based on user input. How would you accomplish this?

Write a scrip to pass a color, return a result based on the color.

My first attempt was through the use of users selection in a particular set of available options. For instance:

#!/bin/bash
OPTIONS=”Blue Green Red”
select opt in $OPTIONS; do
    if [ “$opt” = “Blue” ]; then
      echo The sky is blue.
      elif [ “$opt” = “Green” ]; then
       echo The grass is green.
      elif [ “$opt” = “Red” ]; then
echo Roses are red.
      else
       clear
       echo Not a valid option. Please choose the following above. 
      fi

   done

 

When run this will show:
1) Blue
2) Green
3) Red 
If you input 1 and press Enter it will output:
      The sky is blue.
If you input 2 and press Enter it will output:
      The grass is green.
If you input 3 and press Enter it will output:
      Roses are red.
This is an IF ELSE statement that determines the output against the input.
Advertisements
Structuring a script to return value based on options. (BASH)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s