Redirecting command output as well as commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting command output as well as commands
# 1  
Old 07-29-2010
Question Redirecting command output as well as commands

I have a Bourne Shell script that is normally run as a background job and redirects it's output to a file internally (using exec >>); I use "set -x" to capture each command which provides me with a nice shell execution log if it all goes to pieces.

I now also need to be able to also run this as an interactive script whilst still keeping the "execution log" concept but have the output from certain commands also printed on the user terminal so they know what's going on during the long sections whilst keeping them in the log as well.

The following Sample Script shows on screen the output from msg() and the output from awk;
'user.log' is fine as a printable logfile;
'exec.log' shows everything BUT the output from awk

QUESTION (finally) - how can I get the output from awk to show on the original script stdout (ie user screen) AND also into exec.log

Sample Script:

Code:
msg() {
    echo `date "+%a %b %d %T"` "$*"  | tee -a user.log 1>&3
}
LOG=exec.log
exec 3>&1                    # remember original stdout (ie user screen)
exec >> $LOG 2>&1        # send stdout/stderr to execution log $LOG
msg "Commenced"          # shows on screen & into 'user.log'
set -x                         # show commands as they run
# use awk to print delayed messages to screen - also want o/p in $LOG
awk 'BEGIN {for(i=0;i<5;i++){printf "\rHelloooo" i;system("sleep 1")};print "";exit }' 1>&3
msg Ended                  # appears on screen, in user.log & $LOG
exit

Any advice/script fragments would be appreciated

Last edited by Scott; 07-29-2010 at 07:34 AM.. Reason: Please use code tags
# 2  
Old 07-29-2010
Code:
awk ... [echo this part to stdout] | tee -a log.txt

Smilie
# 3  
Old 07-29-2010
MySQL

Quote:
awk ... [echo this part to stdout] | tee -a log.txt
I've tried using tee already -

adding 'tee -u -a exec.log' simply sends the output only to exec.log
adding 'tee -u -a exec.log 1>&3' doesn't add anything to exec.log

I need to get the output from 'awk' on the screen AND into exec.log
as well as getting the shell to send it's stderr to exec.log Smilie

Perhaps I should add that I'm testing on SCO UnixSmilie (with a plan on using on SUSE Linux once it works) so I'm really restricted as to what I can do

---------- Post updated at 08:56 AM ---------- Previous update was at 05:45 AM ----------

I've been thinking and found the answer ... not elegant but it works Smilie

create a macro that read characters and echoes them where I need them ensuring that we can't see the commands execute with the macro (could be a separate shell script I suppose to do same)

Code:
listen() {
    exec 6>&2  
    exec 2>&-
    ok=1
    while [ $ok -eq 1 ]; do
        x=`dd bs=1 count=1`
        case "$x" in
        "") ok=0; continue ;;
        esac
        echo -n $x
        echo -n $x >&3
    done
    echo "" >&3
    exec 2>&6
}

and change the 'awk' command to be

Code:
awk 'BEGIN {for(i=0;i<5;i++) {printf "\rHelloooo" i;system("sleep 1") };print "";exit }' | listen

and presto ... I get the output to original stdout AND into my 'exec.log' as I wanted

if you can improve on this please feel free to postSmilie
# 4  
Old 07-29-2010
mmh wouldn't it work if you just redirect the awk output with > into a variable and then echo and print/write it out?
like:
Code:
awk ... > supercoolvariable
echo $supercoolvariable
echo $supercoolvariable > exec.log

# 5  
Old 07-29-2010
I'm using awk here as an example; the program I actually need to use is wodim writing to data a DVD and it takes about 15 minutes; all the time it is interactively outputting status messages that I need the user to see so they won't think it's all gone horribly wrong
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Redirecting output to new file for command "perldoc perllocal"

Hi, I have to redirect output of the command "perldoc perllocal" to new file which contains all the perl module installed. Currently using perldoc perllocal >> mod_data This does not contain all perl modules installed locally on machine, and each character is doubled. Please... (3 Replies)
Discussion started by: asak
3 Replies

2. Linux

RHEL 6.3 - chage command not redirecting the output to console.

When i am issuing chage command, it reporting the output properly. But when i redirect the output, i am not getting the output in the mentioned path. chage -l root >> /tmp/chage.txt. I need to use this into the script to capture the data. I think its seems to be bug with RHEL 6.3. Same... (3 Replies)
Discussion started by: Srini.rk1983
3 Replies

3. Shell Programming and Scripting

Redirecting command output to a file in a shell script

Hello All, I have some unique requirement. I have written a very lengthy script which calls number of resource script to execute a particular task. What I want is output of each command(called from main script and resource scripts) should go to a... (3 Replies)
Discussion started by: anand.shah
3 Replies

4. UNIX for Dummies Questions & Answers

Redirecting the multiple commands output to single file

Hi, I am new to shell scripting and have a question. I would like to redirect the output of multple commands to single file, From what I read from the bash manpage and from some searching it seems it cannot be done within the shell except setting up a loop. Is it? I am running all clearcase... (1 Reply)
Discussion started by: saku
1 Replies

5. Shell Programming and Scripting

Reading UNIX commands from file and redirecting output to a file

Hi All I have written the following script: #!/bin/ksh while read cmdline do echo `$cmdline` pid="$cmdline" done<commands.txt =========== commands.txt contains: ps -ef | grep abc | grep xyz |awk '{print $2}; My objective is to store the o/p of the command in a variable and do... (8 Replies)
Discussion started by: rahulparo
8 Replies

6. Shell Programming and Scripting

Merging output of two commands for a third command

I'm not sure if this is even possible but I'm hoping to avoid generating a temporary file. What I'm trying to do is append a perl command to the start of a list created by grep, then send the entire thing to mail. This is mainly to ensure that something isn't wrong when the list is blank, but it... (2 Replies)
Discussion started by: Chthonic
2 Replies

7. Shell Programming and Scripting

Redirecting output of a command to a file

Hi We are having a requirement where one shell script, say a.sh (which uses Java and connects to Oracle database using JDBC) keeps on running everytime. I created a wrapper (to check whether a.sh is running and if not then to start it) and scheduled it in the crontab. Now all the output from... (3 Replies)
Discussion started by: ankitgoel
3 Replies

8. UNIX for Dummies Questions & Answers

redirecting commands to new xterm

hi, I need help in redirecting the commands to a new xterm. I have a executable, say my_script and another file called command.list that has several commands. my_script contents pwd xterm -e command.list I am expecting, all the commands in command.list needs to be executed in the new... (3 Replies)
Discussion started by: sujith
3 Replies

9. Shell Programming and Scripting

problem redirecting output of command to variable

Hi. I'm a newbie in scripting and i have this problem: i want to use the 'fuser' command on a file to tell if it's being accessed (for my purposes: still being written). I want to save the output of the command and later compare with the 'not being used' result. the script: #!/bin/bash... (2 Replies)
Discussion started by: nunovc
2 Replies

10. Shell Programming and Scripting

Redirecting OUTPUT

Hi, I want to move the output of a command/script to a file as well as to to be displayed on stdout. Can anybody help me in this. Thanks in advace .. -Chanakya M (1 Reply)
Discussion started by: Chanakya.m
1 Replies
Login or Register to Ask a Question