LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   executing per line output of script (https://www.linuxquestions.org/questions/programming-9/executing-per-line-output-of-script-4175525637/)

bradvan 11-17-2014 06:58 AM

executing per line output of script
 
Just wondering if anyone had a thought on how to accomplish this. du -k prints the results in kilobytes. In this particular instance, I know all of the sub-directories are in gigabytes and wanted to print out the results that way. So, I tried the following expecting "f" to be set to each line of output. Instead, bash returned all the output in one line and broke it up by white space.
Code:

for f in $(du -k -s *); do
  sk=$(echo ${f} | awk '{ print $1 }')
  sg=$(echo "${sk} / (1024 * 1024)" | bc)
  fil=$(echo ${f} | awk '{ print $2 }')
  echo -e "${sg}\t${fil}"
done

The result was the directory size in GB followed by the next line having the directory name processed as if a number and printed. Not quite the results I wanted. :)

bradvan 11-17-2014 07:08 AM

Found a solution with the help of Google. :) See process output of command line-by-line
I tried piping the output of du to "while read f; do," but that was failing with bash trying to execute the first directory size as a command. I then tried setting the IFS variable to a new line and re-ran my code. That time it worked! :)


All times are GMT -5. The time now is 03:28 PM.