multiple variables assignement (stdout/stderr outputs)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers multiple variables assignement (stdout/stderr outputs)
# 1  
Old 08-24-2010
multiple variables assignement (stdout/stderr outputs)

Hi all,

I've been looking around for this for a while and can't seem to find a satifactory way to do what I want:

I would like to assign the output of stdout to a variable and that of stderr to another one, and this without using temporary files/named pipes. In other words be able to assign several variable at a time.

For instance:
Code:
touch a_file
STDOUT=$(ls not_a_file a_file) # but error message is lost unless redirected somewhere
ls: cannot access not_a_file: No such file or directory
echo $STDOUT
a_file

Is it even possible? problems with backticks (``) or $(...) is that it only assigns 1 variable at a time I believe. Anyone's got any idea?

I could do:

Code:
STDOUT=$(ls not_a_file a_file 2>&1)
echo $STDOUT
ls: cannot access not_a_file: No such file or directory a_file

and parse the output but is the order even guaranteed? even if it is, distinguishing the stderr part from the stdout one would be difficult...

Thanks!
Anthony

Last edited by anthalamus; 08-24-2010 at 05:53 PM..
# 2  
Old 08-24-2010
I don't think you can do this without pipes or temporary files. Messages on stderr are usually meant for humans and stdout for parsing by programs anyway, so scripts usually capture stdout for their own use and let operators see via stderr if anything goes wrong. After all, the error message may not be what the program expected.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-25-2010
Quote:
Originally Posted by anthalamus
I would like to assign the output of stdout to a variable and that of stderr to another one, and this without using temporary files/named pipes.
I agree with Corona688, cannot be done without pipes. However, you said 'named pipes' which technically are different beasts so I offer this "mess" for you to consider.

Code:
#!/usr/bin/env ksh 
(ls *.jpg bfoo | sed 's/^/STDOUT /') 2>&1 | awk ' 
        /STDOUT/ { 
                gsub( "STDOUT ", "" ); 
                o1 = o1 $0 "; "; 
                next;
        } 
        {
                o2 = o2 $0 "; ";
        } 
        END{ 
                printf( "%s~%s\n", o1, o2 ); 
        }' |  IFS="~" read  stdout stderr
echo "stdout=$stdout"
echo "stderr=$stderr"

It all can be crammed on one line; I've formatted it to be easier to read. It makes the assumption that the tilde (~) is never found in either the stdout or stderr stream. The records of each output stream are separated with semicolons, so if the output contains them, things will be difficult.

Running the script in my small test environment produced:
Code:
$ t20   
stdout=bar.jpg; foo.jpg; snapshot.jpg; 
stderr=ls: bfoo: not found;

I tested this in Kshell; I don't think it will work under bash as bash has issues executing a command and reading the output into variables. It will also be subject to the limits that Kshell might impose on variable value lengths or that awk might impose on string lengths. If you have a command that produces rather long output/error you could have problems.

Regardless, interesting brain twister.
This User Gave Thanks to agama For This Post:
# 4  
Old 08-25-2010
ha ha thanks agama, I guess i'll keep my temporary files then Smilie, they're not so bad after all!!
It's a pity though, i was hoping some kind of easy redirection like

Code:
ls not_a_file a_file >$OUT 2>$ERR # obviously does not actually work!

It wouldn't seem so hard to provide in shells nowadays, since the same can be achieved with a few commands using files. Anyway, I guess the question of whether it is actually useful or not is worth asking.

Anyway thanks to both of you!

EDIT: well I settled for:
Code:
COMMAND="ls not_a_file a_file"
OUT=`mktemp` && ERR=`mktemp` && eval "$COMMAND" 1>$OUT 2>$ERR; STDOUT=`cat $OUT` && STDERR=`cat $ERR` && rm $OUT $ERR
echo -e "STDOUT=$STDOUT\nSTDERR=$STDERR"

If something thinks of something much better, let me know!!

Last edited by anthalamus; 08-25-2010 at 02:40 PM..
# 5  
Old 08-25-2010
I think that eval is redundant, the command being eval-ed should work without it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[stdin / stdout] Strategies for redirecting outputs

Well.. let's say i need to write a pretty simple script. In my script i have 2 variables which can have value of 0 or 1. $VERBOSE $LOG I need to implement these cases: ($VERBOSE = 0 && $LOG = 0) => ONLY ERROR output (STDERR to console && STDOUT to /dev/null) ($VERBOSE = 1... (5 Replies)
Discussion started by: Marmz
5 Replies

2. Shell Programming and Scripting

stdout, stderr redirection

Hi all, can someone help me with the next redirection? i want to redirect the stdout+stderr of a command to the same file (this i can do by prog &> file) but in addition i want to redirect only the stderr to a different file. how can i do this please? (in BASH) thanks. (4 Replies)
Discussion started by: eee
4 Replies

3. Shell Programming and Scripting

stderr/stdout

Can somebody explain to me why the diff output is not going to stderr? Yet when I issue a diff from the command line the return code is -ne 1. I am guessing diff always writes to stdout??? Is there away I can force the difff to write to stderr USING THE CURRENT template. If possible, I... (5 Replies)
Discussion started by: BeefStu
5 Replies

4. Red Hat

Make STDERR readable as STDOUT

Hi all. I am trying to use backticks in Perl to put STDERR into a string. The code is... $readkey_test = `perl -MTerm::ReadKey -e 1`; print $readkey_test; if ($readkey_test =~ m/]/) { print "ReadKey not installed...\n"; } else { print "ReadKey installed...\n"; } If it comes up... (3 Replies)
Discussion started by: austinharris43
3 Replies

5. UNIX for Dummies Questions & Answers

Redirecting several outputs to /dev/stdout

I have an executable that, depending on its input, outputs to either one file or several. It usually prints nothing on screen. The usual way to call this program is to specify an input and output filenames, like this: ./executable.exe -i inputfile -o outputfileIt will then try to use the output... (1 Reply)
Discussion started by: aplaydoc
1 Replies

6. Shell Programming and Scripting

sending stdout and stderr to a file

working on a c sell script I think I understand the concept of it, which is: filename >> file.txt (to appaend) or filename | tee -a file.txt (to append) The problem is that my shell script is used with several parameters, and these commands don't seem to work with just filename. They... (2 Replies)
Discussion started by: mistermojo
2 Replies

7. Shell Programming and Scripting

How to use tee with stdout and stderr?

I have been doing this: make xyz &> xyz.log &; tail -f xyz.log The problem with this is that you never can ge sure when "make xyz" is done. How can I pipe both stderr and stdout into tee so both stderr and stdout are copied both to the display and to the log file? Thanks, Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

8. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

9. Shell Programming and Scripting

precedence of stderr and stdout

#!/usr/bin/perl open(STDOUT, ">>$Textfile") open(STDERR, ">>$Textfile") print "program running\n"; $final = join("+", $initial,$final) #5 close (STDOUT); close (STDERR);Hi all, above is my perl code. Notice i have captured the stdout and stderr to the same textfile. my code is expected to... (1 Reply)
Discussion started by: new2ss
1 Replies

10. Shell Programming and Scripting

Redirect stdout and stderr

How can I redirect and append stdout and stderr to a file when using cron? Here is my crontab file: */5 * * * * /dir/php /dir/process_fns.php >>& /dir/dump.txt Cron gives me an 'unexpected character found in line' when trying to add my crontab file. Regards, Zach Curtis POPULUS (8 Replies)
Discussion started by: zcurtis
8 Replies
Login or Register to Ask a Question