Pattern matching from the input arguments
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]+$
user31522
Updated on September 18, 2022Comments
-
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 likeJboss5.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 over 11 yearsI 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 over 11 yearsI've one of my script and looking for enhancing it.
-
user31522 over 11 yearsSo 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 over 11 yearssmall 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 over 11 yearsThanks 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 over 11 yearsI've tried to use = , == and ran it passing jboss5.0.3fds but getting the result as No Match ...
-
lmcanavals over 11 yearsWhat 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 over 11 yearsHi Martin Canaval, Thanks so much sir for your assistance! I'm on Linux
code
Linux 2.6.5-7.308-bigsmp #1 SMP Mon Dec 10 11:36:40 UTC 2007 i686 i686 i386 GNU/Linuxcode
-
lmcanavals over 11 yearsI made some changes that should work on ksh.
-
user31522 over 11 yearsLooks like we've it bash-2.05b-305.18 ... which bash /bin/bash
-
Mikel over 11 yearsThe
=~
operator was introduced in bash 3.0. Maybe OP has bash 2.05 or earlier? -
Mikel over 11 years@user31522 Please update your question, which says you're using
ksh
. -
user31522 over 11 yearsOMG .... getting No Match ./test3 jboss5.0.3 No match ./test3 5.0.3 No match
-
user31522 over 11 yearsHi Mike ...yeah may be =~ is not with 2.*
-
Mikel over 11 yearsMartin 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 over 11 yearsTo 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 over 11 yearsAwesome !!! Thanks so much Mike and Marting .... it is working now after changing + to a... Thanks so much again for the help!!!
-
user31522 over 11 yearsHey Martin/Mike, I've updated my question. I believe it si little clear now. My apologies for the inconvenience caused. Thanks so much!