Regular expression to control date format

Le
titof92
Hi,
I'm running a script sh, with a date formatted as "YYYYMMDD", as
argument (example : ./my_prog.sh 20111216
In my script, I just would like to test if the user specified an
argument respecting the folowing rules :
- 8 characters long
- constituted only by numbers

I tried to write this :
if [[$1 =~ /^[2][0][1-2][0-9][0-1][0-9][0-3][0-9]$/ ]]
then
..
fi

But it does not work;

Any suggestion ?

Thanks,
Christophe
Vidéos High-Tech et Jeu Vidéo
Téléchargements
Vos réponses
Gagnez chaque mois un abonnement Premium avec GNT : Inscrivez-vous !
Trier par : date / pertinence
Stephane CHAZELAS
Le #24074211
2011-12-15, 08:43(-08), titof92:
Hi,
I'm running a script sh, with a date formatted as "YYYYMMDD", as
argument (example : ./my_prog.sh 20111216
In my script, I just would like to test if the user specified an
argument respecting the folowing rules :
- 8 characters long
- constituted only by numbers

I tried to write this :
if [[$1 =~ /^[2][0][1-2][0-9][0-1][0-9][0-3][0-9]$/ ]]
then
.....
fi


[...]

Tu peux parler francais ici, tu sais.

case $1 in
(20[12][0-9][01][0-9][0-3][0-9]) ...
esac

--
Stephane
titof92
Le #24076541
On 15 déc, 18:27, Stephane CHAZELAS wrote:
2011-12-15, 08:43(-08), titof92:> Hi,
> I'm running a script sh, with a date formatted as "YYYYMMDD", as
> argument (example : ./my_prog.sh 20111216
> In my script, I just would like to test if the user specified an
> argument respecting the folowing rules :
> - 8 characters long
> - constituted only by numbers

> I tried to write this :
> if [[$1 =~ /^[2][0][1-2][0-9][0-1][0-9][0-3][0-9]$/ ]]
>     then
>         .....
> fi

[...]

Tu peux parler francais ici, tu sais.

case $1 in
  (20[12][0-9][01][0-9][0-3][0-9]) ...
esac

--
Stephane



Super, merci beaucoup. Cela fonctionne bien.
Désolé, je pensais poster sur le forum comp.unix.shell... c'est
pourquoi mon message était en anglais... ;-)
Publicité
Poster une réponse
Anonyme