View Full Version : [SOLVED] How do You Compare Two Strings With Wildcards in Bash?
Penguin Guy
April 6th, 2009, 07:45 PM
if [ "foobar" == *"bar" ]; then # Won't work because you need double brackets '[[...]]'
if [[ "foobar" == "*bar" ]]; then # Will not work because * is inside quotes
if [[ "foobar" == *"bar" ]]; then # Will work
Thanks to everyone who helped!
akvino
April 6th, 2009, 07:51 PM
I am not sure if this is what you're looking for but if I were you I would set a variable to take the input or if you want
'$*' will give you the entire input argument.
Martin Witte
April 6th, 2009, 08:11 PM
It has to do with quotation, see this link (http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html)
martin@pc ~
$ if [[ "abc: GPG encrypted data" = *': GPG encrypted data' ]]; then echo Match
; fi
Match
Penguin Guy
April 6th, 2009, 08:44 PM
Thank you! I just needed to put the '*' outside the quotation marks and use double square-brackets.
Penguin Guy
April 6th, 2009, 08:45 PM
Here is the correct code:
#!/bin/bash
# Compare Test
if [[ "abc: GPG encrypted data" = *": GPG encrypted data" ]]; then
echo "Match!"
exit 0
else echo "No match."
fi
exit 1
ghostdog74
April 7th, 2009, 01:39 AM
you can also use case/esac
Penguin Guy
April 7th, 2009, 11:17 AM
Unless I am actually comparing multiple items, I think I'll stick with if for now, thanks anyawy!
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.