Redirect within ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect within ksh
# 1  
Old 10-16-2008
Redirect within ksh

I am using ksh on an AIX box.
I would like to redirect the stdout and stderr to a file but also show them on the terminal. Is this possible? I have tried tee within my file without success.
This is the code I have so far

Code:
exec > imp.log 2>&1 | tee exec 1>&1

I am new to shell scripting, so take it easy on me. Smilie
# 2  
Old 10-16-2008
This is an example of one way of doing what you want to do from within a shell script. It is a modified version of a code snippet I picked up some time ago on the Internet, forget where, so apologies to the original author.

Code:
#!/usr/bin/ksh
#
#  Within a shell script set up simultaneous output
#  to both terminal and a file using a FIFO
#

# delete output file
OUTPUT=log
[[ -e $OUTPUT ]] && rm $OUTPUT

# set up redirects
exec 3>&1 4>&2
FIFO=fifo.$$
[[ -e $FIFO ]] || mkfifo $FIFO
tee $OUTPUT < $FIFO >&3 &
PID=$!
exec > $FIFO 2>&1

# rest of your shell script here i.e.
echo "STDOUT `date`"
echo "STDERR `date`" >&2
# to here

# exit tee and clean up
exec 1>&3 2>&4 3>&- 4>&-
wait $PID
rm $FIFO

exit 0

# 3  
Old 10-16-2008
Thank you for the post. I found another way of doing it.
I wrap the whole shell in ()
Like this
#!/bin/ksh
(
....
) 2>&1 | tee imp.log
# 4  
Old 10-17-2008
Yes, that is another way of doing it. However, you need to explicitly handle STDERR wherever it occurs, e.g.
Code:
{
  echo "STDOUT: xxxxxxxxxxxxxxxxx"
  echo "STDERR: xxxxxxxxxxxxxxxxx" >&2
} 2>&1 | tee imp.log


Last edited by fpmurphy; 10-17-2008 at 10:48 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh- redirect stderr to file and then modify the file

I have the following: 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... (4 Replies)
Discussion started by: chipblah84
4 Replies

2. UNIX for Dummies Questions & Answers

Difference Between executing llike ./myscript.ksh and . ./myscript.ksh

Hi , What is the diffence between executing the script like ./myscript.ksh . ./myscript.ksh I have found 2 difference but could not find the reason 1. If i export a variable in myscript.ksh and execute it like . ./myscript.ksh the i can access the other scripts that are present in... (5 Replies)
Discussion started by: max_hammer
5 Replies

3. Shell Programming and Scripting

Redirect o/p from shell

hi, I've a shell which does few manipulations on different text files. I need to divert all the output to a single file. i dont want to mention the o/p file name in command prompt (or) in every shell command. Is it possible to mention it part of the shell? Thanks (2 Replies)
Discussion started by: dvah
2 Replies

4. Shell Programming and Scripting

KSH - mailx - Redirect the undelivered mail

Hi, I need to create one KSH which will send mail to set of recipients using "mailx" command like below. mailx -s "Test mail" "test@yahoo.com, test@gmail.com" <$output.txt The recipients are in different domains (like yahoo, gmail, etc.). My requirement is, if any mail is undelivered,... (1 Reply)
Discussion started by: Matrix2682
1 Replies

5. Shell Programming and Scripting

KSH Redirect to Pipe (">|") Syntax

Occasionally I see this in ksh scripts: somepgm >| someoutputfile This appears to be redirecting the output of somepgm to the file someoutputfile, and it does do so. But when you remove the pipe symbol "|" from this command, output simply goes to the screen (stdout). Why do we need the... (6 Replies)
Discussion started by: Tanuka
6 Replies

6. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

7. Shell Programming and Scripting

KSH problem - how do i redirect three times?

i need to output an ls command to a file but also capture any errors from that command and output them to a log file and the screen. if it's only possible to output them to a log file and not the screen then that's fine. this is what i've tried so far, but it won't populate log.txt. i've... (16 Replies)
Discussion started by: mjays
16 Replies

8. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

9. UNIX for Dummies Questions & Answers

how do redirect in bind

Hello all! Does anyone know how to redirect i link to a host or alias name? Here is the example: i wank to type "Bob" in my browser and be redirected to http://192.168.54.37:7001/Bob/BobMainServlet on that perticular port. Im using Redhat 6.2 with bind 9.2.3 regards... dOzY (4 Replies)
Discussion started by: dozy
4 Replies

10. IP Networking

Redirect

I'm sittig behind a firewall that doesn't allow ftp. I have a conection to a UNIX system, connecting throug SSH. Is it possible to redirect the ftp through the UNIX to my computer? (1 Reply)
Discussion started by: <Therapy>
1 Replies
Login or Register to Ask a Question