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
# 1  
Old 01-05-2011
I/O Redirection: how can I redirect error msgs to a temp file

Hi there,

I just want to know how can I redirect error msgs returned from running a command (e.g. nslookup) and then only print out the correct output using an if statement?.

I've tried the following:

Quote:
nslookup "$a" 2>bad_command
if [ ! -s
bad_command ]
then
nslookup "$
a" | grep -i "name =" | awk '{print $4}' >> hostnames
fi
where $a is a list of IPs.

but I got all the error msgs printed out to screen and the corret values hasn't enhanced Smilie

any help is apprieciated
# 2  
Old 01-05-2011
You might find it easier to do one at a time, since nslookup dos not do lists as I recall:
Code:
for i in $a
do
 ... $i ....
done

You might use "ping" or "ping -s $i 100 1" if they are supposed to be live and accessible, and get the name from that.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 01-06-2011
You can try to redirect the output to:

2>/dev/null
These 2 Users Gave Thanks to LiorAmitai For This Post:
# 4  
Old 01-06-2011
nslookup does not produce an error message on stderr or produce a return status when a host is not found. It just reports on stdout that a host could not be found.
You might want to try host instead if available, but host only works with a single name, not with a list of names...
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-06-2011
thanks for your help and input guys,

what I need is to give the nslookup an IP address each time using a while loop (done)

and than, check if a host name was returned.. if it does then great! and it will be added to a temporary file. if it is not, than I want a customized error msg to appear.

what I am doing now is taking only the host name part of the nslookup output.. which can has something like "ns9.google.com" or sometimes.. nothing!

I want to replace the "nothing" with an error msg that I wrote.

---------- Post updated at 06:21 PM ---------- Previous update was at 05:25 PM ----------

here is the part I am trying to do that through:

Quote:
awk ' {if ( ! -s (print $2)) then "you've got an error pal!" else '{print $2}' fi}'
what I'm trying to achieve here is that, when ever that value of print $2 is null, than print an error mssage, otherwise print the value in print $2

this should make things clearer to you I wish

---------- Post updated at 07:41 PM ---------- Previous update was at 06:21 PM ----------

this code seems to be partially working:

Quote:
awk '{if ( ! -n $4) { print $4 } else { print "Error"}}
but I am getting no "Error" messages. any suggestions please.

Last edited by Abdulelah; 01-06-2011 at 02:44 PM..
# 6  
Old 01-07-2011
Some messages are on stdout (1), some on stderr (2) (in bits!), none on /dev/tty, for my HPUX system:
Code:
$ truss -o /tmp/nsl.tr nslookup 12.23.34.45 ; grep write /tmp/nsl.tr          
Using /etc/hosts on:  hideme
looking up FILES
Trying DNS
Trying NIS
*** No hostname information is available for "12.23.34.45"
write(1, "U s i n g   / e t c / h o s t s ".., 32) ....... = 32
write(1, "l o o k i n g   u p   F I L E S ".., 17) ....... = 17
write(1, "T r y i n g   D N S \n", 11) ................... = 11
write(1, "T r y i n g   N I S \n", 11) ................... = 11
write(2, "* * *   N o   h ", 8) .......................... = 8
write(2, "o s t n a m e ", 7) ............................ = 7
write(2, "  i n f o r m a ", 8) .......................... = 8
write(2, "t ", 1) ........................................ = 1
write(2, "i o n   i s   a ", 8) .......................... = 8
write(2, "v ", 1) ........................................ = 1
write(2, "a i l a b l e   ", 8) .......................... = 8
write(2, "f ", 1) ........................................ = 1
write(2, "o r   " 1 2 . 2 ", 8) .......................... = 8
write(2, "3 . 3 4 . 4 5 ", 7) ............................ = 7
write(2, "" \n", 2) ...................................... = 2
$ 
 
 
 
Good IP:
 
$ truss -o /tmp/nsl.tr nslookup 89.67.45.123 ; grep write /tmp/nsl.tr
Using /etc/hosts on:  hideme
looking up FILES
Name:    hideme
Address:  89.67.45.123
Aliases:  hideme.somedomain.com
write(1, "U s i n g   / e t c / h o s t s ".., 32) ....... = 32
write(1, "l o o k i n g   u p   F I L E S ".., 17) ....... = 17
write(1, "N a m e :         h i d e m e \n".., 26) ....... = 26
write(1, "    8 9 . 6 7 . 4 5 . 1 2 3 ".., 26) ....... = 26
write(1, "    h i d e m e . s o m e d o ".., 34) ....... = 34
$
 
 
Good IP, PTR specific query for pointer-reverse = ip to name:
 
$ truss -o /tmp/nsl.tr nslookup -q=PTR 89.67.45.123 ; grep write /tmp/nsl.tr
Using /etc/hosts on:  hideme
looking up FILES
Trying DNS
123.45.67.89.in-addr.arpa    name = hideme.somedomain.com
write(1, "U s i n g   / e t c / h o s t s ".., 32) ....... = 32
write(1, "l o o k i n g   u p   F I L E S ".., 17) ....... = 17
write(1, "T r y i n g   D N S \n", 11) ................... = 11
write(1, "1 2 3 . 4 5 . 6 7 . 8 9 . i n - ".., 28) ....... = 28
write(1, "\tn a m e   =   h i d e m e . s ".., 34) ....... = 34
write(1, "\n", 1) ........................................ = 1
$
 
 
Good IP, specific DNS server:
 
$ truss -o /tmp/nsl.tr nslookup 89.67.45.123 89.123.45.67 ; grep write /tmp/nsl.tr 
Name Server:  nameserver1.somedomain.com
Address:  89.123.45.67
Trying DNS
Name:    hideme.somedomain.com
Address:  89.67.45.123 
write(1, "N a m e   S e r v e r :     n a ".., 50) ....... = 50
write(1, "    8 9 . 1 2 3 . 4 5 . 6 7 \n".., 17) ....... = 17
write(1, "T r y i n g   D N S \n", 11) ................... = 11
write(1, "N a m e :         h i d e m e ".., 44) ....... = 44
write(1, "    8 9 . 6 7 . 4 5 . 1 2 3 ".., 19) ....... = 19
$

Tools like this, called truss, tusc or strace, can tell you what you are seeing in more detail.

Last edited by DGPickett; 01-07-2011 at 01:44 PM..
# 7  
Old 01-07-2011
This thread seems to be a repeat of a previous thread:
https://www.unix.com/shell-programmin...ing-right.html
In the previous thread the O/P posted sample output from a version of "nslookup" which I do not recognise.

Anybody worked out what Operating System and TCP/IP package this is?

Just for interest, unix versions of "nslookup" do use the output channels in a strange manner. I use an unusual (but portable) method to capture the whole output of one enquiry to a file having failed to get the combined output to a pipe by normal means.
Code:
>mylogfile
nslookup server_name 2>>mylogfile 1>>mylogfile

It is then possible to search "mylogfile" for error messages such as "No hostname information is available".
If anybody recognises the O/P version of "nslookup" can they try various techniques to capture the whole output from one enquiry?
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