LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   assigning commands to a variable (http://www.linuxquestions.org/questions/linux-general-1/assigning-commands-to-a-variable-399213/)

aunquarra 01-03-2006 06:27 PM

assigning commands to a variable
 
I'm having trouble with a shell script I wrote that parses through logs using awk and tail's "follow" option. I wanted to add an optional argument to search (using grep), and I'm having trouble.

Code:

if [ "$1" = "--logger" ]
then
    action="Watching"
    cmd="tail -f /var/log/foobar.log";
    if [ "$2" ]
    then
      action="Watching for '$2' in";
      cmd="tail -f /var/log/foobar.log | grep '$2'";
    fi

echo $action "Logs:";

$cmd | awk 'BEGIN {.....

If I echo the $cmd, it's right. But for some reason, tail is interpreting "|", "grep", and the argument as files to also tail. Am I missing something obvious?

bigrigdriver 01-03-2006 07:38 PM

Don't know if this will fix it, but. You have two if statments and only one fi. The script should crash instead of run: unmatched if error. The usual syntax is:
if (something);
then (do something)
elif
(do something else)
fi

aunquarra 01-04-2006 03:31 AM

I have the "fi" in there. It's just on the other side of a huge awk script (hence the "...").

berbae 01-04-2006 04:30 AM

You can bypass the problem, for example :
Code:

if [ "$1" = "--logger" ]
then
    action="Watching"
    cmd="tail -f /var/log/foobar.log"
    if [ "$2" ]
    then
      echo $action " for '$2' in Logs:"
      $cmd | grep "$2" | awk -f <program-file>
    else
      echo $action "Logs:"
      $cmd | awk -f <program-file>
    fi
fi

where <program-file> is the name of the 'huge awk script' put in a file.

Another possibility would be to integrate the second filter into the awk script.

aunquarra 01-04-2006 07:01 AM

I actually really like the idea of sticking the awk script in its own file. That worked well. Now my code appears as...
Code:

<clip>    if [ "$2" ]
    then
      echo "Watching for '$2' in log";
      echo "tail -f /var/log/foobar.log | grep '$2' | awk -f /root/foolog.awk";
    else
      echo "Watching log";
      tail -f /var/log/foobar.log | awk -f /root/foolog.awk;
    fi

Now the script executes properly, but the grep seems to eliminate every line. When I run the commands manually ("tail -f /var/log/foobar.log | grep 'test' | awk -f /root/foolog.awk"), I get the same result (even if I grep for "" [nothing]).

It should be possible for me to work the search into the awk script by making it 'next' if it doesn't see the search string in each iteration, but I'm not sure how to do that. I know you can use something like "/[^a-zA-Z]/" to exclude anything that has letters, but I'm not sure how to go about doing the same thing with a string.

aunquarra 01-04-2006 07:58 AM

Hah, I guess I should try the obvious first. I made it work by just parsing each line and using index() to see if the search value is there. The search string is passed to awk through the -v declaration and is set to "" if left blank.

Code:

BEGIN {

}
{
  if (index($0, searchfor) == 0)
  {
    next;
  }
}



All times are GMT -5. The time now is 08:00 PM.