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

trying to check if $* contains a *

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I trying to check if the parameters given to my scrip contains

tyed some thing like this(and numerus variations - with "" and ')
echo $*|grep '*'|wc -l


Larshg
 
To test if parameters contain a character '*', you can do :

[tt]if echo "$*" | grep -q '*'
then echo "'*' found"
else echo "'*' not found"
fi[/tt]

Another solution with bash and ksh :

[tt]if [[ "$*" = *\** ]]
then echo "'*' found"
else echo "'*' not found"
fi[/tt]



Jean Pierre.
 
Another way:
case "$*" in
*\**) echo "almost one '*' found";;
*) echo "no '*' found";;
esac

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Just escape the asterisk. [tt]grep[/tt]'s argument is just a regular expression.
Code:
#!/bin/ksh

CHECKSTARS=`echo "$*" | grep "\*" | wc -l`

if [ $CHECKSTARS -gt 0 ]
then
    echo "There be stars, but no green clovers, blue diamonds, or purple horseshoes"
fi

--
-- GhodMode
 
Code:
echo "$*" | tr -cd '\*' | wc -c
would tell you the number of stars explicitly :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top