Find the answer to your Linux question:
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 ...
  1. #1
    Just 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:

    Code:
    ./script_name arg1 <arg2> <arg3> <regex>
    the regex is optional. within my script, then, i need to check if the regex was added. this is what i'm thinking:

    Code:
    for i in $*
      if [ $i begins with any of these '[][\\.*$^]/\\&' ]
        do this
      fi
    done
    but i don't know how the syntax for doing this. i would appreciate any help you can provide.

    thanks.

  2. #2
    Linux 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.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Shouldn't you be checking the number of arguments?
    burschik, 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.
    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.
    True. For example, this:
    Code:
    fred.txt
    or even this:
    Code:
    fred
    is a valid regular expression.

    The safest thing to do is to rework what he requires as syntax in his script arguments. Perhaps instead of this:
    Code:
    ./script_name arg1 <arg2> <arg3> <regex>
    He should have this:
    Code:
    ./script_name arg1 <arg2> <arg3> <-e 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.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just 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.

  5. #5
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    burschik, you're quite possibly correct. But I'm guessing that in his example, the <> stands for an optional argument.
    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.

  6. #6
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by lackawaxen View Post
    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.
    For the first two, you can use parameter expansion. For the last item, you will probably need to call "grep", or "sed", or something like that.

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Code:
           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 ksh93
    I suggest reading the man page and then writing a few scripts to explore the possibilities ... cheers, drl
    Welcome - 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 )

  8. #8
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Nice, but my ksh can't do that.

  9. #9
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi, burschik.
    Quote Originally Posted by burschik View Post
    Nice, but my ksh can't do that.
    There is an alternative:
    Code:
                  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.39
    which may not help the OP, but might be useful in other situations ... cheers, drl
    Welcome - 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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...