Script does not read the last line of text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script does not read the last line of text file
# 1  
Old 03-18-2009
Script does not read the last line of text file

Hello,
I have got a script that reads a text file, and have got three problems that I an struggling with.

1. The script does not read the last line in the text file
2. within the second 'elif' within the script I included a 'break' - the script runs successfully (except for the first problem) , but if a situation occurs where the second 'elif' is executed the echo message is shown and all seems to work well, but if I try and run the script a second time with a condition that does not satisfy the second 'elif' the echo from the second 'elif' is still being shown' - this problem ony happens after the second 'elif' is executed at least once.

3. Instead of breaking out of the code within the second 'elif' I wanted the user to be taken back to the first echo where they are asked to enter the 'Test Day'

I tried goto but after doing some research I realise that I am using Korn Shell and I cann use goto

CAN ANYONE HELP?

===sCRIPT ====

#!/bin/ksh

echo 'Please enter the Test Day or 0 for all days: '
read x
count=0
while read line
do
echo $line | read a b c d
if [ "$a" = "$x" ] ; then

( IFS=-
printf ' 1\n 1\n 0\n 0\n 1\n 1\n 7\n 0\n'
printf ' %s\n' $c
printf ' 1\n 1\n 0\n 0\n 0\n N\n 1\n 100\n' ) |
"$LOTO"/bin/loto_tsim > /dev/null 2>&1

let count=count+1
echo "Wager Number ${count} is:" $c

elif [ "$x" = "0" ] ; then

( IFS=-
printf ' 1\n 1\n 0\n 0\n 1\n 1\n 7\n 0\n'
printf ' %s\n' $c
printf ' 1\n 1\n 0\n 0\n 0\n N\n 1\n 100\n' ) |
"$LOTO"/bin/loto_tsim > /dev/null 2>&1

let count=count+1
echo 'Your wager is: ' $c


elif [ "$a" != "$x" ] || [ "$a" != "0" ] ; then

echo 'Sorry The Test Day Entered Does Not Exist'
break


fi



done < LottWagers2.txt



=======text file data= ====

3 1 01-02-27-28-29-30 (99) Both 1
1 1 01-31-32-33-34-35 (99) Both 3
1 1 03-06-09-10-20-21 (99) Both 3
1 1 05-31-32-33-34-35 (99) Both 3
1 1 07-06-09-10-20-21 (99) Both 3
4 1 05-07-08-09-10-25 (99) Both 4
4 1 01-02-10-11-24-25 (99) Both 7
4 1 01-02-31-32-33-34 (99) Both 5
4 1 04-02-31-32-33-34 (99) Both 5
# 2  
Old 03-18-2009
I'm just a starter in UNIX Scripting but think you could try the following:
1. Reason why it doesn't read the last line might be due to missing newline. Open your text file again in VI editor and save and quit using ":wq". That will take care of the newline character issue.
2. In the IF-ELSE block, instead of using "=" try using "==" or "-eq"
3. Put the whole code in a while loop. Use some flag say gotoMenu = true and keep going back to MENU till the flag is set to FALSE.

Hope this helps.

regards,
Arun.
# 3  
Old 03-18-2009
arunsoman80,

Thank you very much for your help, you suggestion for problem number 1 works fine. I will try suggestion 2 & 3 and tell you if it works.

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Shell Programming and Scripting

How to read the first work of each line from text file?

Hi ppl. I want to read the first word of each line of a text file to a variable in a bash script. Can anyone help pls, thanks. (6 Replies)
Discussion started by: cannot
6 Replies

4. Shell Programming and Scripting

how read specific line in a file and write it in a new text file?

I have list of files in a directory 'dir'. Each file is of type HTML. I need to read each file and get the string which starts with 'http' and write them in a new text file. How can i do this shell scripting? file1.html <head> <url>http://www.google.com</url> </head> file2.html <head>... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

6. Shell Programming and Scripting

Read text file line by line

Hi everyone, I am writing a BASH shell script that I will use to process data files. I have a text file that contains the names of the files to be processed, for example: cat filenames.txt file1 file2 file3 file4 file5 What I would like to do is set up a FOR loop within my script... (2 Replies)
Discussion started by: msb65
2 Replies

7. Shell Programming and Scripting

How to read specific line of text from a Script and send email notification

Hi ! I am a newbie and never officially wrote a shell script before. The requirement for this script is : 1) Read a file called 'bpm.log' and identify if it has a specific text such as 'this is the text'. Its a static value and that is the only text we need to read. 2) If that... (2 Replies)
Discussion started by: atechcorp
2 Replies

8. Shell Programming and Scripting

Read random line from a text file

I have a text file with hundreds of lines, i wish to run a script and reads a random line to pass it to another command line such as: for line in `cat file |grep random line`; do echo $line |mail my@example.com ; done thank you (6 Replies)
Discussion started by: Bashar
6 Replies

9. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

10. Shell Programming and Scripting

Read from text file misses first line

Hi! I need to read in the first line from a text file (which will only ever have one line in it), so I tried this.... while read line do echo $line done < $file But this wasn't returning anything. So I tired a different file, which had multiple lines of text in it, and it returned... (2 Replies)
Discussion started by: davewg
2 Replies
Login or Register to Ask a Question