Bash script for searching a specific word in terminal output

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Bash script for searching a specific word in terminal output



I'm trying to implement a bash script who supposed to search for a word in a Python script terminal output.



The Python script doesn't stop so "&" in the end of the command is needed but the "if [ $? == 0 ] ; then" condition doesn't work.



How it can be solved?



Thanks, Gal.


#!/bin/bash

#Check if Pixhawk is connected
PORT=/dev/ttyPixhawk

end=$((SECONDS+3))

not_exists=f
/usr/local/bin/mavproxy.py --daemon --non-interactive --master=$PORT | grep 'Failed' &> /dev/null &

while [ $SECONDS -lt $end ] ; do
if [ $? == 0 ] ; then
not_exists=t
fi
sleep 1
done

if [ $not_exists=t ] ; then
echo "Not Exists"
else
echo "Exists"
fi

kill $(pgrep -f '/usr/local/bin/mavproxy.py')





why don't you sleep 3 in first place instead of sleep 1. Any way you are checking for [ $SECONDS -lt $end ]. If you do this, you don't need the while loop
– Ashish Singh
48 mins ago


sleep 3


sleep 1


[ $SECONDS -lt $end ]




2 Answers
2



Bash doesn't know anything about the output of background commands. Check for yourself with [ 5444 -lt 3 ] & echo $?.


[ 5444 -lt 3 ] & echo $?



your if statement wouldn't work in any case because $? checks for the return value of the most recent previous command, which in this case is your while loop.


if


$?


while



You have a few different options. If you're waiting for some output, and you know how long it is in the output until whatever target you're looking for occurs, you can have the python write to a file and keep checking on the file size with a timeout for failure.



You can also continue with a simple timed approach as you have where you just check the output after a few seconds and decide success or failure based on that.



You can make your python script actually end, or provide more error messages, or write only the relevant parts to file that way.



Furthermore, you really should run your script through shellcheck.net to notice more problems.



You'll need to define your goal and use case more clearly to get real help; all we can really say is "your approach will not work, but there are definitely approaches which will work"



You are checking the status of grep command output inside while loop using $?. This can be done if $? is the next command to be fired after grep and if grep is not a back-group process . But in your script, $? will return the status of while [$SECONDS -lt $end ]. You can try to re-direct the output to a temp file and check it's status


grep


$?


$?


$?


while [$SECONDS -lt $end ]


/usr/local/bin/mavproxy.py --daemon --non-interactive --master=$PORT | grep 'Failed' &> tmp.txt &
sleep 3

# If file exists and it's size is greater than 0, [ -s File] will return true
if [ -s tmp.txt ]; then
echo 'pattern exists'
else
echo 'pattern not exists'
fi






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs