Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

we're trying to enhance the 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 if condition with ksh script

Sample usecases/test:

./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 teh script.
share|improve this question
    
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? –  Bernhard Jan 31 '13 at 16:00
    
I've one of my script and looking for enhancing it. –  user31522 Jan 31 '13 at 16:09
    
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 Jan 31 '13 at 16:17
    
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 Jan 31 '13 at 16:22

2 Answers 2

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
share|improve this answer

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|improve this answer
    
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 Jan 31 '13 at 16:38
    
I've tried to use = , == and ran it passing jboss5.0.3fds but getting the result as No Match ... –  user31522 Jan 31 '13 at 16:43
    
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. –  Martín Canaval Jan 31 '13 at 16:48
    
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 –  user31522 Jan 31 '13 at 16:53
1  
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. –  Mikel Jan 31 '13 at 17:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.