Dual output (stdout and file)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dual output (stdout and file)
# 1  
Old 03-22-2007
Dual output (stdout and file)

Hello everybody,

Is there a more elegant way to make dual output (display on standard output and append to a file) while I'm executing a shell script, besides duplicating the echo command for every string?

echo "Message..." > 1
echo "Message..." >> myfile.out

Thank you for your time,
Adrian
# 2  
Old 03-22-2007
Code:
echo "message...." | tee -a myfile.out

# 3  
Old 03-23-2007
Using Log Function

Code:
#!/bin/ksh

LOG_FILE=/tmp/${0##*/}.log

LogMsg() {

 #Redirecting the Output just to the Log file.
 #print "$@:$(date +%d/%m/%y:%H:%M:%S)"  >> $LOG_FILE

 #Redirection to stdout and file.
 print "$@:$(date +%d/%m/%y:%H:%M:%S)"  | tee -a $LOG_FILE
}

CallB() {
 LogMsg "Entering CallB routine"
}

Main() {
 LogMsg "Entering the function"
 CallB
}

#Call the Main Function here.
 Main

Just in case in future after having tested code you may have to remove logging,using the above approach you have just haveto change the LOG_FILE=/dev/null or LogMsg function.


Pls comment of the above approach.

Thanks
Nagarajan Ganesan.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output of until to file versus stdout

Why does this until false; do history | head -5; done result in a stdout infinite loop, yet until false; do history | head -5 > hist5; done only writes it once to file hist5? Furthermore, I can hear the hard drive working on the 2nd command until I end the process, but the history | head -5 is... (1 Reply)
Discussion started by: Xubuntu56
1 Replies

2. Shell Programming and Scripting

Redirecting stdout output to whiptail menu box

As a result of whiptail menu option I am getting a data from a file. Naturally it is output to terminal as stdour. I like to redirect the output back to the menu. It can be done with single input of line of text , see attached. I just cannot see where or how the sample... (0 Replies)
Discussion started by: annacreek
0 Replies

3. Shell Programming and Scripting

File descriptors, redirecting output, and stdout

Hello all. I've been lurking here for a year or two and finally decided to post. I need some assistance with file descriptors, stdout, and redirecting output. I've searched through a number of very helpful threads here (unfortunately I can't link to any of them yet due to my low post count...),... (2 Replies)
Discussion started by: Michael_K
2 Replies

4. Programming

Print C file output both in stdout and file

Hello! I want to post a question. does anybody know how to print the output of a C program both in stdout and a file?? thanx (8 Replies)
Discussion started by: nicos
8 Replies

5. Shell Programming and Scripting

Redirecting part of output to stdout

Hi, I am trying to execute a command like this: find ./ -name "*.gz" -exec sh -c 'zcat {} | awk -f parse.awk' \; >> output If I want to print the filename, i generally use the -print argument to the find command but when I am redirecting the output to a file, how can I print just the... (2 Replies)
Discussion started by: Legend986
2 Replies

6. Shell Programming and Scripting

Creating an Array in KSH from output (stdout)

Using Cygwin PDksh - But also have tested it on Linux with same results ---- I have a script that invokes a program/script and returns a string of data (1234 "12 34 56" 6789) and using "set -A" inserting it into an array. script code snipit >> get_array=$(php... (2 Replies)
Discussion started by: carlos25
2 Replies

7. Shell Programming and Scripting

let curl output to stdout AND save to a file

hello hackers. i have a curl process running as cgi directly pushing stdout to the client. but i want to additionally save that stream to a file at the same time. any directions madly welcome. thanks in advance (3 Replies)
Discussion started by: scarfake
3 Replies

8. Shell Programming and Scripting

getting stderr & stdout output lively modified

This is about getting all output to stderr and stdout localized. Nothing to do with redirecting output to a file (there already are some interesting threads about that issue on this forum). What I intend to do is capturing all lines of text sent to the screen, compare them with an array of... (2 Replies)
Discussion started by: teo ramirez
2 Replies

9. Shell Programming and Scripting

Script Output Woes (stdout?)

Hey everyone. I have been trying a few filtering scripts with both SED and PERL. So far I have both of these versions working to reformat the incoming text stream (from stdin) into the corrent format (it looks good in the terminal), but I don't think that I am doing it right because the... (2 Replies)
Discussion started by: c0nn0r
2 Replies

10. UNIX for Advanced & Expert Users

sending syslog output to stderr or stdout

Is there a way to send the syslog output for a given facility to stderr or stdout? I do not want to use the "tail" command to achieve this, I would like it to go directly to stderr. Thanks in advance (1 Reply)
Discussion started by: dmirza
1 Replies
Login or Register to Ask a Question