LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 07-16-2009, 04:17 AM   #1
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Rep: Reputation: 15
Arrow 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.
 
Old 07-17-2009, 01:07 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello stormcloud

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.

Best

Charles
 
Old 07-20-2009, 02:50 AM   #3
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Hi Catkin,

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:

VALUE1=`validate VALUE1 DEFAULT_VALUE1 "${VALUE1}"`
VALUE2=`validate VALUE2 DEFAULT_VALUE2 "${VALUE2}"`

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.
 
Old 07-20-2009, 09:01 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello stormcloud
Quote:
Originally Posted by stormcloud View Post
[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.

How about not using stdout?
Code:
validate (){
	# <do some magic here>
        defaulted_flag=<whatever>
	eval "\$$1=$out"
}
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
Best

Charles
 
Old 07-21-2009, 02:40 AM   #5
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Original Poster
Rep: Reputation: 15
This sounds like exactly what I'm trying to do. :-)
 
Old 07-21-2009, 03:33 AM   #6
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Wink

Both of these mechanisms work perfectly

Many thanks
 
Old 07-21-2009, 04:25 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello stormcloud

Glad they worked for you.

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.

Best

Charles
 
Old 07-21-2009, 10:08 AM   #8
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Ah well, I came into the forum with half a solution - your solution(s) bypassed the exact problem


Knowing that /proc/$$/fd/1 points to std out, I guess I could have done something along the lines of:

#!/bin/bash

function() {
<add magic here>
echo "Message to standard out" >> $OLD_OUT
echo "return value"
}

OLD_OUT=`ll /proc/$$/fd/1 | cut -d">" -f 2`
VAR1=`function a b c`
VAR2=`function d e f`

That said, I prefer simply assigning standard out (where ever it points to) to a new file descriptor and using that.

Last edited by stormcloud; 07-21-2009 at 10:15 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Domain - DNS Pointing wizzkid8 Linux - Networking 5 03-14-2006 12:55 AM
Pointing from an integer without a cast halfpower Programming 20 12-18-2005 06:00 PM
pointing bash Patbuzz86 Linux - Newbie 4 04-03-2004 04:38 PM
i solved the pointing thingy Longinus Programming 5 02-28-2004 03:20 AM
directory pointing to another albean Linux - Newbie 1 10-04-2002 10:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration