LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2014, 06:58 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
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.
 
Old 11-17-2014, 07:08 AM   #2
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Removing line from executing script waddles Slackware 6 09-16-2013 03:18 AM
Get output from first and fourth line though shell script sandeep_hello Linux - Server 10 08-21-2012 11:36 PM
[SOLVED] zgrep output different in script than on command line. jhwilliams Programming 1 12-04-2011 06:42 PM
reading command output by line in Bourne script? ocicat Programming 6 02-14-2010 02:48 AM
Possible to output line Makefile is executing? homer_3 Programming 2 08-03-2009 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration