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

Pattern Matching in Bash

Status
Not open for further replies.

bdw238

MIS
Dec 15, 2005
52
GB


Hello,


Can someone advise me how to pattern match like the following bit of perl code in BASH

$message="There has been an error detected";
$pattern="error";
if ($message =~ m/$pattern/i) {
print "Found error\n";
} elsif {

# Do nothing

}


Brian
 
for a start:
man grep
man bash

e.g. your example:

message="There has been an error detected"
pattern="error"
echo $message | grep -q $pattern && echo "Found error"

hope this helps
 
case $message in *$pattern*) print "Found error\n";; esac

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Or:

[tt]if [[ $message = *$pattern* ]] ; then echo "Found error" ; fi[/tt]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top