LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-15-2007, 04:35 PM   #1
cornish
Member
 
Registered: May 2005
Location: Sussex, England
Distribution: Ubuntu 7.10
Posts: 131

Rep: Reputation: 15
Grep result passed to variable


Help

I'm am trying to create a script that checks a log for errors I want it to be nice and simple and this is what I am aiming for can any one help me??

Code:
#!/bin/bash

#Check the log

var1 = grep error test1.log
echo $var1

IF statement

if $var = "" ie empty
print no errors

else
there were errors
 
Old 11-15-2007, 04:45 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Code:
v1=`grep err_string file.log`
if [[ $? -eq 0 ]]
then
    echo "ERROR: $v1"
else
    echo "no err"
fi
I hope this isn't homework
 
Old 11-15-2007, 04:57 PM   #3
cornish
Member
 
Registered: May 2005
Location: Sussex, England
Distribution: Ubuntu 7.10
Posts: 131

Original Poster
Rep: Reputation: 15
No its not homework
I have done what you suggested and this is the output

checkbackups.sh: 10: [[: not found
no err


Quote:
#!/bin/sh

#Check the backup logs
v1=`grep rsync test1.log`
if [[ $? -eq 0 ]]
then
echo "Errors: $v1"
else
echo "no err"
fi
 
Old 11-15-2007, 04:58 PM   #4
cornish
Member
 
Registered: May 2005
Location: Sussex, England
Distribution: Ubuntu 7.10
Posts: 131

Original Poster
Rep: Reputation: 15
test1.log has errors
 
Old 11-15-2007, 04:59 PM   #5
cornish
Member
 
Registered: May 2005
Location: Sussex, England
Distribution: Ubuntu 7.10
Posts: 131

Original Poster
Rep: Reputation: 15
Ok I fixed that using only 1 open square bracket thanks for the help
 
Old 11-15-2007, 05:05 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Weird, 2 open/close ie [[ ]] is valid bash & ksh: see http://www.tldp.org/LDP/abs/html/testconstructs.html. Maybe your sh isn't symlinked to bash ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using find and grep - how to exclude a result ? graziano1968 Linux - General 13 12-04-2010 10:02 PM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 05:28 AM
how to grep within a script, and test result? bobbyr Programming 4 01-13-2004 12:11 PM
multiline grep result > multiple variable? mikshaw Linux - Software 5 12-25-2003 10:13 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM


All times are GMT -5. The time now is 05:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration