Why does my script only read two lines of a file and not the third


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why does my script only read two lines of a file and not the third
# 1  
Old 01-30-2011
Why does my script only read two lines of a file and not the third

I'm learning about the read command and wrote this little script to read data from a file:
Code:
readfile()
{
 while read variable; do
echo $variable
done
}    
readfile < File.txt

I have three lines in File.txt; each a single word. The script only echoes the first two lines and drops the third one. Can anyone tell me why?

Also, what does the statment

if [ $variable ] mean? Does it mean 'if the value of variable exists....'?
# 2  
Old 01-30-2011
can you post your File.txt?This code runs okay on my laptop.
I think [ $variable ] means to test if the value of variable is null or unset,if so,evaluated as false.use [ -n $variable ] or [ -z $variable ] will be better.
# 3  
Old 01-30-2011
Sure can. Here is the file -- only three lines

Red
Blue
Green

It echoes Red and Blue but not green. Did it return all the lines in your file?
# 4  
Old 01-30-2011
--Works fine for me too:

Code:
$ cat testit
readfile()
{
  while read variable; do
echo $variable
done
}
readfile < File.txt
$ cat File.txt
one
two
three
$ ./testit
one
two
three

if [$variable ] wont work as intended when $variable contains a space eg variable="one two", best to stick with:
Code:
if [ -n "$variable" ]

# 5  
Old 01-30-2011
Never mind -- figured it out, although I feel like an idiot. Smilie

I saved the file without hitting return after the last word. Out of curiosity, can anyone tell me why that is?
Runs fine now.
# 6  
Old 01-30-2011
Quote:
Originally Posted by Straitsfan
I saved the file without hitting return after the last word. Out of curiosity, can anyone tell me why that is?
Runs fine now.
Smilie,interesting~Even without hitting return,i get three lines and if i hit return,it will print a blank line as expected.
# 7  
Old 01-30-2011
read returns a non-zero if it encounters a END-OF-FILE char and zero if it encounters a CR char. variables are populated with anything that is read to this point.

You should probably do something like:

Code:
readfile()
{
  while read variable; do
    echo "$variable"
  done
  echo -e "${variable}\c"
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Script to Read lines and print

Dears, I am trying to write a script to read the lines from a file in AIX Server. Below are the contents of the file. To explain, first part before = is path and second part is number. i am trying to write the script to print the path and count where count>100. any help must be appreciated... (3 Replies)
Discussion started by: sibbala
3 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

Read few last lines of a file

Hi, I have a txt file in below format - START 1 2 3 4 END START 5 6 7 8 END START 9 10 END (8 Replies)
Discussion started by: bhupinder08
8 Replies

4. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

5. Shell Programming and Scripting

Help with script to read lines from file and count values

Hi, I need some help with a script I'm trying to write. I have a log file containing references to a number of different webservices. I wish to write a script that will list the webservices with a count as to how many times they appear in the log. An example of the log file content: ... (2 Replies)
Discussion started by: gman2010
2 Replies

6. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

7. Shell Programming and Scripting

read some lines from file!!!

any idea please!!! I want to pick up all lines of "state" and "desc" from x files: ... # state blah blah blah blah ... .. # desc blah blah blah .... Thx Andy (7 Replies)
Discussion started by: andy2000
7 Replies

8. UNIX for Dummies Questions & Answers

Read lines from file

i have a problem on my bourne shell script. I want to read line by line and then stop when the line does not have letters or is an empty string. But i encounter an error at "while ". The error nessage is "test.sh: test: unknown operator line". Can anyone help me on this thanks :) (2 Replies)
Discussion started by: sagolo
2 Replies

9. UNIX for Dummies Questions & Answers

how to read lines one by one from a file

I have one file in which some commands have written line line i have to read lines from this file(file name passed as avariable) and then i have to execute these commands.. how can i do it? (5 Replies)
Discussion started by: bihani4u
5 Replies

10. Shell Programming and Scripting

Shell Script to read specific lines in a file

I have a file with contents as follows Record 1: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 5: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 6:... (5 Replies)
Discussion started by: varshanswamy
5 Replies
Login or Register to Ask a Question