0

I'm trying to create a bash command in which you enter a number between 1 and 12 and it is using the switch command but in the bash shell the case command. I just need to make sure I have the right idea going for this or if i'm completley off

#!/bin/bash
1="January"
2="February"
3="March"
4="April"
5="May"
6="June"
7="July"
8="August"
9="September"
10="October"
11="November"
12="December"
case "1" in
print "$1"
case "2" in
print "$2" 
case "3" in
print "$3"
case "4" in
print "$4"
case "5" in
print "$5"
case "6" in
print "$6"
case "7" in
print "$7"
case "8" in
print "$8"
case "9" in
print "$9"
case "10" in
print "$10"
case "11" in
print "$11"
case "12" in 
print "$12"
echo "Please enter a number 1 through 12"
read input_variable
echo "$input_variable"
7
  • 1
    1=January isn't a valid variable assignment, either. If you wanted to set $1 through $12, you'd run: set -- January February March April May June July August September October November December. Commented Apr 1, 2014 at 16:18
  • 2
    ...but, since you're using bash, the better answer is an array: declare -a months=( January February ...etc... ); read -r input_variable; echo "${months[$(( input_variable - 1))}". A case statement is overkill for this use. Commented Apr 1, 2014 at 16:19
  • @CharlesDuffy I thought the same - I have added an array-based example to my answer. Commented Apr 1, 2014 at 16:24
  • @CharlesDuffy Or even echo "${months[input_variable-1]}" Commented Apr 1, 2014 at 16:26
  • @devnull very nice, I hope you don't mind me using it in my answer. Commented Apr 1, 2014 at 16:28

2 Answers 2

3

I'm afraid to say you are a bit off. Here's something a bit more like it:

#!/bin/bash

echo "Please enter a number 1 through 12"
read input_variable

case $input_variable in
    1) 
        echo "January"
        ;;
    2) 
        echo "February"
        ;;
    3) 
        echo "March"
        ;;
    4) 
        echo "April"
        ;;
  # 5), 6) etc.
    *) # anything else
        echo "not recognised" 
        ;;
esac

@Charles Duffy is right, you don't actually need a case for this. You could instead use an array like this:

edit: as I write this I see an improvement from @devnull!

#!/bin/bash

echo "Please enter a number 1 through 12"
read input

months=( January February March April May June July August September October November December )

[ $input -gt 0 -a $input -le 12 ] && echo "${months[input-1]}"

or if you prefer, using a dummy entry at the start of the array (thanks @chepner)

months=( "" January February March April May June July August September October November December )

[ ! -z "${months[input]}" ] && echo "${months[input]}"
Sign up to request clarification or add additional context in comments.

3 Comments

Had a feeling i was pretty far off but thank you for the responce gonna give it a try
As a cosmetic variation, you can add a dummy element to the beginning of the array to server as Month 0; then you don't need to subtract one from the input to use it as an index.
@chepner thanks, I've added it as another alternative
0

use:

read -p "Enter a number between 1 and 12: " input

as it simplifies code. Then use:

case $input in 

...etc as Tom Fenech stated

But yeah case statements are more for testing against the same type of result, but performing different actions with it based on what the result turns out to be. You are echoing everything here, so yeah store the strings in an array and just output it based on the input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.