******************************************
* Description: Linux Shell Scripting Tips
* Date: 09:21 AM EST, 09/21/2018
******************************************
<1> Reading a file line by line:
|
|__ $ #!/bin/bash
while read var_each_line
do
echo $var_each_line
done < /home/emeralit/target_file_name.txt
<2> Advanced reading file line by line:
|
|__ # IFS='' (or IFS=) prevents leading/trailing whitespace from being trimmed.
|
|__ # "-r" prevents backslash escapes from being interpreted.
|
|__ # "||" [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n (since read returns a non-zero exit code when it encounters EOF).
|
|__ $ #!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Text read from file: $line"
done < /home/emeralit/target_file_name.txt
Reference:
|
|__ o. https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable
Your Comments