Manually iterating a line of a file | bash Manually iterating a line of a file | bash shell shell

Manually iterating a line of a file | bash


Instead of using a for loop to read through the file you should maybe read through the file like so.

#!bin/bashwhile read linedo    do_something_to_line($line)done < "your.file"


Long story short, while read line; do _____ ; done

Then, make sure you have double-quotes around "$line" so that a parameter isn't delimited by spaces.

Example:

$ cat /proc/cpuinfo | md5sumc2eb5696e59948852f66a82993016e5a *-$ cat /proc/cpuinfo | while read line; do echo "$line"; done | md5sumc2eb5696e59948852f66a82993016e5a *-

Second example # add .gz to every file in the current directory: # If any files had spaces, the mv command for that line would return an error.

$ find -type f -maxdepth 1 | while read line; do mv "$line" "$line.gz"; done


You should post follow-ups as edits to your question or in comments rather than as an answer.

This structure:

while read linedo    for (( i=1; i<$max_number_allowed; i++ ))    do        foo $line    donedone < file

Yields:

HiHiHiByeByeBye...etc.

While this one:

for (( i=1; i<$max_number_allowed; i++ ))do    while read line    do        foo $line    done < filedone

Yields:

HiByeYellowGreenHiByeYellowGreen...etc.