Echo date variable from data input to a script

Hi, I'm trying to make a script which you type the year, select the month and day and then create the date in the format 2010-12-7.

#!/bin/bash

dia () {
echo " Seleccione el dia:"
select file in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Salir
do
case $file in
"1") d=1 ; fecha ;;
"2") d=2 ; fecha ;;
"3") d=3 ; fecha ;;
"4") d=4 ; fecha ;;
"5") d=5 ; fecha ;;
"6") d=6 ; fecha ;;
"7") d=7 ; fecha ;;
"8") d=8 ; fecha ;;
"9") d=9 ; fecha ;;
"10") d=10 ; fecha ;;
"11") d=11 ; fecha ;;
"12") d=12 ; fecha ;;
"13") d=13 ; fecha ;;
"14") d=14 ; fecha ;;
"15") d=15 ; fecha ;;
"16") d=16 ; fecha ;;
"17") d=17 ; fecha ;;
"18") d=18 ; fecha ;;
"19") d=19 ; fecha ;;
"20") d=20 ; fecha ;;
"21") d=21 ; fecha ;;
"22") d=22 ; fecha ;;
"23") d=23 ; fecha ;;
"24") d=24 ; fecha ;;
"25") d=25 ; fecha ;;
"26") d=26 ; fecha ;;
"27") d=27 ; fecha ;;
"28") d=28 ; fecha ;;
"29") d=29 ; fecha ;;
"30") d=30 ; fecha ;;
"31") d=31 ; fecha ;;
"Salir") exit 1;;
 *)
    echo "ERROR: Seleccione correctamente... "
    dia;;
esac
done
}

mes () {
echo " Seleccione el mes:"
select file in enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre Salir
do
case $file in
"enero") m=1 ; dia ;;
"febrero") m=2 ; dia ;;
"marzo") m=3 ; dia  ;;
"abril") m=4 ; dia  ;;
"mayo") m=5 ; dia  ;;
"junio") m=6 ; dia  ;;
"julio") m=7 ; dia  ;;
"agosto") m=8 ; dia  ;;
"septiembre") m=9 ; dia  ;;
"octubre") m=10 ; dia  ;;
"noviembre") m=11 ; dia  ;;
"diciembre") m=12 ; dia  ;;
"Salir") exit 1;;
 *)
    echo "ERROR: Seleccione correctamente... "
    mes;;
esac
done
}

anno () {
echo " Seleccione la anno, insertando las dos ultimas cifras:"
read yshort

if [[ $( echo $yshort | sed 's/^[-+0-9][0-9]*//' | wc -c ) -ne 1 ]]; then echo "ERROR: Introduzca correctamente dos caracteres numericos." && exit 1; fi

if [[ $( expr length $yshort) -ne 2 ]] ; then echo "ERROR: Introduzca SOLO dos caracteres" && exit 1 ;fi
Y=20$yshort
}

fecha () {
fecha=`echo " $Y-$m-$d"`
echo $fecha
exit 0
}

main () {

anno
mes
dia

}

main

The problem? Simple, I want to select the day regarding the month, I mean, to print 30 days if you selected the month of 30 days and so on.

Any help on how to change my script?
Thanks in advance.

use the cal command !

Hi,

Not sure on how to use cal command...:frowning:

---------- Post updated at 09:58 AM ---------- Previous update was at 09:49 AM ----------

As far as I know cal only display data.. what I want is:

  • 1st you select the year.
  • late you select the month, and here comes the problem
  • I need to print only the days regarding the selected month.

Understand? This script is interactive.

Thanks

Which is what cal does:

raz:/home/vbe $ cal 12 2009
   December 2009
 S  M Tu  W Th  F  S
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

The thing is quite different if lets say you want to know what week day is the 15th of dec 2009... there is a bit of encoding to do from cal results...

Hi Vbe,
Sometimes my english sucks.. dont know if I couldn't explain what I want or you dont understand me what I write..:slight_smile:
The code below does:
1- Ask you for the las two characters of the year. --> I get the year VAR. (2010)
2- Is shows all months on console, so you select the desired month. --> I get month VAR (12)
3- It shows the 31 days on console, so you select the desired day --> I get here the day VAR (7)
But if you select a month with 30 days, the 3er part shows you 31 days.. that's the point. I want to print on the 3er part of the script the days the selected month have and I don't know how to do it.

Thanks

---------- Post updated at 10:48 AM ---------- Previous update was at 10:09 AM ----------

OK, here's my ugly code.. but at least works:

#!/bin/bash

dia () {
clear
echo $m

if  [[ $m -eq 1 || $m -eq  3 || $m -eq 5 || $m -eq 7 || $m -eq 8 || $m -eq 10 || $m -eq 12 ]] ;then

echo " Seleccione el dia:"
select file in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Salir
do
case $file in
"1") d=1 ; fecha ;;
"2") d=2 ; fecha ;;
"3") d=3 ; fecha ;;
"4") d=4 ; fecha ;;
"5") d=5 ; fecha ;;
"6") d=6 ; fecha ;;
"7") d=7 ; fecha ;;
"8") d=8 ; fecha ;;
"9") d=9 ; fecha ;;
"10") d=10 ; fecha ;;
"11") d=11 ; fecha ;;
"12") d=12 ; fecha ;;
"13") d=13 ; fecha ;;
"14") d=14 ; fecha ;;
"15") d=15 ; fecha ;;
"16") d=16 ; fecha ;;
"17") d=17 ; fecha ;;
"18") d=18 ; fecha ;;
"19") d=19 ; fecha ;;
"20") d=20 ; fecha ;;
"21") d=21 ; fecha ;;
"22") d=22 ; fecha ;;
"23") d=23 ; fecha ;;
"24") d=24 ; fecha ;;
"25") d=25 ; fecha ;;
"26") d=26 ; fecha ;;
"27") d=27 ; fecha ;;
"28") d=28 ; fecha ;;
"29") d=29 ; fecha ;;
"30") d=30 ; fecha ;;
"31") d=31 ; fecha ;;
"Salir") exit 1;;
 *)
    echo "ERROR: Seleccione correctamente... "
    dia;;
esac
done

fi

if  [[ $m -eq 4 || $m -eq 6 || $m -eq 9 || $m -eq 11 ]] ;then
echo " Seleccione el dia:"
select file in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Salir
do
case $file in
"1") d=1 ; fecha ;;
"2") d=2 ; fecha ;;
"3") d=3 ; fecha ;;
"4") d=4 ; fecha ;;
"5") d=5 ; fecha ;;
"6") d=6 ; fecha ;;
"7") d=7 ; fecha ;;
"8") d=8 ; fecha ;;
"9") d=9 ; fecha ;;
"10") d=10 ; fecha ;;
"11") d=11 ; fecha ;;
"12") d=12 ; fecha ;;
"13") d=13 ; fecha ;;
"14") d=14 ; fecha ;;
"15") d=15 ; fecha ;;
"16") d=16 ; fecha ;;
"17") d=17 ; fecha ;;
"18") d=18 ; fecha ;;
"19") d=19 ; fecha ;;
"20") d=20 ; fecha ;;
"21") d=21 ; fecha ;;
"22") d=22 ; fecha ;;
"23") d=23 ; fecha ;;
"24") d=24 ; fecha ;;
"25") d=25 ; fecha ;;
"26") d=26 ; fecha ;;
"27") d=27 ; fecha ;;
"28") d=28 ; fecha ;;
"29") d=29 ; fecha ;;
"30") d=30 ; fecha ;;
"Salir") exit 1;;
 *)
    echo "ERROR: Seleccione correctamente... "
    dia;;
esac
done

fi

if  [[ $m -eq  2  ]] ;then
echo " Seleccione el dia:"
select file in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Salir
do
case $file in
"1") d=1 ; fecha ;;
"2") d=2 ; fecha ;;
"3") d=3 ; fecha ;;
"4") d=4 ; fecha ;;
"5") d=5 ; fecha ;;
"6") d=6 ; fecha ;;
"7") d=7 ; fecha ;;
"8") d=8 ; fecha ;;
"9") d=9 ; fecha ;;
"10") d=10 ; fecha ;;
"11") d=11 ; fecha ;;
"12") d=12 ; fecha ;;
"13") d=13 ; fecha ;;
"14") d=14 ; fecha ;;
"15") d=15 ; fecha ;;
"16") d=16 ; fecha ;;
"17") d=17 ; fecha ;;
"18") d=18 ; fecha ;;
"19") d=19 ; fecha ;;
"20") d=20 ; fecha ;;
"21") d=21 ; fecha ;;
"22") d=22 ; fecha ;;
"23") d=23 ; fecha ;;
"24") d=24 ; fecha ;;
"25") d=25 ; fecha ;;
"26") d=26 ; fecha ;;
"27") d=27 ; fecha ;;
"28") d=28 ; fecha ;;
"Salir") exit 1;;
 *)
    echo "ERROR: Seleccione correctamente... "
    dia;;
esac
done


fi
}

mes () {
clear
echo " Seleccione el mes:"
select file in enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre Salir
do
case $file in
"enero") m=1 ; dia ;;
"febrero") m=2 ; dia ;;
"marzo") m=3 ; dia  ;;
"abril") m=4 ; dia  ;;
"mayo") m=5 ; dia  ;;
"junio") m=6 ; dia  ;;
"julio") m=7 ; dia  ;;
"agosto") m=8 ; dia  ;;
"septiembre") m=9 ; dia  ;;
"octubre") m=10 ; dia  ;;
"noviembre") m=11 ; dia  ;;
"diciembre") m=12 ; dia  ;;
"Salir") exit 1;;
 *)
    echo "ERROR: Seleccione correctamente... "
    mes;;
esac
done
}

anno () {
clear
echo " Seleccione la anno, insertando las dos ultimas cifras:"
echo -n "20"
read yshort

if [[ $( echo $yshort | sed 's/^[-+0-9][0-9]*//' | wc -c ) -ne 1 ]]; then echo "ERROR: Introduzca correctamente dos caracteres numericos." && exit 1; fi

if [[ $( expr length $yshort) -ne 2 ]] ; then echo "ERROR: Introduzca SOLO dos caracteres" && exit 1 ;fi
Y=20$yshort
}

fecha () {
fecha=`echo "$Y-$m-$d"`
exit 0
}

main () {
clear
anno
mes
dia

}

main


Have a look at the following thread especially at the code provided in post #9 :

By "use cal ", i was thinking : use it to validate that the entered date is OK (also handle leap year)

echo "Enter the year on 4 digit) :"
read y
echo "Enter the month on 2 digit) :"
read m
echo "Enter the day on 2 digit) :"
read d

chk_date(){
typeset -i y m d
y="${1%????}" ; t="${1%??}" ; m="${t#$y}" ; d="${1#??????}"
cal $m $y 2>/dev/null | tail +3 | sed -e 'N;N;N;N;N;s/\n/ /g' | grep " $d" >/dev/null 2>&1 && return 0 || return 1
}

chk_date "$y$m$d" && echo "Date Valide" || echo "Date not Valide"

Of course this short code does not behave exactly the way you want, but it gives some clue... and it handles leap year :slight_smile:

---------- Post updated at 05:56 PM ---------- Previous update was at 05:50 PM ----------

Of course if you have some recent date tools that can handle it better, you should of course use it instead of my hugly code :slight_smile:

1 Like

Umm.. very interesting... Thanks..