I/O Redirection: how can I redirect error msgs to a temp file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I/O Redirection: how can I redirect error msgs to a temp file
# 8  
Old 01-07-2011
The general rule is " >> file 2>&1 " to capture both, one (stdout) first, then 2 (stderr).

Very occasionally, you will see/do these in the opposite order, and it is not always a mistake (just usually), to send stdout to a file and stderr down a pipeline. If the app writes to (or reads from) an fd opened on /dev/tty, then you are into the land of expect/rsh/ssh to create a pseudo/remote tty you can recapture onto stdout.

Keeping the controlling terminal mapped to /dev/tty, FILE*'s: stdin, stdout, stderr, fd's: 0, 1, 2 ; cooked and uncooked tty input and output all straight and separate is one of those classic initial UNIX learning tasks.

Putting both into one file is popular: you do no care which wrote what, no research, one or no files, and you can grep or otherwise process the output to see if you got what you wanted and discard the rest. You might do something like this for simplicity and volume efficiency:
Code:
for ip in $ip_list
do
 echo ==== $ip
 nslookup $ip $DNS_SERVER
 echo ====
done 2>&1 | sed '
  /^==== /{
    :loop
    /\n====$/!N
    b loop
   }
 . . .
 ' > output_file

This way, you do an nslookup for each ip and frame the output for each. One sed can collect all the lines for one ip into the buffer using that framing, and then ( . . . = depends on what your nslookup writes and what you want) you can make it into whatever you want. The only file you make is the output file -- no cleanup, no old data, no file name creation or collisions, no running out of space with big data sets.

---------- Post updated at 03:35 PM ---------- Previous update was at 02:41 PM ----------

PS: nslookup has an interactive command mode, so you can use one nslookup if you generate commands to it equivalent to the command line args, and avoid calling it so many times.
# 9  
Old 01-09-2011
@DGPickett
Under what unix O/S does nslookup react correctly to the Shell redirects like "2>&1". It is one of the very few programs where I had to use unusual techniques.
Imho unix "nslookup" just ignores unix I/O conventions.


Btw. Scripting a unix "nslookup" global enquiry and saving the results is not hard work. The secret there is not to try to redirect the output in Shell but to use the output functions in "nslookup" itself.
There almost certainly will be issues with listing the contents of a remote Internet Name Server because most of them block such enquiries.
# 10  
Old 01-09-2011
Yes, zone inquiry is no longer considered innocent!

nslookup is not a mainstream tool, and like many somewhat admin-level tools, it not so user friendly. For the casual user, learning the various query methods and output formats is a bit of a pain. They might be better served doing getpeername, gethostbyname and such in C, JAVA or PERL.

Last edited by DGPickett; 01-09-2011 at 11:00 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect grep error/result to file

I'm having trouble piping grep results/error to a file. Why can't I redirect these messages into a file? $ grep -r -o "pattern" * > log Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches Binary file foo4 matches The log is created, but remains empty. ... (1 Reply)
Discussion started by: chipperuga
1 Replies

2. Shell Programming and Scripting

find and replace a string in a file without the use of temp file

Hi - I am looking for a replacing a string in a in multiple *.sql files in directory with a new string without using a temporary file Normally I can use sed command as below for W in ls `FILE*.sql` do sed 's/OLD/NEW/g' $W > TEMPFILE.dat mv TEMPFILE.dat $W done But Here in my... (9 Replies)
Discussion started by: raghutapal
9 Replies

3. Shell Programming and Scripting

Problem in redirecting ftp msgs to a log file

Hi all.. The following set of statements is used in a shell script to ftp a file to a remote machine I want to redirect the ftp messages thrown by the first ftp statement to a log file. As you can see there is a logic downstream to decide if the ftp was a success or not. But i am not... (5 Replies)
Discussion started by: hareeshkumaru
5 Replies

4. Shell Programming and Scripting

How to avoid a temp file

Hi all. I want to check the free space on a given FS and process the output. Right now, I'm using a temp file to avoid using df twice. This is what I'm doing #!/usr/bin/ksh ... df -k $FS_NAME > $TMP_FILE 2>&1 if ]; then RESULT="CRITICAL - $(cat $TMP_FILE)" else cat $TMP_FILE | ...... (3 Replies)
Discussion started by: fox1212
3 Replies

5. Solaris

troubleshooting log detailing symptoms/error msgs/fix actions for NIS+ client authent

summary found at bottom. to skip straight to action summary, ctrl+f for <summary> this initially started with trouble changing passwords due to client being unable to authenticate, this was further caused by missing client files. This was transparent to me, so this details the road I took,... (0 Replies)
Discussion started by: ProGrammar
0 Replies

6. UNIX and Linux Applications

mail: no space for temp file

I'm trying to read mail but I obtained this message: msgcnt 2446 vxfs: mesg 001: vx_nospace - /dev/vg00/lvol4 file system full (1 block extent) mail: no space for temp file I want to empty my mailbox How could I make this? Regards Leonardo Siza (3 Replies)
Discussion started by: usernamea
3 Replies

7. Shell Programming and Scripting

Error file Redirection

Hello All, I was wondering is there any other way in Shell Scripting to redirect the errors to a output file inside a shell script with using a error status checking or a command line redirection. Say without doing this ksh test.ksh 2> error.txt or without doing this ... if ; then... (3 Replies)
Discussion started by: maxmave
3 Replies

8. HP-UX

How to Redirect the error messages from Syslog file to our own Application Log File

Hello, I am New to Unix. I am Using HP-UX 9000 Series for my Application. I am Currently Facing an Issue that the error messages are being written in the syslog file instead of the Application Log File. The Codes for that Syslog.h is written in Pro*C. I want to know how to Redirect these... (3 Replies)
Discussion started by: balasubramaniam
3 Replies

9. UNIX for Dummies Questions & Answers

redirect standard error into log file

Hi, I am new in shell scripting. Can anyone point out what wrong of below script. If I want the error output to "sqlerror.log" and database pool data output to "bulk_main.dat". Right now, the below script, if successful execute, the data will output to bulk_main.dat && sqlerror.log both... (7 Replies)
Discussion started by: epall
7 Replies
Login or Register to Ask a Question