Friday, July 20, 2007

Linux:shell:grep how to use grep's result

mystr="IloveChina"
echo $mystr |grep China
echo $? #this will return 0
echo $mystr |grep USA
echo $? #this will return 1
#so you could use this returned value to justify if grep found the string.
# Or you use how many searched results returned to do this, for example,
echo $mystr |grep -c China # return 1 to you
echo $mystr |grep -c USA # return 0 to you
# so simply, you can use the number to justify if you found the results like
if [ `
echo $mystr |grep -c China` -ge 1 ]; then
echo "Yes, you are"
fi




No comments:

Locations of visitors to this page