!/bin/bash
if [ $# -ne 1 ] then echo "$0 filename" exit 0 fi
file=$1 while IFS= read -r line do echo "$line" done <"$file"
!/bin/bash
if [ $# -ne 1 ] then echo "$0 filename" exit 0 fi
file=$1 count=0 while IFS= read -r line do ((count++)) done <"$file" echo $count
((count+=2)) , ((count-=5))
!/bin/bash
file="/etc/passwd" while IFS=: read -r f1 f2 f3 f4 f5 f6 f7 do echo "Username: $f1, Shell: $f7, Home Dir: $f6" done <"$file"
!/bin/sh
FOR LOOP VERSION
for line in $(cat test.txt) do echo $line"nospace" "space between words" done
!/bin/bash
Read file lines into an array
declare -a ARRAY
Link filedescriptor 10 with stdin
exec 10<&0
stdin replaced with a file supplied as a first argument
exec < $1 let count=0
while read LINE; do
ARRAY[$count]=$LINE
((count++))
done
echo Number of elements: ${#ARRAY[@]} echo ${ARRAY[@]}
restore stdin from filedescriptor 10
and close filedescriptor 10
exec 0<&10 10<&-