LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-16-2009, 11:57 PM   #1
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

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

Last edited by WhisperiN; 10-16-2009 at 11:58 PM.
 
Old 10-17-2009, 01:02 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
MYVAR=$(tail ... | grep ...)
 
Old 10-17-2009, 06:45 AM   #3
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
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 :-)
 
Old 10-17-2009, 06:55 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 10-17-2009, 08:33 AM   #5
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
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.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
variable in a tail command jadeddog Programming 4 06-29-2008 09:35 PM
Help with variable assigning in shell scripts fultron Linux - Newbie 2 02-16-2007 04:32 PM
Trouble assigning value to variable with fatsheep Programming 1 11-07-2006 07:44 PM
getting rpoblem in assigning value to variable in if construct Ankit mittal Programming 9 01-07-2006 09:12 AM
assigning commands to a variable aunquarra Linux - General 5 01-04-2006 07:58 AM


All times are GMT -5. The time now is 05:11 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