Sponsored Content
Top Forums Shell Programming and Scripting Problems with I/O redirection via exec Post 302950148 by Clovis_Sangrail on Tuesday 21st of July 2015 12:26:13 PM
Old 07-21-2015
Problems with I/O redirection via exec

Hello,
I am running a shell script on AIX 6.1. The script calls ksh to run. ksh is also the login shell for the account under which I am running this script, but for convenience I always change to the bash shell via "exec bash" after I "su" to the account.

The script redirects stdout and stderr to a file via exec, then builds a report via a bunch of "echo" and "awk" commands, and then finally reconnects to stdout and stderr and emails the report:

Code:
#!/usr/bin/ksh
#set -x
   .   .   .   .
STAMP=`date "+%H%M%S"`.$$               # Unique suffix for tmp files.
RPTF=/tmp/arch_cmp_rpt.$STAMP           # Seq chk report file.
   .   .   .   .
exec 2>&1 > $RPTF                       # Capture std output and stderr.
   .   .   .   .
exec                                    # Restore stdout/stderr.
cat $RPTF | /usr/sbin/sendmail recip@myemployer.com
                                        # Pipe report to sendmail.
rm $RPTF                                # Clean up.

I run into difficulties when I try to run this script (archcmp.sh) in the background via "./archcmp.sh &" . I can do so, and the script does run, but as soon as I type anything at the terminal (like an 'ls -l' on the report file in /tmp) the script stops and I cannot resume it. I see the same behavior whether or not I switch from the ksh login shell to bash before running the report:

Code:
$ ./archcmp.sh &
[1] 12189786
ls -l /tmp/arch_cmp_rpt.115918.12189786 
-rw-rw----    1 eprprde  profile         700 Jul 21 11:59 /tmp/arch_cmp_rpt.115918.12189786
[1]+  Stopped                 ./archcmp.sh
$ bg
[1]+ ./archcmp.sh &
ls -l /tmp/arch_cmp_rpt.115918.12189786 
-rw-rw----    1 eprprde  profile         700 Jul 21 11:59 /tmp/arch_cmp_rpt.115918.12189786
$ date
Tue Jul 21 12:00:43 EDT 2015
$ date
Tue Jul 21 12:05:46 EDT 2015
$ ls -l /tmp/arch_cmp_rpt.115918.12189786 
-rw-rw----    1 eprprde  profile         700 Jul 21 11:59 /tmp/arch_cmp_rpt.115918.12189786
$

It will never write anything more to the report file.

I find that I can get my program to behave the way that it should (or, at least, the way that I would like it to) if I change I redirecting 'exec' command to redirect all three of the standard file handles, as follows:

Code:
exec 2>&1 >$RPTF </dev/null

But this should be unnecessary, shouldn't it? When I place a command into the background via '<commandname> &' , it should be unconditionally dissociated from the terminal. Is this a bug in ksh? Is there some other way to get the background process to actually resume?

These are the OS & shell versions:

Code:
$ uname -a
AIX servername 1 6 00C7252F4C00
$ 
$ set -o vi
$ Version M-11/16/88f
$ bash --version 
GNU bash, version 3.2.16(1)-release (powerpc-ibm-aix5.2.0.0)
Copyright (C) 2005 Free Software Foundation, Inc.
$

Thanks for any explanation you can provide.
 

10 More Discussions You Might Find Interesting

1. Programming

Help with redirection

Here is my problem. I don't know make this redirection thing work. The output file (called output.c) looks like this #include<stdio.h> int main() { int k; int m; print f("%d\n", k); printf("%d\n", m); return 0; } the input file(called input.c) is this #include<stdio.h> int... (2 Replies)
Discussion started by: Shallon1
2 Replies

2. Shell Programming and Scripting

redirection

Hi, The code below works, it's a part of a bash shell script that serve to search a pattern $pattern_da_cercare in the files contained in a directory $directory_iniziale. Now the proble is: How can I redirect stderr to a file? PS: so I want to redirect ALL the errors to a file. I tryed... (9 Replies)
Discussion started by: DNAx86
9 Replies

3. UNIX for Dummies Questions & Answers

Help with Redirection

Hi Guys, I m new to UNIX and new to this forum. Was wondering if someone can help me understand redirection (standard input output pipeline etc) for starters, not too sure what this would mean who | sort > sortedfile | pr | lp im starting to understand common commands but when throwing... (2 Replies)
Discussion started by: jmack123
2 Replies

4. Shell Programming and Scripting

Output redirection to exec does not work

Hello Experts, I am on Solaris 10 Due to some limitations in one of the vendor software, I am forced to output the command to exec and then run it from there. For example.. $(echo "/usr/bin/cp a.dat b.dat") # This works However, $(echo "/usr/bin/cat a.dat > c.dat") # This does not... (8 Replies)
Discussion started by: Gokul Kumar G
8 Replies

5. Shell Programming and Scripting

Double redirection

Hi to all. It's possible with a single line redirect to stdout and to a file a echoed string? I need something like this: echo "Pizza" >/tmp/file (and same time print to stout "Pizza")... What can i do? (2 Replies)
Discussion started by: mendez
2 Replies

6. Shell Programming and Scripting

I/O redirection

Hello everyone,I'm reading a book and there's code fragment: exec 3>&1 ls -l 2>&1 >&3 3>&- | grep bad 3>&- exec 3>&- It says that the red part of that code does not close fd 3 but the green does close the fd 3.I can't understand that.....Why?Any predicate will be appreciated.:) (18 Replies)
Discussion started by: homeboy
18 Replies

7. Shell Programming and Scripting

Redirection

Hello All, I am using the below script to gather various tools running by the user, we have more than 100 tools running on the server so my challenge is to redirect memory & cpu load to the file with the name of the tool.so am using the below script i am stucking how to redirect to the file... (2 Replies)
Discussion started by: ajaincv
2 Replies

8. UNIX for Dummies Questions & Answers

about different redirection

explain the redirections 1>, 2>, 3>, ..... and 1< ,2<,3<..... where we use these things thanks Thread moved from AIX forum (2 Replies)
Discussion started by: tsurendra
2 Replies

9. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

10. Shell Programming and Scripting

Several exec on find send all the output to the last redirection

Example script: find mydir -type f -exec echo {}>aaa \; -exec echo {}>bbb \;The two paths go the the bbb file, while there should be one of them on each file. How should I do it to get it working? (2 Replies)
Discussion started by: Tribe
2 Replies
All times are GMT -4. The time now is 11:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy