Thursday 10 November 2016

While Read Loop

Cited from For and Read-While Loops in Bash

while read line
do
  echo "$line"
done < list-of-dirs.txt

or 

while read line
do
  echo "$line"
done < <(cat list-of-dirs.txt)

Cited from While loop

while IFS= read -r line
do
  command1 on $line
  command2 on $line
  ..
  ....
  commandN
done < "/path/to/filename"

or

while IFS= read -r field1 filed2 field3 ... fieldN
do
  command1 on $field1
  command2 on $field1 and $field3
  ..
  ....
  commandN on $field1 ... $fieldN
done < "/path/to dir/file name with space"

"IFS is used to set field separator (default is while space). The -r option to read command disables backslash escaping (e.g., \n, \t). This is failsafe while read loop for reading text files."


No comments:

Post a Comment