Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question
    
If you want to read lines from a file, then why are you trying to read from the console? –  Massimo Sep 22 '11 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. –  Kyle Brandt Sep 22 '11 at 20:13
    
so, questions is how i must enter 50000 lines without enters? –  Korjavin Ivan Sep 22 '11 at 20:44
    
As @KyleBrandt said: what are you actually trying to do in a while loop? –  quanta Sep 23 '11 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 '11 at 7:45

3 Answers 3

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
share|improve this answer

your_script < 5000_lines.dat

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

share|improve this answer

Try this:

yes "" | while ....

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.