1

I have script here that will list the date that the user will enter and output the date 5 days ago.

#!/bin/bash
echo "What month?"
            echo "1 - January"
            echo "2 - February"
            echo "3 - March"
            echo "4 - April"
            echo "5 - May"
            echo "6 - June"
            echo "7 - July"
            echo "8 - August"
            echo "9 - September"
            echo "10 - October"
            echo "11 - November"
            echo "12 - December"
            echo ""
            echo -n "What month?  "
            read m

            if [ "$m" == "1" ]
            then
            mn="Jan "
            elif [ "$m" == "2" ]
            then
            mn="Feb "
            elif [ "$m" == "3" ]
            then
            mn="Mar "
            elif [ "$m" == "4" ]
            then
            mn="Apr "
            elif [ "$m" == "5" ]
            then
            mn="May "
            elif [ "$m" == "6" ]
            then
            mn="Jun "
            elif [ "$m" == "7" ]
            then
            mn="Jul "
            elif [ "$m" == "8" ]
            then
            mn="Aug "
            elif [ "$m" == "9" ]
            then
            mn="Sep "
            elif [ "$m" == "10" ]
            then
            mn="Oct "
            elif [ "$m" == "11" ]
            then
            mn="Nov "
            elif [ "$m" == "12" ]
            then
            mn="Dec "
            else
            echo "Invalid month"
            fi
            echo ""

            #DAY
            echo -n "What day? "
            read d
            if [ "$d" -lt "9" ]
            then
                    mnd="$mn"" ""$d"
            elif [ "$d"  -gt "31" ]
            then
                    mnd="1"
            else
                    mnd="$mn""$d"
            fi



 for dy in {0..4}; do
    date -d "$mn $d - $dy days" +'%b %_d'
 done

Output:

What month?
8
What day?
1

Aug  1
Jul 31
Jul 30
Jul 29
Jul 28

What I want now is to store each date in to variable for example the first line must Aug 1 must be stored on variable x1 Jul 31 must be stored on variable x2 etc.. What I mean whatever output on the first list must be stored on x1 and so on.

2

Use an array instead of trying to create separate variables.

declare -a x

for dy in {0..4}; do
  x+=( "$( date -d "$mn $d - $dy days" +'%b %_d' )" )
done

You may then access the four values in ${x[0]} through to ${x[3]}.

For the first part of your script, have you considered using a select statement?

select mn in "Jan" "Feb" "Mar" "Apr" "May" "Jun" \
             "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
do
  if [[ -z "$mn" ]]; then
    echo "Invalid choice" >&2
  else
    break
  fi
done

printf "You selected '%s'\n" "$mn"

This does the following:

1) Jan    3) Mar   5) May   7) Jul   9) Sep  11) Nov
2) Feb    4) Apr   6) Jun   8) Aug  10) Oct  12) Dec
#? 56
Invalid choice
#? 5
You selected 'May'

The value of $mn will be the selected string (May for example).

|improve this answer|||||
  • Will this actually I'm looking for. I can't explain more further earlier the i++ hahaha. – Edmhar Aug 5 '16 at 6:54
  • for the first part of my script I already have that if..else statement is under of select statement – Edmhar Aug 5 '16 at 6:57
  • how can the variable? is right? echo ${x[0]} ? – Edmhar Aug 5 '16 at 7:03
  • @JJ-SAMA echo "${x[0]}" outputs the first value in the array x. The select statement replaces your if ... then ... else statements. – Kusalananda Aug 5 '16 at 7:07
  • 1
    @JJ-SAMA Sorry, I made a typo. It's fixed now. – Kusalananda Aug 5 '16 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.