LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Assigning (tail) results to a variable (https://www.linuxquestions.org/questions/programming-9/assigning-tail-results-to-a-variable-762516/)

WhisperiN 10-16-2009 11:57 PM

Assigning (tail) results to a variable
 
Hello all :-)
I hope you all doing great.

I'm writing a script which in some part of it read some log file with the command ( tail ).

I'd like to assign the result of tail | grip to a variable for some extra process.

Here is an example of the command:

Code:

tail /var/log/somefile -n20 | grep something
What I'd like to do, is to store the result of that command into a variable instead of printing it on screen.


Any help will be more than appreciated :)

Thanks..

acid_kewpie 10-17-2009 01:02 AM

MYVAR=$(tail ... | grep ...)

WhisperiN 10-17-2009 06:45 AM

Oh..
Great..!!

The funny thing, is I really know that..
But I tried so many time, with so many ways..
And it was always failing..


After your answer, I got so sure that I was right, so I started digging some more deeper into the script.
I discovered I was forgetting a part of a commented out command Un-commented.
That was right before the tail command.

SO, thanks you for your help :-)

pixellany 10-17-2009 06:55 AM

Depeding on what exactly you are doing, assigning text to a variable may not be the best idea. You can also send the results of any command to a file using redirection, e.g.:
tail /var/log/somefile -n20 | grep something >filename

WhisperiN 10-17-2009 08:33 AM

Hello pixellany,

Yes, in fact I know about redirecting the results to a file.
The point is, I intended to assign the value to variable because the script will be taking actions depending on the results loaded at that variable.

Let me illustrate the meaning using an example..

Let's assume we have an application running, and it's logging its status on a regular basis.
Let's assume that application logs look like this:
Code:

Oct 17 04:09: Processing Started..
Oct 17 05:09: Still working, Every thing is Alright.
Oct 17 06:09: Still working, Every thing is Alright.
Oct 17 07:09: Oops..!! fatal error found..!!
Oct 17 07:09: Processing Stopped..!!
Oct 17 08:09: Processing Started..
Oct 17 09:09: Still working, Every thing is Alright.
Oct 17 10:09: Still working, Every thing is Alright.
Oct 17 11:09: Done, completed all tasks successfully.

My script is meant to read that log file, and take action according to the value..
So, an example will be like this:

Code:

#!/bin/bash
result=$(tail /var/log/application_log_file -n1 | grep 'successfully')
        if [[ $result != '' ]]; then
                # Do some action, like halting the machine:
                halt -p
                exit 0
        else
                result=$(tail /var/log/application_log_file -n1 | grep 'fatal error found')
                if [[ $result != '' ]]; then
                # Do some other action, like restarting the application:
                application restart
                exit 0
                fi
        fi


Kindly, Give me your opinion if you see or recommend something in such a case..!! :-)


Thanks a lot.


All times are GMT -5. The time now is 12:03 AM.