# Tee multiple streams to create a var

**URL:** <https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096>\
**Category:** Shell Programming and Scripting\
**Created:** [October 12, 2014, 1:20am UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096 "2014-10-12T01:20:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![reid](https://community.unix.com/user_avatar/community.unix.com/reid/32/2388_2.png) [@reid](https://community.unix.com/u/reid)\
**Post date:** [October 12, 2014, 1:20am UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/1 "2014-10-12T01:20:31Z")

</div>

Greetings,

It seems obvious to me that I am missing something here. I have the following:

```plaintext
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 that I need something like:  
[how-to-use-a-variable-instead-of-a-file-to-output-this-wc-command](http://unix.stackexchange.com/questions/67420/how-to-use-a-variable-instead-of-a-file-to-output-this-wc-command)

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 12, 2014, 3:31pm UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/2 "2014-10-12T15:31:05Z")

</div>

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).

```nohighlight
VAR=$(du -a ./ | awk -F'[/ \t]' '{ T += $1 ; printf("\rChecking %40s", $2) >"/dev/stderr" } END { print "" > "/dev/stderr" ; print T }')

```

---

<div class="post-metadata">

**Author:** ![reid](https://community.unix.com/user_avatar/community.unix.com/reid/32/2388_2.png) [@reid](https://community.unix.com/u/reid)\
**Post date:** [October 13, 2014, 10:48am UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/3 "2014-10-13T10:48:52Z")

</div>

Perhaps I am overthinking (I can't deny that I do that frequently), however on both Ubuntu and OS X,

```nohighlight
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

```nohighlight
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.

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/5_5575768a8748004e209b776fc1b2916d.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [October 13, 2014, 12:44pm UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/4 "2014-10-13T12:44:30Z")

</div>

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.

Can you elaborate a little?

Thanks, in advance,  
Robin

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 14, 2014, 2:43pm UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/5 "2014-10-14T14:43:28Z")

</div>

> [@reid](#):
>
> Perhaps I am overthinking (I can't deny that I do that frequently), however on both Ubuntu and OS X,
> 
> ```plaintext
> 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
> 
> ```plaintext
> 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.
> 
> ```plaintext
> 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.

That's why I had -F, to split the path. Now that I understand that you just want the last line:

```plaintext
du -a ./ | awk -F'[/ \t]' '{ printf("\rChecking %40s", $2) >"/dev/stderr" } END { print "" > "/dev/stderr" ; print $1 }'

```

---

<div class="post-metadata">

**Author:** ![reid](https://community.unix.com/user_avatar/community.unix.com/reid/32/2388_2.png) [@reid](https://community.unix.com/u/reid)\
**Post date:** [October 15, 2014, 1:40pm UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/6 "2014-10-15T13:40:21Z")

</div>

Robin,

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

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 15, 2014, 1:43pm UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/7 "2014-10-15T13:43:12Z")

</div>

> [@reid](#):
>
> Corona, that still doesn't display anything

if by "not anything" you mean prints "checking ." I see what you mean. Easily fixed by changing $2 to $3.

---

<div class="post-metadata">

**Author:** ![reid](https://community.unix.com/user_avatar/community.unix.com/reid/32/2388_2.png) [@reid](https://community.unix.com/u/reid)\
**Post date:** [October 15, 2014, 3:34pm UTC](https://community.unix.com/t/tee-multiple-streams-to-create-a-var/347096/8 "2014-10-15T15:34:54Z")

</div>

Corona,

If I'd have exercised that bit between my ears... I'll try this out in my script.
