Pattern matching from the input arguments

7,300

Solution 1

Pattern matching in POSIX shells is done with the case construct. ksh also as the [[ x = pattern ]] operator (also copied by bash and zsh) and [[ x =~ regexp ]] in recent versions.

So:

case $1 in
  (*5.0.3*)
    install=$1.tar
    echo Found;;
  (*)
    echo >&2 Not found
    exit 1;;
esac

Solution 2

I'm not an expert at regular expressions, but this works, at least for what you described.

#!/bin/sh

argument="$1"

#if [[ $argument =~ [a-zA-Z]*5\.0\.3[a-zA-Z]+ ]]; then# only works on bash
if echo $argument | egrep -q '[a-zA-Z]*5\.0\.3[a-zA-Z]+'; then
  #echo "Found: ${BASH_REMATCH[0]}" # for bash
  echo "Match Found"

  # you can check for $argument at some other location, here.

else
  echo "No match"
fi

Saving it as test and running it, gives the following results:

bash test 333xxxx5.0.3xxxxx777
Match Found

bash test 333xxxx5.0.2xxxxx777
No match

bash test 5.0.3xxxxx777
Match Found

bash test 5.0.2xxxxx777
No match

You can add ^ at the beginning and $ at the end, to match the full string or nothing. Like this ^[a-zA-Z]*5\.0\.3[a-zA-Z]+$

Share:
7,300
user31522
Author by

user31522

Updated on September 18, 2022

Comments

  • user31522
    user31522 over 1 year

    We're trying to enhance one of our scripts.

    Users will pass some arguments and part of the arguments will have 5.0.3. For an example, the input argument would be like Jboss5.0.3GA. Since it ( Jboss5.0.3GA ) has "5.0.3" we'll try to locate the installation binary file "Jboss5.0.3GA.tar".

    The current script we've now is a ksh script. I'm trying to use an if condition with the script.

    Sample use cases and results:

    ./test.sh Jboss5.0.3GA
    Match found... we'll try to locate the installation binary
    ./test.sh Jboss5.0.3
    Match found... we'll try to locate the installation binary
    ./test.sh 5.0.3
    Match found... we'll try to locate the installation binary
    ./test.sh Jboss5.1.3
    No Match found ... we'll be exiting the script.
    
    • Bernhard
      Bernhard over 11 years
      I have no clue what you want. I think it is not me, but the way you ask the question. Can you clarify a bit? Be more specific in input and desired behavior? Also, what have you tried?
    • user31522
      user31522 over 11 years
      I've one of my script and looking for enhancing it.
    • user31522
      user31522 over 11 years
      So users running the scripts will pass some arguments while running the script like jboss5.0.3GA ( = xxxx5.0.3xxx). So when I see 5.0.3 as as part of their input ( sometimes they can pass 5.0.3jboss ) all I'd need is to lookup for that string 5.0.3 in their input argument ...then I'd need to lookup for the file name at some location. Filename would be Jboss5.0.3GA.tar. If they do pass 5.0.3 as a part of their input argument I'll throw an error with usage function which will tell the users to pass the right arguments.... Thanks so much for the prompt response!
    • user31522
      user31522 over 11 years
      small correction in my previous post:If they donot* pass 5.0.3 as a part of their input argument I'll throw an error with usage function which will tell the users to pass the right arguments
  • user31522
    user31522 over 11 years
    Thanks so much again for the prompt response ...I'm getting the error code./test2.bash jboss5.0.3fds ./test2.bash: line 5: conditional binary operator expected ./test2.bash: line 5: syntax error near =~' ./test2.bash: line 5: if [[ $argument =~ ^[a-zA-Z]*5\.0\.3[a-zA-Z]+$ ]]; then'code
  • user31522
    user31522 over 11 years
    I've tried to use = , == and ran it passing jboss5.0.3fds but getting the result as No Match ...
  • lmcanavals
    lmcanavals over 11 years
    What OS are you running and do you have bash installed at all? the "=~" operator is bash specific. Let me know if that is a problem I'll see what I can do.
  • user31522
    user31522 over 11 years
    Hi Martin Canaval, Thanks so much sir for your assistance! I'm on LinuxcodeLinux 2.6.5-7.308-bigsmp #1 SMP Mon Dec 10 11:36:40 UTC 2007 i686 i686 i386 GNU/Linuxcode
  • lmcanavals
    lmcanavals over 11 years
    I made some changes that should work on ksh.
  • user31522
    user31522 over 11 years
    Looks like we've it bash-2.05b-305.18 ... which bash /bin/bash
  • Mikel
    Mikel over 11 years
    The =~ operator was introduced in bash 3.0. Maybe OP has bash 2.05 or earlier?
  • Mikel
    Mikel over 11 years
    @user31522 Please update your question, which says you're using ksh.
  • user31522
    user31522 over 11 years
    OMG .... getting No Match ./test3 jboss5.0.3 No match ./test3 5.0.3 No match
  • user31522
    user31522 over 11 years
    Hi Mike ...yeah may be =~ is not with 2.*
  • Mikel
    Mikel over 11 years
    Martin was guessing that you didn't want to allow numbers at the end. Change the last + to a *. And please update your question with clear examples of inputs and corresponding expected output.
  • lmcanavals
    lmcanavals over 11 years
    To match jboss5.0.3 you need to change to ^[a-zA-Z]*5\.0\.3[a-zA-Z]*$. Because the + at the end, expects at least 1 character after the 5.0.3
  • user31522
    user31522 over 11 years
    Awesome !!! Thanks so much Mike and Marting .... it is working now after changing + to a... Thanks so much again for the help!!!
  • user31522
    user31522 over 11 years
    Hey Martin/Mike, I've updated my question. I believe it si little clear now. My apologies for the inconvenience caused. Thanks so much!