Is it possible for back to discover what std out is pointing to?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is it possible for back to discover what std out is pointing to?
An odd problem, but...
I'd like to write a bash script that contains a function. This function needs to be able to
1) write to standard out to say what it's doing
2) return a string value.
My script can be called in one of two ways
1) from the command line - standard out is to the terminal
2) from a GUI - standard out is to a panel on the GUI.
I think my script needs to look something like this:
fn (){
out=<do some magic here>
#now write to std out to say what we did
echo "out is now ${out}" >> $std_out
# write to standard out to return a value
echo "$out"
}
std_out=<what goes here?>
VALUE1=`fn a b c`
VALUE2=`fn c d e`
I'm want to write to std out inside the function to set the return value. but I also need to write to the original std out to send messages back to the user.
I guess I need to assign std_out to something at the top of my script to the "real" standard out, but how?
Last edited by stormcloud; 07-21-2009 at 10:15 AM.
Sounds possible. Please give more detail on "from a GUI - standard out is to a panel on the GUI": which GUI? How do you start the script? Which panel? Does stdout already go to this panel OK?
Bash functions can only return integers; after calling the function this value is available as $? No problem; bash functions can set global variables that are accessible to the caller.
The GUI is written by a couple of other people in Java. I'm not quite sure how all of their stuff works (and asking them to make changes is not an option), but from my POV all I need to do to get something on the panel is write to standard out - either echo or printf will do the trick. Their GUI calls my script(s) and redirects my output to their app. When I'm developing I don't bother with the GUI - I just run the scripts directly.
You are right about the return value of the function just being an integer - to return a string I'd like to send my value to standard out and have the caller pick it up.
The point of this function will be to validate about 30 environment variables - if they are not set they will be defaulted, I intend to call my function as follows:
So my arguments are:
1) Name of variable (for debug purposes)
2) Default value if variable is invalid
3) Value to validate (may be empty string)
By assigning the variable I'm validating back to it's self I can pick up the string I echo from the function (a.k.a. a string return value) and it all looks quite neat, and I don't need to to set up temporary variables :-)
The problem is that I also have to tell the user *if* the variable has been defaulted, and this means writing back to the *original* standard out. Trouble is I'm using standard out in the function to handle the return string. What I need to do is redirect the echo in the validation function to write to the original standard output stream. I just need to know what it is.
Last edited by stormcloud; 07-20-2009 at 07:24 AM.
[snip]
By assigning the variable I'm validating back to it's self I can pick up the string I echo from the function (a.k.a. a string return value) and it all looks quite neat, and I don't need to to set up temporary variables :-)
The problem is that I also have to tell the user *if* the variable has been defaulted, and this means writing back to the *original* standard out. Trouble is I'm using standard out in the function to handle the return string. What I need to do is redirect the echo in the validation function to write to the original standard output stream. I just need to know what it is.
Thanks for explanation -- it's clear. A neat technique! Only problem is that `<commands>` is outputting to stdout and the GUI code that calls your script has set up stdout to go to the GUI.
Alternatively, if you really want to use stdout, the following code (limited testing!) shows how it can be done including restoring stdout to whatever the Java GUI program had set up.
Code:
exec >GUI # Initial stdout redirection, "simulating" Java GUI program's setting
echo 'echo to stdout under initial setting'
exec 3>&1 # Save stdout redirection on file descriptor 3
echo 'echo to stdout after copying to file descriptor 3'
echo 'echo to 3 after copying to file descriptor 3' >&3
exec 1>/dev/tty # Open stdout as default
echo 'echo to stdout after after opening stdout as default'
x=`echo 'echo to stdout in backqotes after opening stdout as default'`
echo x is $x
exec 1>&3 # Restore stdout
echo 'echo to stdout after restoring stdout to initial setting'
exec 3>&- # Tidy up by closing (deleting?) file descriptor 3
echo 'echo to stdout after closing (deleting?) file descriptor 3'
Here's running the above:
Code:
c@CW8:~/d/bin/try$ ./essay.sh
echo to stdout after after opening stdout as default
x is echo to stdout in backqotes after opening stdout as default
c@CW8:~/d/bin/try$ cat GUI
echo to stdout under initial setting
echo to stdout after copying to file descriptor 3
echo to 3 after copying to file descriptor 3
echo to stdout after restoring stdout to initial setting
echo to stdout after closing (deleting?) file descriptor 3
What we haven't done is answered your question, "Is it possible for bash to discover what std out is pointing to?". The answer is "yes"; /proc/$$/fd/1 (where $$ is the shell's PID) is a symbolic link to where stdout is going.
It may not answer your need though; if you saved that information, re-assigned stdout to /dev/tty (a very virtual device, meaning "my terminal"), did your stuff and then re-assigned stdout to the original destination, there could be unwanted side effects from closing it.
For example, if it is a pipe to the Java program and the Java program has the pipe open in a "blocking read", the Java program might get an error when you disconnect from it by re-assigning stdout; if there is no process writing to the pipe it would be waiting indefinitely. Not my area of knowledge so a "plausible hypothesis" rather than fact!
Definitely safer to duplicate stdout and then restore it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.