Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple "if" command question 2

Status
Not open for further replies.
Feb 12, 2002
80
NO
I can't believe I'm struggling with this - I can't work out why it isnt working ...
The script I have entered bleow is not the script I am working on - I have writte it to show what I am trying to do:
Code:
#!/bin/ksh
#set -vx
#
# Check number of parameters
if [  $# -lt 1 ]
  then
   echo "Usage:"
   echo "        vel <word>"
  exit
fi

WORD=$1

    if [ "${WORD}" = *VEL* ]
       then
          echo "${WORD} contains VEL "
       else
          echo "${WORD} !contain VEL "
    fi

exit


Whi is this not working?!
I want to be able to pick out VEL ENVELOPE VELOCITY etc
What am I doing wrong?

 

try:

case "${WORD}" in
*VEL*) echo "${WORD} contains VEL"
;;
*) echo "$WORD} doesn't contain VEL"
;;
esac

HTH,

p5wizard
 
To clarify (or perhaps confuse!), [ is an alias for test, which does not support the syntax you were using. [[ is a shell built-in in ksh and bash which has more capabilities.

And strictly speaking they are not "regular expressions" you are using, but use filename expansion semantics.

Annihilannic.
 
Cheers.

Din't get the regular expression version to work - but the case one did.

I'll try to implement this into my overall script.

thanks a lot p5wizard
 
The Korn shell has a pattern match operator for use in the [tt][[ ... ]][/tt] construct. It's [tt]@(pattern)[/tt]. Also in the Korn shell, double parenthesis are a shell built in that simplifies numeric comparisons and operations.

The following should work.
Code:
#/bin/ksh

if (( $# < 1 ))
then
   echo "Usage:"
   echo "        vel <word>"
   exit
fi

WORD=$1

if [[ "${WORD}" = @(*VEL*) ]]
then
    echo "${WORD} contains VEL "
else
    echo "${WORD} !contain VEL "
fi

exit

Hope this helps.
 
Hi

Hmm... My [tt]ksh[/tt] ( PD KSH v5.2.14 ) works as its man page says.
man ksh said:
[[ expression ]]
Similar to the test and [ ... ] commands (described later), with the following exceptions:
[gray](...)[/gray]
· The second operand of != and = expressions are patterns (e.g., the comparison in
[[ foobar = f*r ]]
succeeds).
Code:
[blue]master #[/blue] word="what is ENVELOPE VELOCITY anyway"
[blue]master #[/blue] [[ "${word}" = *VEL* ]] && echo match || echo no
match
[blue]master #[/blue] [[ "${word}" = *HEL* ]] && echo match || echo no
no

[blue]master #[/blue] word="VEL"
[blue]master #[/blue] [[ "${word}" = *VEL* ]] && echo match || echo no
match

[blue]master #[/blue] word="Hello Afrika, tell me how you're doin'!"
[blue]master #[/blue] [[ "${word}" = *VEL* ]] && echo match || echo no
no
Of course, Sam's [tt]@(...)[/tt] code also works. Is there an incompatibility, or I misunderstood something ?

Feherke.
 
Feherke's method works for me too. I'm on Solaris 8.

littleIdiot, what OS and version of KSH are you running?

Just curious why it wasn't working for you.
 
Hey,

Sorry for delayed response.

I tried SamBones' suggestion - no joy.
Not sure what OS/version of KSH I am on - Solaris ... ?

I got this message (file called vel2.sh):
vel2.sh: 0: not found
vel2.sh: syntax error at line 12: `(' unexpected

I liked the idea of using the switch command, but I also need to have a catch-all option:

if $WORD = *VEL* do this
else if $WORD = *JOB* do this
else do this

Can I do that with switch?
 
Hi

Of course you can. Did you tried to work on p5wizard's code ?
Code:
case "${WORD}" in
  *VEL*) echo "${WORD} contains VEL" ;;              [gray]# if $WORD = *VEL* do this[/gray]
  *JOB*) echo "${WORD} contains JOB" ;;              [gray]# else if $WORD = *JOB* do this[/gray]
  *) echo "${WORD} contain nothing interesting" ;;   [gray]# else do this[/gray]
esac

Feherke.
 
That's the ticket - thank you.

I tried a few things but they just wern't working ... I guess I just need to pack up and go home!

It'all sorted now - I tink!!

Enjoy your weekend, and thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top