It seems obvious to me that I am missing something here. I have the following:
cfDU ()
{
clear
printf "Calculating Sizes of Data In:"
while [ -n $2 ]; do
awk -v C=$(tput cup 1 0) -v B=$(tput el) '{split($2,a,"/")
print C B a[2]}'
done
}
du -a | tee >(awk 'END {print $1}') >(cfDU) >/dev/null
It's simple enough, running du and using tee to redirect to multiple commands for processing. The function creates a little in-the-moment report so we have an idea of where in the processing we are. The awk 'END' prints out the final value from du (the total).
What I want to do is use the awk 'END' to set a variable ($sizeRAW). I've been exploring many ideas (including a second simple function), but I haven't yet come across a solution. Maybe exec? Named pipe? Great gurus of the BASH, there must be a way.
I suspect you're overthinking this a great deal. Just print your progress to standard error, and the actual data to standard output. stdout will be caught in the variable, stderr will go direct to your console.
You can even do some console-tricks (\r, to go to the beginning of the line, instead of \n, to go to the next line) so its output isn't too spammy.
/dev/stderr is how you tell awk to print to standard error. It has a special meaning to awk (even though some systems do have an actual /dev/stderr these days).
VAR=$(du -a ./ | awk -F'[/ \t]' '{ T += $1 ; printf("\rChecking %40s", $2) >"/dev/stderr" } END { print "" > "/dev/stderr" ; print T }')
Perhaps I am overthinking (I can't deny that I do that frequently), however on both Ubuntu and OS X,
VAR=$(du -a ./ | awk -F'[/ \t]' '{ T += $1 ; printf("\rChecking %40s", $2) >"/dev/stderr" } END { print "" > "/dev/stderr" ; print T }')
gives me a total approximately 8 times as big as expected (probably because it's adding $1 which includes a summary of every folder, a size for every entry within said folders, plus the final total). It also doesn't seem to actually print a path.
So, cleaning up a bit
VAR=$(du -a ./ | awk -v C=$(tput el) '{printf("\rChecking %40s", C $2) >"/dev/stderr"} END {print $1}')
sort of does what I want, although the output is a bit spazzy. I don't really need the full path, and some of these will be too long for the output anyway.
Ideally, I'd printf() just the root-level folders for the argument, but splitting that out of $2 doesn't seem to work. Again, there's no path printed to the console.
VAR=$(du -a ./ | awk '{split($2,a,"/"); printf("\rChecking %40s", a[2]) >"/dev/stderr"} END {print $1}')
I appreciate the alternate viewpoint. I'll keep exploring it.
Are you just trying to get the total of the top level directories or something? I'm confused to what the overall aim is. If you can explain what you are trying to achieve then perhaps an alternate logic, or just using the -s flag of du might sort you out.
What I'm trying to do is display the top level directory as du is grabbing size data from it's contents, and grab the total size of all contents for a variable used later in a script.
For instance, if I run
du -a /
this "function" would display "var" as it is gather sizes of items in the "var" folder, and at the end of the command run it would grab the total into a variable.
Corona, that still doesn't display anything as it's running. Admittedly, I'm currently on an OS X system (at work on break), but I got the same results (no display of the directories after the "Checking ") when I ran it on Ubuntu 14.04.