LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration