LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Standard script output to screen & -x option output to file (http://www.linuxquestions.org/questions/programming-9/standard-script-output-to-screen-and-x-option-output-to-file-879462/)

BoBeau236 05-08-2011 06:55 AM

Standard script output to screen & -x option output to file
 
Hi all,

Well, here I am. Close to 50 years old, new to scripting and first post. (Ironically sitting in front of the TV watching New Tricks). But I digress, so to business...

Someone has written for me a bash script running under Cygwin that uses wget and http (and more)to send commands to, and get responses from, a web-based system. I have written a basic wrapper script for it to make it more user friendly (i.e. present a menu etc.) and I used the -x option a lot when developing my wrapper script to be able to view the http responses from the system. I would now like to have a log file created that contains the information shown by -x when a user runs my script, but I don't want it echoed to the screen.

To summarise: Standard script output to the screen and -x output to a file.

Is there a way to do this? My investigations so far show that it may not, so I am willing to consider other methods of debug output to file.

Many thanks,

Bo.

r3sistance 05-08-2011 07:08 AM

if it's just the echo statement itself you want logged, within the script file just add ">> /path/file" to the end of the line that echos this to the screen.

echo "my line" >> /path/file

where path and file are replaced by the actual path and file you wish to log this in.

grail 05-08-2011 07:59 AM

Well it will depend on how you want it done. ie at runtime or as part of the script (I assume probably the latter), but here are both:
Code:

# at runtime
./script [any arguments here] 2> logfile

#as part of the script
exec 2> logfile
set -x

The obvious issue here is that any errors will also go to the logfile as this is all redirecting stderr, which is where -x publishes its information.

BoBeau236 05-09-2011 07:57 PM

Hi r3sistance and grail,

Thank you both for your replies.

r3sistance, my script is a bit large to make the suggested addition to the end of every line I want echoed to the log file, but I will keep it in mind for future use.

grail, I tried both of your suggestions and found that they both do almost does what I want. I tested using this:

Code:

#!/bin/sh
exec 2> logfile.txt
set -x
read -p "Type something here: " stuff
echo "You typed" $stuff
exit 0

However, I realise now that I was not as clear in my original post as I should have been. Using the test code above, the "read" prompt is output to the log file and the user doesn't get to see it. Is there a way that the prompts can be sent to stdout as well as the log file?

Thanks,

Bo.

vikas027 05-09-2011 08:09 PM

Quote:

Originally Posted by BoBeau236 (Post 4351541)
Is there a way that the prompts can be sent to stdout as well as the log file?.

I do not know how to redirect the prompts but yes you can very well redirect the variables (which has the prompt answers).
I think this can serve your purpose.

For e.g. See the bold line

Quote:

#!/bin/sh
exec 2> logfile.txt
set -x
read -p "Type something here: " stuff
echo "You typed" $stuff
echo $stuff >> /your/log/file
exit 0

grail 05-09-2011 11:37 PM

Well not sure how complicated you wish to get, but this seems to solve your current scenario:
Code:

#!/bin/sh
exec 2> logfile.txt
set -x
read -p "Type something here: " stuff 2>&1
echo "You typed" $stuff
exit 0


BoBeau236 05-10-2011 07:43 AM

Hi grail, vikas027 and r3sistance,

I'm getting close but I'll need to play around with all the suggestions to get exactly what I need. I'll be travelling for a while now, so I'll get back to you with results after further testing and when I can. This post is just to say thanks, I appreciate your answers and I just didn't want to leave you without a response and thanks until I can get back to the forum.

Best regards,

Bo.


All times are GMT -5. The time now is 09:16 AM.