[wplug] Bash conditional help
Moshe Hyzon
mokatz at gmail.com
Fri May 2 15:40:31 EDT 2008
In the interest of "There's more than ONE way to do it!"....
case "$myvar" in
*apple*)
echo "We've got an apple!"
;;
*)
echo "No apple here."
;;
esac
Moshe Hyzon
On Fri, May 2, 2008 at 2:34 PM, David J. Pryke <david-wplug at pryke.us> wrote:
>
> 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
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
>
More information about the wplug
mailing list