I have the following code from a script below that's supposed to capture the output of
ls -l on your current directory and discard the first line, where total is written. Then output to the terminal for every file the permission, owner and file name and at the end the total number of lines. The problem is if in the directory there is a file containing white space in the name it's not listed correctly. Why not?
list=`ls -l` IFS_old=$IFS IFS=$'\n' i=0 for row in $list do if (( $i == 0 )); then ((i++)) else IFS=$' ' j=0 for attrib in $row do ((j+=1)) if (( $j == 1 )) || (( $j == 3 )) || (( $j == 5 )) || (( $j == 9 )); then printf "%s " $attrib fi done printf "\n" IFS=$'\n' ((i++)) fi done printf "%s%s\n" "Total number of files : " $((i-1)) IFS=$IFS_old exit 0