Operating System - HP-UX
1752648 Members
5758 Online
108788 Solutions
New Discussion

Re: Checking script parameter (Respecting a mask)

 
SOLVED
Go to solution
SwissKnife
Frequent Advisor

Checking script parameter (Respecting a mask)

Hi,

 

my script accept only one parameter

./myscript.ksh param1

 

I need to write a routine to check and validate the parameter first priot to actions and this parameter needs to contain at least something matching NN_NN_NNNN_NN_NN_NN_NNNN format. Where N is digit between 0 and 9.

 

Example:

myscript.ksh serveurX120_11_2016_23_55_55_0200_commentZ21

myscript.ksh magic10020_11_2016_23_55_55_0200_commentZ23

 

Then, how to be sure that something matching NN_NN_NNNN_NN_NN_NN_NNNN is part of the parameter ?

 

 

Your help is very welcome, any idea here ?

 

Thanks in advance,

Den

 

4 REPLIES 4
Steven Schweda
Honored Contributor
Solution

Re: Checking script parameter (Respecting a mask)

   One possible way, using "sed" [*]:

mba$ p1='[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_'           # Part 1.
mba$ p2='[0-9][0-9]_[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]' # Part 2.

mba$ g1='erveurX120_11_2016_23_55_55_0200_commentZ21' # Good string.
mba$ b1='erveurX120_11_2016_23_55_55_02o0_commentZ21' # Bad string.

mba$ echo "${g1}" | sed -e "s/..*\(${p1}${p2}\)..*/\1/"
20_11_2016_23_55_55_0200

mba$ echo "${b1}" | sed -e "s/..*\(${p1}${p2}\)..*/\1/"
erveurX120_11_2016_23_55_55_02o0_commentZ21

   If this "sed" command returns the original string, then the
NN_NN_NNNN_NN_NN_NN_NNNN pattern was not matched.

   The "..*" parts here require at least one non-pattern character
before and after the pattern.  As usual in such situations, many things
are possible, and you must decide exactly what you wish to accept and
reject.


   [*] As I always say, if you can't do it with "sed", then it doesn't
need doing.

SwissKnife
Frequent Advisor

Re: Checking script parameter (Respecting a mask)

Hi Steven,

 

Thank you very much, it works perfectly.

 

Kind regards,

Den.

 

 

SwissKnife
Frequent Advisor

Re: Checking script parameter (Respecting a mask)

Almost OK.

 

As you mention, it cannot work with:

       aaaa10_10_2017_23_12_22_0004

       10_10_2017_23_12_22_0004eeee

       10_10_2017_23_12_22_0004

 

Alll these strings are respecting the mask.

 

I could add systematically Z before and Z after for my test. Correct ?

 

 

 

Kind regards, Den.

 

 

 

 

Steven Schweda
Honored Contributor

Re: Checking script parameter (Respecting a mask)

> The "..*" parts here require at least one non-pattern character
> before and after the pattern. [...]

> I could add systematically Z before and Z after for my test. Correct ?

   Sure.

mba$ g2='aaaa10_10_2017_23_12_22_0004'
mba$ g3='10_10_2017_23_12_22_0004eeee'
mba$ g4='10_10_2017_23_12_22_0004'

mba$ echo "Z${g1}Z" | sed -e "s/..*\(${p1}${p2}\)..*/\1/"
20_11_2016_23_55_55_0200

mba$ echo "Z${g2}Z" | sed -e "s/..*\(${p1}${p2}\)..*/\1/"
10_10_2017_23_12_22_0004

mba$ echo "Z${g3}Z" | sed -e "s/..*\(${p1}${p2}\)..*/\1/"
10_10_2017_23_12_22_0004

mba$ echo "Z${g4}Z" | sed -e "s/..*\(${p1}${p2}\)..*/\1/"
10_10_2017_23_12_22_0004

mba$ echo "Z${b1}Z" | sed -e "s/..*\(${p1}${p2}\)..*/\1/"
ZerveurX120_11_2016_23_55_55_02o0_commentZ21Z

   That way, the result from every bad string, and no good string, will
begin (and end) with "Z".