- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Help with grep and if
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 02:52 AM
тАО02-15-2007 02:52 AM
if [[ grep string $file ]]
then
file contains string
else
file does not contain string
fi
I got this but it errors on "]]"
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 02:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 03:02 AM
тАО02-15-2007 03:02 AM
Re: Help with grep and if
FOUND=`grep string $file`
if [[ -n $FOUND ]]
then
contains value
else
contains nothing
-n is non zero check whereas -z is a zero check
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 03:06 AM
тАО02-15-2007 03:06 AM
Re: Help with grep and if
a=`grep 09 $file`
if [ -n "$a" ]
then
echo file contains string
else
echo file does not contain string
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 03:21 AM
тАО02-15-2007 03:21 AM
Re: Help with grep and if
if grep 'string' "$file" 1>/dev/null 2>&1 ; then
echo "$file contains string"
else
echo "$file doesn't contain string, please add some"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 03:22 AM
тАО02-15-2007 03:22 AM
Re: Help with grep and if
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 03:34 AM
тАО02-15-2007 03:34 AM
Re: Help with grep and if
The 'grep' filter will return zero (0) if there are matches; one (1) if not. Hence:
# grep local /etc/hosts > /dev/null || echo "no matches!"
...is a terse way to meet your goal.
You can also count and return the number of matches with the '-c' switch. See the manpages for more information.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 04:17 AM
тАО02-15-2007 04:17 AM
Re: Help with grep and if
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 04:24 AM
тАО02-15-2007 04:24 AM
Re: Help with grep and if
# grep string file && echo file contains string || echo file does not contain string
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 12:47 PM
тАО02-15-2007 12:47 PM
Re: Help with grep and if
grep -q string $file
if [ $? -eq 0 ]
then
echo "string is in $file"
else
echo "string is not in $file"
fi
Your friends here are:
man test
man grep