ksh- redirect stderr to file and then modify the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh- redirect stderr to file and then modify the file
# 1  
Old 09-26-2011
ksh- redirect stderr to file and then modify the file

I have the following:
Code:
remsh $host -n 2>>syslog_issue_list.txt grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log |
awk /"$DATE1"/ | awk -vhost="$host" '!/remsh|telnetd/{print host "\n", $0 >> "syslog_issue_list.txt"}'

I am creating a health script that has numerous health checks in it. This check is looking at a remote host's syslog for certain words (EMS, error, etc..). I am then using awk to print the hostname of the remote host and then errors that i searched for in the syslog.

As you can see, I redirected stderr to syslog_issue_list.txt in order to catch certain errors which did not show up in teh awk statement. I was wondering if it is possible to redirect stderr, to that file, and then immediately put in before/after the syslog_issue_list.txt file the hostname that is producing the error.

For example, the redirection will show errors like this:

remshd: Login Incorrect

I have dozens of hosts to check and I will have no clue which host created that error entry. I would like the stderr redirect to the error log to show:

HOSTNAME remshd: Login Incorrect

Anyone have any ideas? Thanks
# 2  
Old 09-26-2011
You mean, redirect stderr to more than one file?

You can't redirect one file to more than one place, you have to use utilities like tee to read one file descriptor and write to two.

This will definitely mess up the timing of the messages.
# 3  
Old 09-27-2011
Not redirect to more than one file. I would like to have stderr redirect to a file, but insert the hostname (which created the error) to be listed before the error.

Instead of this entry in the log file (from host BLAH):

remshd: Login Incorrect

I would like this:

BLAH remshd: Login Incorrect

I have tried to put in the host variable before/after/between the redirect, but nothing has worked...
# 4  
Old 09-27-2011
See if this works for you...
Code:
remsh $host -n grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log 2>&1 | xargs  echo $host >>syslog_issue_list.txt

# 5  
Old 09-27-2011
shamrock,

Thank you for the reply but your suggestion did not work. The error, remshd: Login Incorrect, propagated on the monitor and the hostname was written to the log file.

Through alot of trial and error, it seems the redirection will only work correctly if it is placed just after the remsh command, as it currently stands.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: Code: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.log But during script execution I would like the output come back again to screen, how to do that? Thanks Luc edit by bakunin: please use CODE-tags like the... (6 Replies)
Discussion started by: tmonk1
6 Replies

2. Shell Programming and Scripting

How to redirect stderr for <file if file does not exist?

$ a=$(<missing) -bash: missing: No such file or directory $ a=$(<missing) 2> /dev/null -bash: missing: No such file or directory $ a=$(<missing 2> /dev/null) -bash: missing: No such file or directory $ a=$( { <missing 2> /dev/null; } ) -bash: missing: No such file or directory $... (10 Replies)
Discussion started by: Michael Stora
10 Replies

3. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.logBut during script execution I would like the output come back again to screen, how to do that? Thanks Lucas (4 Replies)
Discussion started by: Lord Spectre
4 Replies

4. Shell Programming and Scripting

How to redirect stderr to a file as well

Hello everyone, I'm a nooby in Linux, and I need some help. I have a shell script like this: echo "Start of script" > ../My_Log_Dir/Script_Name.log .. cp ../My_DataIn/File.txt ../My_DataOut/ 2>> ../My_Log_Dir/Script_Name.log rc=$? .. echo "End of Script" >>... (5 Replies)
Discussion started by: H.Faria
5 Replies

5. Shell Programming and Scripting

redirect stdout and stderr to file wrong order problem with subshell

Hello I read a lot of post related to this topic, but nothing helped me. :mad: I'm running a ksh script with subshell what processing some ldap command. I need to check output for possible errors. #!/bin/ksh ... readinput < $QCHAT_INPUT |& while read -p line do echo $line ... (3 Replies)
Discussion started by: Osim
3 Replies

6. Shell Programming and Scripting

Why stderr file descriptor redirection makes ksh's "select" construct hang.

I am trying to use one global declaration --> "exec 2>$ERR" to capture all stderr outputs that may occur anywhere in my script. Then close it at the end of the script using --> "exec 2<&-" I am using KSH on Solaris 8. KSH Version M-11/16/88i If I comment two "exec .." statements in the... (11 Replies)
Discussion started by: kchinnam
11 Replies

7. Shell Programming and Scripting

Redirect stdout/stderr to a file globally

Hi I am not if this is possible: is it possible in bach (or another shell) to redirect GLOBALLY the stdout/stderr channels to a file. So, if I have a script script.sh cmd1 cmd2 cmd3 I want all stdout/stderr goes to a file. I know I can do: ./script.sh 1>file 2>&1 OR ... (2 Replies)
Discussion started by: islegmar
2 Replies

8. UNIX for Dummies Questions & Answers

Redirect just stderr to a file with a timestamp

I'm using below command to redirect stderr to a file but I also want to add timestamp to stderr.out to find out the date / time the error occurred. ls -ltr 2>>/tmp/stderr.out Thanks (5 Replies)
Discussion started by: mbak
5 Replies

9. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

10. Shell Programming and Scripting

how can i redirect stderr to file in Make?

Hello all im using tcsh shell on sun Solaris , using the Make utility for compilation i will like to be able to redirect the stderr to file , how can it be done ? (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question