LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-08-2011, 07:55 AM   #1
BoBeau236
LQ Newbie
 
Registered: May 2011
Location: Melbourne, Australia
Posts: 3

Rep: Reputation: 0
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.
 
Old 05-08-2011, 08:08 AM   #2
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
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.

Last edited by r3sistance; 05-08-2011 at 08:09 AM.
 
Old 05-08-2011, 08:59 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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.
 
Old 05-09-2011, 08:57 PM   #4
BoBeau236
LQ Newbie
 
Registered: May 2011
Location: Melbourne, Australia
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 05-09-2011, 09:09 PM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Rep: Reputation: 99
Quote:
Originally Posted by BoBeau236 View Post
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
 
Old 05-10-2011, 12:37 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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
 
Old 05-10-2011, 08:43 AM   #7
BoBeau236
LQ Newbie
 
Registered: May 2011
Location: Melbourne, Australia
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script question,standard input,output,error cola Programming 4 04-15-2010 02:11 AM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 08:05 AM
Bash Scripting: Redirect output of entire script to file & screen Kristijan Programming 1 09-11-2008 06:31 AM
How do I parse the output of a command that output to standard out? gauol Programming 9 01-17-2008 02:54 PM
standard output to screen and ffile hypexr Linux - General 2 10-27-2005 11:10 AM


All times are GMT -5. The time now is 06:10 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration