Help with finding last line of file: if statement depending on that line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with finding last line of file: if statement depending on that line.
# 1  
Old 05-15-2012
Help with finding last line of file: if statement depending on that line.

Good morning,
My first time actually posting in this forum, though I have used this forum to help with numerous projects.

I am trying to figure out why my if statement does not work. I have a file where a line is inputted every 15 seconds. I want this if statement to check what the last line is, and if it "X" to not do anything, but if the line is anything else, to execute a command. My problem is that it executes the command regardless what the last line in the file is.

Code:
if [ tail -1 musiclog.txt = 'Lecrae Up' ];

then 

echo OK

else
gnome-terminal -e "pkill mplayer"

fi

This is probably not the most elegant way in the least to accomplish this task. So any guidance of how to proceed is appreciated.

Last edited by methyl; 05-15-2012 at 01:08 PM.. Reason: changed quote to code tags
# 2  
Old 05-15-2012
What Operating System and version are you running and what Shell do you use?
# 3  
Old 05-15-2012
Hi Shanrunt,

I would use grep, the -q option suppress output and <(...) is a command redirection.
Code:
$ cat infile
1
2
3
4
5
6
7
$ grep -q '6' <(tail -1 infile) && echo "Found" || echo "NOT found"
NOT found                                                                                                                                                                                                                                    
$ grep -q '7' <(tail -1 infile) && echo "Found" || echo "NOT found"
Found

# 4  
Old 05-15-2012
Ubuntu 12.04 bash shell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

4. Shell Programming and Scripting

Change nth depending on matiching a pattern in Y line

Hi, I have below file, each line that starts with /* marks the beginning of the a new job. /* ----------------- cmdsMlyMoveTPMPLANTJ ----------------- UNIX_JOB CMMM002J CMDNAME /home2/proddata/bin/moveTPMPLANT.sh AGENT CMDSHP USER proddata AFTER CMMU001J /* "DDRG monthly... (13 Replies)
Discussion started by: varun22486
13 Replies

5. Shell Programming and Scripting

getting timestamp of a file and if it was accessed today then finding a line in it

i have my files and the variables value extracted from db is taken as in1=slot0312 in2=best in3=it is :veryliong/fine as varibles.. i have a folder stuctures in my unix machine as : /2011/hand_sl0312/best/HOD/file1.txt /2011/hand_sl0312/happy/HOD/file1.txt... (1 Reply)
Discussion started by: rajniman
1 Replies

6. Shell Programming and Scripting

toupper or tolower case of first letter of the line depending on another file

Hi I would like to read if the first letter of a line in a first file (gauche.txt) is uppercase or lowercase, and change consequently the first letter of the corresponding line in the second file (droiteInit.txt). I have done this but it won't work (I launch this using gawk -f... (16 Replies)
Discussion started by: louisJ
16 Replies

7. Shell Programming and Scripting

finding the line number of a particular line in a file

Hi Frnds, I need to find the line number of a particular line in a file and store that line number to a variable. if a file named myfile contains following look at the sun look at the moon look at the star look at the ocean i need to get the line number of the line 'look at the... (3 Replies)
Discussion started by: mvignesh
3 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

Finding line with highest number in a file

Hi All, My file looks some thing like this, File 1: - A 10 B 30 C 5 D 25 E 72 F 23 now my requirement is to find the line with highest number in it, i;e the result should be E 72 Thanks in Advance (1 Reply)
Discussion started by: balu_puttaganti
1 Replies

10. Shell Programming and Scripting

Finding a certain string on each line in a file

Hi, I need a script to get every line from a file where there are less then 17 ; on a line. Thank's (5 Replies)
Discussion started by: VODAFUN
5 Replies
Login or Register to Ask a Question