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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wildcard in loops

Status
Not open for further replies.

vidmo

Technical User
Sep 18, 2006
14
GB
I'm hoping this is a simple question:

my script:

x="B1"
if [ "$x" = B* ]; then
echo B
else
echo M
fi

this doesn't work but hopefully you can see what i want to do - how do i get the if loop to recognize that the input contains the letter 'b' with out having to do:
if [ x = 1 ] || [ x = 2 ] || [ x = 3 etc.......

thanks

vidmo
 
You could use expr. for example
Code:
x=B1
if expr $x : "B*" >/dev/null 
then
  echo B
else
  echo M
fi

or you could use case
Code:
case $x in
  B*) echo B;;
  *)  echo M;;
esac

Ceci n'est pas une signature
Columb Healy
 
Hi

Eh, turbo Columb. Only one solution left to me :
Code:
x="B1"
if [red][[/red][ "$x" = B* ][red]][/red]; then
echo B
else
echo M
fi
Tested with [tt]bash[/tt] and (pd)[tt]ksh[/tt].

By the way, the [tt]if[/tt] is not a loop.

Feherke.
 
thanks very much, both of you,
feherke- can i ask why the extra brackets enable the wildcard?

thanks
vidmo
 
[ is the original Bourne shell (sh) syntax for evaluating conditional expressions. [[ was added by ksh and used in subsequent shells like bash. I think. :)

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top