Read 2 lines from File, Run Command based off output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read 2 lines from File, Run Command based off output
# 1  
Old 07-17-2012
Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list.

So.... To get the last two numbers, I can have the script do

tail -2 filename.txt

But where I run into trouble is as follows. If these last two numbers, retrieved via tail, are Greater than 400, I want to run a command. The command is irrelevant, so lets just make something up: touch filename.txt

If it helps, it doesnt necessarily have to do > 400. It would still meet my needs if it did something more like. If = 400 or 401 or 402 or 403 or 404 or 405 as there will really only be that number set, with nothing over 405.

So the script can function with whatever is easier.

Thanks a ton! Smilie
# 2  
Old 07-17-2012
Try:
Code:
tail -2 filename.txt | tr "\n" " " | read a b
if [ $a -gt 400 ] && [ $b -gt 400 ]; then 
  your command 
fi

# 3  
Old 07-17-2012
Thanks for the post Bartus. When I run this, it says

./filescript.sh: line 2: [: -gt: unary operator expected

So before re-posting I tried to find the answer via Google again. No luck. But I have identified it does not seem to be getting variables for a and b. If I do an echo of these, I am getting null (or a space, can't tell).

The current output of

Code:
 tail -2 http.txt | tr "\\n" " "

Is showing as: "48 36 "
Without the quotes of course. So it seems like it may not be properly removing the spaces perhaps? Not sure.

Thanks again
# 4  
Old 07-17-2012
Your shell is likely running the read in a subshell. The variables are set there, but unset when you try to use them in the parent shell. You could try either writing the pipeline to a temp file and reading that, using process substution/here-string/here-doc to redirect the result of a command substitution into the read, or doing all the work with those variables in the subshell where read runs.

Regards,
Alister
# 5  
Old 07-19-2012
Afraid I am still finding myself lost. Here is what I have right now. I tried creating a second file and doing the read on that, but still no variable being passed in my echo.

Code:
tail -2 filename.txt | tr "\n" " " > filename2.txt
tail filename2.txt | read a b
echo $b
if [ $a -gt 400 ] && [ $b -gt 400 ]; then
  touch filename.txt
fi

# 6  
Old 07-19-2012
Try:
Code:
tail -2 filename.txt | 
{
  read a; read b
  echo $b
  if [ $a -gt 400 ] && [ $b -gt 400 ]; then
    touch filename.txt
  fi
}

# 7  
Old 07-19-2012
That did the trick. Thanks Scrutinizer! And of course, thanks again to the others who provided input. Have a good day.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

Read in txt file and run a different command for each line

hi, i'm trying to write a tcsh script that reads in a text file (one column) and the runs a different command for each line of text. i've found lots of example commands for bash, but not for tcsh. can anyone give me a hint? thanks, jill (8 Replies)
Discussion started by: giuinha
8 Replies

3. Shell Programming and Scripting

Different output based on run method

Sample Data #cat /mylist.txt /raid/job/123 /raid/job/456 /raid/job/789 # Script #cat /myfind.scr #!/bin/bash touch /nojob.txt touch /job.txt unset dirfolder for dirfolder in `/bin/cat /mylist.txt`;do if then echo $dirfolder >>/job.txt else echo $dirfolder >>/nojob.txt (3 Replies)
Discussion started by: crowman
3 Replies

4. Shell Programming and Scripting

Read file and run a command

Hi I have jobs (some 1000) defined in a file and I want to read those jobs and run a a command. For example: jobs.txt abc efg I want to read the entire file and run the following command Delete -JOB "abc" Deleteing abc... Delete -JOB "efg" Delete efg... Can somebody help me... (4 Replies)
Discussion started by: karan8810
4 Replies

5. Shell Programming and Scripting

Run this grep every 10 minutes and do something based on the output

OS : Red Hat Linux 6.4 Shell : Bash We have a file called status.txt which will have only 1 line. The content will be the string "Processing" for most of the day. # cat status.txt Processing #I want to write a shell script (notify.sh) which will be executing a grep every 10 minutes . ... (7 Replies)
Discussion started by: kraljic
7 Replies

6. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

7. Shell Programming and Scripting

Output block of lines in a file based on grep result

Hi I would appreciate your help with this. I have a output file from a command. It is broken based on initial of the users. Exmaple of iitials MN & SS. Under each section there is information pertaining to the user however each section can have different number of lines. MY challenge is to ... (5 Replies)
Discussion started by: mnassiri
5 Replies

8. UNIX for Dummies Questions & Answers

Read workstation list from file und run command

Hi, how do I read in a file which includes a list of workstations and then run a command for each workstation ? I am unclear which command to use to read in , or is this not possible ? Thanks, (3 Replies)
Discussion started by: manni2
3 Replies

9. UNIX for Dummies Questions & Answers

Cat command drops lines in output file

I use the cat command to concatenate text files, but one of the rows I was expecting doesn't display in the output file. Is there a verbose mode\logging mechanism for the cat command to help me investigate where the lines I was expecting are going?? cat 7760-001_1_*_06_*.txt | grep -v... (1 Reply)
Discussion started by: Xin Xin
1 Replies

10. Shell Programming and Scripting

read line and run a different command according to the output

Hi. I'm trying to write a script that reads a line on a file and runs a different command for a different line output. For example, if it finds the word "Kuku" on the line it sends mail to Kuku@kuku.com. Otherwise, it sends mail to Lulu@lulu.com. TIA. (2 Replies)
Discussion started by: Doojek9
2 Replies
Login or Register to Ask a Question