LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 04-07-2009, 09:22 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
How do I get the grep output 0 or 1 and test it?


I am trying to use a shell script to find a string in a file and do something when found.

For example:

(sudo pppd call gsm > pppdout &)

FOUND=1
while [ $FOUND -eq 1 ]; do
FOUND = $(grep "Script" pppdout)
sleep 1
done

wait


What should happen is pppd will start in a different process and stream it's output to pppdout. pppdout should be created in the current folder. Then the script should periodically check the pppdout file for the string Script (which eventually will appear, some seconds later) and when found exit the script. Ultimately the script will do something useful when the text is found. However, the output from the program is a repeating: 'scriptname.sh: 12: FOUND: not found'

Where scriptname.sh would be the name of your script and 12 refers to the line with 'done'.

Why does grep not find the text, or at least why deos my script not check the grep output correctly?

Thanks in advance.
 
Old 04-07-2009, 09:41 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Would this work for you?
Code:
#!/bin/bash

while [ true ]
do
        grep -q "Script" pppdout

        if [ $? -eq 0 ]
        then
                break
        fi

        sleep 1
done

Last edited by dwhitney67; 04-07-2009 at 09:42 AM.
 
Old 04-07-2009, 09:56 AM   #3
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Thumbs up

Yes yours does work, thanks.

Now I understand your version, but can you explain what is wrong with my version please? Can you not use a programs output i.e. $(grep...) as I did?
 
Old 04-07-2009, 10:06 AM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by DEF. View Post
Now I understand your version, but can you explain what is wrong with my version please? Can you not use a programs output i.e. $(grep...) as I did?
If you want the return code from grep, you need to set FOUND to $?. You were storing the string produced by $(grep "Script" pppdout), which was empty. Since FOUND was set to "", it was no longer equal to 1, and the loop exited. You really wanted
Code:
(sudo pppd call gsm > pppdout &)

FOUND=1
while [ $FOUND -eq 1 ]; do
    sleep 1
    grep "Script" pppdout
    # Note the lack of white space around the equal sign.
    # If there is white space after FOUND, the shell interprets
    # it as a command.
    FOUND=$?
done

wait
I put the sleep before the grep and assignment because it makes no sense to sleep after you have updated the loop conditional.
 
Old 04-07-2009, 10:08 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by DEF. View Post
Yes yours does work, thanks.

Now I understand your version, but can you explain what is wrong with my version please? Can you not use a programs output i.e. $(grep...) as I did?
Apparently not. Seriously, I do not know of another way to get the status of the command.
 
Old 04-07-2009, 10:11 AM   #6
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Exclamation

Thanks you guys - I suck at shell programming
 
Old 04-07-2009, 10:11 AM   #7
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by dwhitney67 View Post
Apparently not.
I think I got him sorted.
 
Old 04-07-2009, 01:16 PM   #8
The_Kernel
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
Quote:
Originally Posted by dwhitney67 View Post
Apparently not. Seriously, I do not know of another way to get the status of the command.
Well if grep did match anything it would have some output, and if it didn't then no output, so you could just as well test if the output of the command is zero length. Also it's unnecessary to have the "if" statement separate from the grep command. You could just do 'if grep -q "Script" pppdout' to the same affect.
 
Old 04-07-2009, 01:32 PM   #9
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
In loops such as
while [ .. ];do
...
done
the return code of the 'test' command is being used to control the loop.
Other commands can be used instead of 'test'.
So this works too
Code:
until grep -q "Script" pppdout;do
  sleep 1
done
 
Old 04-07-2009, 08:42 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The original script had a syntax error; there must be no spaces around the "=" in assignments.
Code:
FOUND = $(grep "Script" pppdout)
So that code will execute a program called FOUND with 2 arguments: "=" and the output of grep "Script" pppdout.

If you take out the spaces you would then have the problem David1357 mentioned.
 
Old 04-07-2009, 09:45 PM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
It can be a little easier than that.
Code:
if test "`grep -c "pattern" "file"`" -ge 1; then
  echo found
else
  echo not found
fi
Kevin Barry
 
  


Reply



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
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
Grep on output of command returns all output traigo Linux - Newbie 2 02-24-2009 05:15 PM
grep for string and test if successfully or not B-Boy Programming 3 02-17-2009 01:43 PM
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-30-2009 11:21 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration