Manually iterating a line of a file | bash
bash, iteration, loops, shell
Solution
Instead of using a for loop to read through the file you should maybe read through the file like so.
#!bin/bash
while read line
do
do_something_to_line($line)
done < "your.file"
Problem
I could do this in any other language, but with Bash I've looked far and wide and could not find the answer. I need to manually increase `$line` in a script. Example: ``` for line in `cat file` do foo() foo_loop(condition) { do_something_to_line($line) } done ``` If you notice, every time the `foo_loop` iterates, `$line` stays the same. I need to iterate `$line` there, and make sure the original for loop only runs the number of lines in file. I have thought about finding the number of lines in file using a different loop and iterating the line variable inside the inner loop of `foo()`. Any ideas? EDIT: Sorry for being so vague. Here we go: I'm trying to make a section of my code execute multiple times (parallel execution) ``` Function foo() # Does something for line in `cat $temp_file`; foo($line) ``` That code works just fine, because `foo` is just taking in the value of line; but if I wanted to do this: ``` Function foo() # Does something for line in `cat $temp_file`; while (some condition) foo($line) end ``` `$line` will equal the same value throughout the while loop. I need it to change with the `while` loop, then continue when it goes back to the `for`. Example: ``` line = Hi foo{ echo "$line" }; for line in `cat file`; while ( number_of_processes_running -lt max_number_allowed) foo($line) end ``` If the contents of file were ``` Hi \n Bye \n Yellow \n Green \n ``` The output of the example program would be (if max number allowed was 3) ``` Hi Hi Hi Bye Bye Bye Yellow Yellow Yellow Green Green Green. ``` Where I want it to be ``` Hi Bye Yellow Green ``` I hope this is better. I'm doing my best to explain my problem.