LinuxQuestions.org
Review your favorite Linux distribution.
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 06-29-2008, 01:00 AM   #1
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Rep: Reputation: 15
variable in a tail command


hey there all

i'm writing a script that outputs the last X bytes of results in a log file ... so im using the -c flag with my tail command... pretty simple, except i don't know that the X number will be beforehand, so i have to figure it out by comparing the log file size over a length of time (say 30 seconds)

so i take the log file size at time 0, wait 30 seconds and get the size of the logfile again (which will now be larger as there is always activity on the log file).... i then minus the two numbers to find out how much new information has been written to the log file in the last 30 seconds.... i save this number to a variable called $sizediff... i then want to tail the last $sizediff bytes from the log file, and output these results to a new file called templog

something like this:
tail -$sizediffc logfile > templog

when i run the script it just hangs at the above command... if i replace the $sizediff with an actual number, it of course then works... the problem is, is that i don't know what that number will be beforehand, so i need to use a variable

if i run:
tail -$sizediff c logfile > templog

i then get the following error: tail: invalid option -- 7 ... the $sizediff variable would be 720 at this point, so its getting the variable correctly, but because the 'c' isn't beside it liek it should be, it doesn't know what to do with the flag

does anybody know a way around this? where you can use a variable in a command?

Last edited by jadeddog; 06-29-2008 at 01:03 AM.
 
Old 06-29-2008, 01:24 AM   #2
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
nm, i figured it out... all i needed to do was put the variable in quotations, so:
tail -"$sizediff"c maillog > templog

i can't believe i didn't try that before posting
 
Old 06-29-2008, 01:40 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I see no need for quotes:

$ tail -c -4 /usr/share/dict/words
ZZZ
$ sizediff=4
$ tail -c -$sizediff /usr/share/dict/words
ZZZ
 
Old 06-29-2008, 09:50 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,954

Rep: Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634
Or

tail -${sizediff}c maillog > templog
 
Old 06-29-2008, 10:35 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quote:
Originally Posted by chrism01 View Post
Or

tail -${sizediff}c maillog > templog
Using arguments this way has some unforeseen traps. Consider if sizediff is negative:

Code:
$ sizediff=-10
$ tail -${sizediff}c /usr/share/dict/words 
tail: unrecognized option `--10c'
Try `tail --help' for more information.

$ tail -c $sizediff /usr/share/dict/words 
z
zZt
ZZZ
It is generally best to adhere to standard option/argument behavior:

-opt arg
--longopt arg
 
  


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
tail command help munna_dude Programming 4 10-08-2007 03:30 AM
roo tail, make Error 127, /bin/sh: rman: command not found DraaX Linux - Desktop 4 08-28-2006 10:57 PM
How do I turn my variable into a command? spiffytech Linux - Software 3 12-29-2005 01:30 PM
Help with 'tail' command! Beppe83 Linux - Software 3 04-07-2004 09:11 AM
Howto end the "tail" command??? Schmurff Linux - Newbie 5 03-03-2004 06:32 AM

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

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