ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.