Regular expression to control date format

2 réponses
Avatar
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

2 réponses

Avatar
Stephane CHAZELAS
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
Avatar
titof92
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... ;-)