[wplug] Bash conditional help
David J. Pryke
david-wplug at pryke.us
Fri May 2 14:34:16 EDT 2008
scoob8000 wrote:
> I'm having a little brain fart here..
>
> How can I use an if then else to test for partial matches in a variable?
>
> For example, say I want to match on "apple" below...
>
> myvar=orange orange apple grape
>
> if [ "$myvar" = "apple" ]; then
> echo Variable contains apple
> else
> echo Variable does not contain apple
> fi
>
> Thanks
Use double square brackets and a double-equals test with wildcards.
For example, this script:
==========================================
#!/bin/bash
if [ 1 == 1 ]
then
echo "1=1"
fi
if [[ "1" == "1" ]]
then
echo "1=1 with quotes"
fi
if [[ "123456" == *34* ]]
then
echo "123456 contains 34"
fi
if [[ "123456" == *d4* ]]
then
echo "123456 contains d4"
fi
============================================
will output:
=====================
1=1
1=1 with quotes
123456 contains 34
=====================
Full reference for this behavior is here:
http://tldp.org/LDP/abs/html/comparison-ops.html
--
Thanks,
David J. Pryke
More information about the wplug
mailing list