Tee multiple streams to create a var


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tee multiple streams to create a var
# 1  
Old 10-12-2014
Tee multiple streams to create a var

Greetings,

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

Code:
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
# 2  
Old 10-12-2014
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).

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


Last edited by Corona688; 10-12-2014 at 04:38 PM..
# 3  
Old 10-13-2014
Perhaps I am overthinking (I can't deny that I do that frequently), however on both Ubuntu and OS X,
Code:
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
Code:
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.
Code:
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.
# 4  
Old 10-13-2014
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
# 5  
Old 10-14-2014
Quote:
Originally Posted by reid
Perhaps I am overthinking (I can't deny that I do that frequently), however on both Ubuntu and OS X,
Code:
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
Code:
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.
Code:
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:

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

# 6  
Old 10-15-2014
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
Code:
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.
# 7  
Old 10-15-2014
Quote:
Originally Posted by 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mindboggling difference between using "tee" and "/usr/bin/tee" in bash

I'm on Ubuntu 14.04 and I manually updated my coreutils so that "tee" is now on version 8.27 I was running a script using bash where there is some write to pipe error at some point causing the tee command to exit abruptly while the script continues to run. The newer version of tee seems to prevent... (2 Replies)
Discussion started by: stompadon
2 Replies

2. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

Short command to create two files >{respo,nd}.php (with "tee" command?)

08:29 < xsi> >{respo,nd}.php bash: {respo,nd}.php: ambiguous redirect 08:31 < geirha> xsi: maybe you want tee So I was advised to do so. And I can't create two OR MORE files at once with {a,b,c,d,e,f}.php (which I quickly now need to create and to learn to create in the future to quickly... (2 Replies)
Discussion started by: Xcislav
2 Replies

4. Shell Programming and Scripting

Create multiple files

dear all suppose I have two files file_000 and file_id: file_000: blablablabla000blablabla000 000blahblah000blahblah blah000blahblahfile_id: 001 002 003now, based on file_id, I want to create 3 files; the name of each file would be file_001,file_002,file_003,respectively, and the... (4 Replies)
Discussion started by: littlewenwen
4 Replies

5. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

6. Shell Programming and Scripting

How to create multiple files?

HI, I would like to create the files as file1.txt file2.txt file3.txt ...... ....... ....... filen.txt in a single unix command, i dont want to use the loops. n is user specific Kindly help me in this. THank you Jagadeesh (2 Replies)
Discussion started by: jagguvarma
2 Replies

7. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

8. Shell Programming and Scripting

Why cannot have multiple pipes from tee?

why I cannot do this? prog_name | tee logfile | awk /regexp/ | awk /regexp/ I now this is not elegant code, but am intrigued as to why multiple pipes from tee not allowed. (2 Replies)
Discussion started by: euval
2 Replies

9. Red Hat

create /var lvm during install

Hi All, How do I create /var as LVM type during install? I want my new OS to have /var as LVM so that I could extend it on the fly. Thanks for any comment you may add. (2 Replies)
Discussion started by: itik
2 Replies

10. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies
Login or Register to Ask a Question