Can STDERR be saved to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can STDERR be saved to a variable
# 1  
Old 04-17-2009
PHP Can STDERR be saved to a variable

Guys i'm trying to save STDERR to a variable for a portion of my ksh script on solaris.

I know i can create redirects to files as such:

exec 4>/tmp/lava
print "This will be saved to /tmp/lava and not screen"; >&4
print "This will be seen on screen" >&2

I want to save the STDOUT of a command to a variable and save STDERR to another without having to have files everywhere.

e.g tapelist=$( command blah blah )

I then want to check if tapelist contains anything and run stuff otherwise dont
i.e

if [[ -n "{tapelist}" ]];then
do stuff....
else
do other stuff
fi

If the command fails it produces STDERR output i want to save so i can print it out later.

So in my thoughts although i know this doesnt work :-

savestderr="";
exec 3>${savestderr}
tapelist=$(command blah blah 2>&3)

Then can reference the error message as ${savestderr} Any ideas?

Last edited by lavascript; 04-17-2009 at 08:38 AM.. Reason: whats wrong with the formatting?
# 2  
Old 04-17-2009
hm the only way I know, to split stdout and stderr to varialbes, without using files, is to run the command twice

short example:

eg.:

Code:
stderr=$(ls /opt /bla 2>&1 1>/dev/null )
stdout=$(ls  /opt /bla 2>/dev/null)


Code:
echo $stderr
ls: 0653-341 The file bla does not exist.

Code:
echo $stdout
opt:  IBM IBMinvscout csm diagnostics freeware hsc ibm_help lost+found perl

if you modify something with your command, you can't use this method

Last edited by funksen; 04-17-2009 at 09:57 AM..
# 3  
Old 04-17-2009
Thanks Dude,

I wonder which is less expensive.

Guess i'm going down the file route
Code:
2>${TMP}/$(basename $0).stderr

Cheers
# 4  
Old 04-17-2009
Use
Code:
exec 2>${TMP}/$(basename $0).stderr

inside the script.
# 5  
Old 04-17-2009
If I want stderr and stdout in variables, I would do it this way

Code:
stdout=$(ls /bla /opt 2>/tmp/stderr.txt)
stderr=$(cat /tmp/stderr.txt)

an then remove the file, or use it for other commands in the script


normally, to check if a command fails or not, you check the return code $?

for just later reading it, to see what happens, you should use a file
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cannot access saved file

This may be wrong place to ask. I am currently modifying "stock" configuration file and making slow progress. Run into a snag - twice so far. I can work on the project switching from editing to run all day long, no issue. Next day i cannot access the file to run it. The file... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

Bash - proper way to append variable to stderr

Hello, Can you please if the bellow is the proper way of appending a variable to the stderr: The easiest way to test this,I was able to imagine, was by touching 5 files and afterwards looping trough to the results: -rw-r--r-- 1 ab owner 0 Sep 14 13:45 file1 -rw-r--r-- 1 ab owner 0 Sep... (7 Replies)
Discussion started by: alex2005
7 Replies

3. Solaris

Aliases not getting saved in OK prompt

Hi Guys, After setting below paths: ok nvalias rootdisk /pci@1c,600000/scsi@2/disk@0,0:a ok nvalias rootmirror /pci@1c,600000/scsi@2/disk@1,0:a ok nvstore to check: ok devalias devailas does show set path but after reboot The alias for disks are not getting saved. I get this error:-... (5 Replies)
Discussion started by: manalisharmabe
5 Replies

4. UNIX for Dummies Questions & Answers

Script Not getting Saved

Hi , Script File Is Not Getting Saved This Are The Steps I Am Following For Saving And Executing A Script 1). vi ( To Open Vi Editor ) 2). vi filename ( vi firstprog.ksh) #!bin\kash date 3) !wq :( Saving And Quit) When I Am Saving The Scrpit I Am Getting The Below... (1 Reply)
Discussion started by: anudeepkumar123
1 Replies

5. Shell Programming and Scripting

How to redirect the STDERR to a variable in perl?

in my perl script i tried the below statement $result = `cleartool rmstream -f $s1 1> /dev/null`; so as to redirect then error messages,when i print the $result ,it seems to be Null. (4 Replies)
Discussion started by: ram_unx
4 Replies

6. UNIX for Dummies Questions & Answers

where alias saved?

step 1 # alias alias cp='cp -i' alias l.='ls -d .* --color=tty' alias ll='ls -l --color=tty' alias ls='ls --color=tty' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' step 2 # cat ~/.bashrc # .bashrc (3 Replies)
Discussion started by: cqlouis
3 Replies

7. UNIX for Dummies Questions & Answers

Cut from inside a saved variable

ok, heres my problem. i have saved the last line from a ls -l command into a variable. What i want to do is be able to cut the second character into that (i.e. the r if its there of the - if its not there) and save that value into another variable. here is the current code i have to save... (1 Reply)
Discussion started by: strasner
1 Replies

8. UNIX and Linux Applications

Bluefish: where are the preferences saved?

I have just tried out Bluefish as an alternative to my regular text editor. If I save the modified preferences and reboot, the preferences have to be reentered again. Does anyone know which file the preferences are saved in? The command find / -mmin -5 | grep bluefish yields zero hits. Thanks... (2 Replies)
Discussion started by: figaro
2 Replies

9. UNIX for Advanced & Expert Users

Sudo file not saved

Hi all, I have edited my sudoers file. I am using visudo command I have added the following lines and saved the file. I am saving the lines as :wq But I am very amazed to see that these lines are not written in the sudoers file. I have retried the above process many times, when I... (0 Replies)
Discussion started by: Asteroid
0 Replies
Login or Register to Ask a Question