1

I am trying to use grep in a while loop that runs curl.

    i=1
    while read -a row
    do
      test $i -eq 1 && ((i=i+1)) && continue
      STRING=${row[2]}
      CURL=`curl ${row[0]}${row[1]} | ${grep '$STRING'}`
      echo $CURL
    done < <(echo "SELECT check_address, check_page, check_string FROM checks" | mysql monitor)

The problem is in the line "${grep '$STRING'}". Since grep needs to be grep 'text' I tried different ways like grep '$var', ${grep $var} couldn't make it work.

SQL Output:

    ${row[0]} = http://www.codeoasis.com/
    ${row[1]} = index.php
    ${row[2]} = Copyright

1 Answer 1

2

You don't need to capture the output of curl or grep:

i=1
while read -a row
do
  test $i -eq 1 && ((i=i+1)) && continue
  STRING=${row[2]}
  curl ${row[0]}${row[1]} | grep "$STRING"
done < <(echo "SELECT check_address, check_page, check_string FROM checks" | mysql monitor)

Note the use of double-quotes around $STRING, rather than single quotes.

Sign up to request clarification or add additional context in comments.

5 Comments

When i run it i still get empty response: [root@oasis monitor]# ./Monitor.sh % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 22248 0 22248 0 0 75002 0 --:--:-- --:--:-- --:--:-- 2450k
Please add the output of your SQL query to your question. Right now, it appears that the URL is fine (2450k returned), but that the requested string just isn't present.
its present if i type the string now with in a variable it works , the only problem is with grep that wont read the variable ( i echo it and its there )
output of SQL: ${row[0]} = codeoasis.com ${row[1]} = index.php ${row[2]} = Copyright
I Copied the code as your wrote it, unforunatly it still give me empty output [root@oasis monitor]# ./Monitor.sh % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 22248 0 22248 0 0 83854 0 --:--:-- --:--:-- --:--:-- 2450k [root@oasis monitor]#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.