LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-03-2006, 06:27 PM   #1
aunquarra
LQ Newbie
 
Registered: Jan 2005
Distribution: Slackware 10.1
Posts: 28

Rep: Reputation: 15
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?
 
Old 01-03-2006, 07:38 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,745

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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
 
Old 01-04-2006, 03:31 AM   #3
aunquarra
LQ Newbie
 
Registered: Jan 2005
Distribution: Slackware 10.1
Posts: 28

Original Poster
Rep: Reputation: 15
I have the "fi" in there. It's just on the other side of a huge awk script (hence the "...").
 
Old 01-04-2006, 04:30 AM   #4
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
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.

Last edited by berbae; 01-04-2006 at 08:47 AM.
 
Old 01-04-2006, 07:01 AM   #5
aunquarra
LQ Newbie
 
Registered: Jan 2005
Distribution: Slackware 10.1
Posts: 28

Original Poster
Rep: Reputation: 15
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.
 
Old 01-04-2006, 07:58 AM   #6
aunquarra
LQ Newbie
 
Registered: Jan 2005
Distribution: Slackware 10.1
Posts: 28

Original Poster
Rep: Reputation: 15
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;
  }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting rpoblem in assigning value to variable in if construct Ankit mittal Programming 9 01-07-2006 09:12 AM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 12:40 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM


All times are GMT -5. The time now is 03:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration