Results 1 to 9 of 9
hi, all.
i have a ksh script that can take a regular expression when invoked:
Code:
./script_name arg1 <arg2> <arg3> <regex>
the regex is optional. within my script, then, i ...
- 10-13-2008 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 11
checking if parameter is regex
hi, all.
i have a ksh script that can take a regular expression when invoked:
the regex is optional. within my script, then, i need to check if the regex was added. this is what i'm thinking:Code:./script_name arg1 <arg2> <arg3> <regex>
but i don't know how the syntax for doing this. i would appreciate any help you can provide.Code:for i in $* if [ $i begins with any of these '[][\\.*$^]/\\&' ] do this fi done
thanks.
- 10-13-2008 #2Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Shouldn't you be checking the number of arguments? And since fixed strings are also valid regular expressions, there is no way to determine whether a particular string is supposed to be a regular expression, or not.
- 10-13-2008 #3burschik, you're quite possibly correct. But I'm guessing that in his example, the <> stands for an optional argument. (lackawaxen, it would have been a kindness if you had spelled that out for us, so we wouldn't be spinning our wheels here figuring out what you meant.) So the second and third arguments could be missing and he could still have a regular expression.Shouldn't you be checking the number of arguments?
True. For example, this:since fixed strings are also valid regular expressions, there is no way to determine whether a particular string is supposed to be a regular expression, or not.
or even this:Code:fred.txt
is a valid regular expression.Code:fred
The safest thing to do is to rework what he requires as syntax in his script arguments. Perhaps instead of this:
He should have this:Code:./script_name arg1 <arg2> <arg3> <regex>
That is, if the caller of his script wants to add a regular expression, then the caller needs to add not one, but two arguments, and the first of these two is -e.Code:./script_name arg1 <arg2> <arg3> <-e regex>
--
Bill
Old age and treachery will overcome youth and skill.
- 10-13-2008 #4Just Joined!
- Join Date
- Oct 2008
- Posts
- 11
Thank you both for your responses. I apologize for not being more clear. wfe_le is correct: I used <> to indicate optional. Sorry about that.
I don't think it matters if the regex is fred or ^f. I am only interested in those that contain a special character. In my original post, I said that I want to know if an argument "begins" with a special character; but I should have said "contain" for cases such as f$.
In any event, I suppose what I really want to know is how to express stuff like this in ksh:
If parameter begins with X
If parameter ends with X
If parameter contains X
Thanks.
- 10-14-2008 #5Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
I had guessed as much. However, if you have several optional arguments and no switches, it is generally the case that the number of arguments determines the type of the arguments. "seq" is an excellent example of this usage pattern.burschik, you're quite possibly correct. But I'm guessing that in his example, the <> stands for an optional argument.
- 10-14-2008 #6Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
- 10-14-2008 #7Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
I suggest reading the man page and then writing a few scripts to explore the possibilities ... cheers, drlCode:string == pattern True, if string matches pattern. Any part of pattern can be quoted to cause it to be matched as a string. With a successful match to a pattern, the .sh.match array variable will contain the match and sub-pattern matches. string != pattern True, if string does not match pattern. When the string matches the pattern the .sh.match array variable will contain the match and sub-pattern matches. string =~ ere True if string matches the pattern ~(E)ere where ere is an extended regular expression. -- excerpt from man ksh in section describing conditions allowable with [[ compound command for ksh93Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 10-14-2008 #8Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Nice, but my ksh can't do that.
- 10-14-2008 #9Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi, burschik.
There is an alternative:
which may not help the OP, but might be useful in other situations ... cheers, drlCode:When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. The return value is 0 if the string matches (==) or does not match (!=) the pat- tern, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string. An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expres- sion and matched accordingly (as in regex(3)). -- excerpt from man bash for GNU bash 3.2.39Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
