0

I have a problem with loop , I used while in my script as below :

while read LINE 
do {
}
done

but the problem is each time I should press enter while running the script, and it supposed to do this 50000 time ! how could I change this to a loop that runs automatically ?

could I count the length of my file using : lenght=`wc -l < myfile and then use it ? or any better solution ?

5
  • If you want to read lines from a file, then why are you trying to read from the console?
    – Massimo
    Sep 22, 2011 at 20:09
  • 2
    As a general rule of thumb you will get the best answer if you explain what you are actually trying to do. Sep 22, 2011 at 20:13
  • so, questions is how i must enter 50000 lines without enters? Sep 22, 2011 at 20:44
  • As @KyleBrandt said: what are you actually trying to do in a while loop?
    – quanta
    Sep 23, 2011 at 5:05
  • I work on a log file and it has 50000 rows , I wrote commands in while loop , but it needs enter for each row and it's impossible !
    – matarsak
    Sep 23, 2011 at 7:45

3 Answers 3

1

You can use a for loop?

for i in {1..5}
  do
  echo "I have $i bottle(s) of Mountain Dew"
done

I have 1 bottle(s) of Mountain Dew
I have 2 bottle(s) of Mountain Dew
I have 3 bottle(s) of Mountain Dew
I have 4 bottle(s) of Mountain Dew
I have 5 bottle(s) of Mountain Dew

You can even loop backwards!

for i in {5..1}
  do
  echo "I have $i bottle(s) of Mountain Dew"
done

I have 5 bottle(s) of Mountain Dew
I have 4 bottle(s) of Mountain Dew
I have 3 bottle(s) of Mountain Dew
I have 2 bottle(s) of Mountain Dew
I have 1 bottle(s) of Mountain Dew
0

your_script < 5000_lines.dat

In your while you should check for "not EoF" and put the read into the loop.

0

Try this:

yes "" | while ....

That sends a stream of newlines that your while-body can use.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .