Sponsored Content
Top Forums Shell Programming and Scripting Very Strange Behavior for redirection Post 302130562 by cahook on Monday 6th of August 2007 06:57:20 PM
Old 08-06-2007
Very Strange Behavior for redirection

I have searched far and wide for an explanation for some odd behavior for output redirection and haven't come up with anything.

A co-worker was working on old scripts which have run for years and embedded in their code were output redirects which worked for the script during execution and then reset back to normal output after the script completed. He tested the same script on a Solaris 10 box and found that the redirection continued after the script finished. I think that the redirection being persistent is normal, (one expects items set in a shell script to remain after execution i.e. .profiles etc.) the return to normal output seems to be the oddity. I have tested on several different boxes featuring solaris 8 9 and 10, and I get the same results everytime, the redirection persists.

Redirecting stdout works and stdout is redirected properly to the file. However, when trying a command like 2>&1 1>filename, this has the effect of redirecting stdout, but not stderr. (In testing the stderr came back to the terminal, and only stdout stuff went to the file)

If you run the code below, and you seem to lose the standard output, stdout can be recaptured to the tty using exec 1>/dev/tty on the command line and things get back to normal.

When stderr is redirected instead however, all output goes to the file, and the result feels like the session has hung. There doesn't seem to be any interaction. Closing the terminal seems to be the only option

I am interested to know if anyone else can replicate this behavior on their environments, and if anyone can suggest some reasons for it.

OS = Solaris 8,9,10
Arch = SPARC (Fujitsu clone)


Thanks


####################################
## CODE LINES
####################################


#!/bin/ksh

echo "Begin Testing, output to TTY"


# uncomment the following line to test stdout redirection
exec 1> output.file
echo "This should be in the file"

# uncomment the following line to test stderror redirection
# exec 2> output.file

#uncomment the following line to test dual redirection
#exec 2>$1 1>output.file

# Create an Error that should go out to the file
cat asdfasdfasdf


############################################
## END CODE
############################################

###
TESTING PROTOCOL


## The command line is :
>. test.sh
Begin Testing, output to TTY
cat: cannot open asdfasdfasdf

## after the script completes, try to obtain some output to the terminal

> ls -al

No result

> exec 1>/dev/tty

Recapture the stdout to the terminal screen

> cat output.file

This should be in the file
total 12
-rw-r--r-- 1 cogcrn cogcrn 0 Aug 6 16:27 -
drwxr-xr-x 2 cogcrn cogcrn 512 Aug 6 16:27 .
drwxr-xr-x 25 cogcrn cogcrn 1536 Aug 6 16:09 ..
-rwxr-xr-x 1 cogcrn cogcrn 969 Jun 13 13:00 ldap_query.pl
-rw-r--r-- 1 cogcrn cogcrn 27 Aug 6 18:52 output.file
-rw-r--r-- 1 cogcrn cogcrn 0 Aug 6 16:26 TERM
-rw-r--r-- 1 cogcrn cogcrn 394 Aug 6 18:49 test.sh
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

strange sed behavior

I have a file called products.kp which contains, for example, 12345678,1^M 87654321,2^M 13579123,3 when I run the command cat products.kp| sed -f kp.sed where kp.sed contains s,^M,, I get the output 12345678,1 87654321,2 13579123,3 (5 Replies)
Discussion started by: Kevin Pryke
5 Replies

2. UNIX for Dummies Questions & Answers

Strange Behavior on COM2

Hi, I have a problem with a new touch screen controller that I am trying to use on a SCO 3.0 system. THe touch screen controller only wants to talk at 9600baud. I have updated /etc/inittab per the manual and also edited /usr/lib/event/devices to use 9600 baud. The only way I can get the... (0 Replies)
Discussion started by: Elwood51
0 Replies

3. Programming

Strange behavior in C++

I have the following program: int main(int argc, char** argv){ unsigned long int mean=0; for(int i=1;i<10;i++){ mean+=poisson(12); cout<<mean<<endl; } cout<<"Sum of poisson: "<< mean; return 0; } when I run it, I get the... (4 Replies)
Discussion started by: santiagorf
4 Replies

4. Red Hat

strange mail behavior

Hi I have script to to take backup and send mail to a group once a day. One strange behavior I have observed recently is that most of the time the mail we receive is fine . But someday it just sends out mail without any subject with undisclosed recipients. I dont know how to find the cause... (0 Replies)
Discussion started by: ningy
0 Replies

5. Shell Programming and Scripting

Insane redirection behavior

Hi guys, I know computers don't misbehave. But I'm puzzled by what's happening right know in a script : I simplified the example to point out what seems weird to me. Don't try to find any sense to this stupid script. There are 10 rows in /tmp/tmp.txt i=0 tmpfile=/tmp/tmp.txt while... (3 Replies)
Discussion started by: chebarbudo
3 Replies

6. AIX

Strange memory behavior

Hello together, i have a strange memory behavior on a AIX 7.1 System, which i cannot explain. The Filesystem-Cache will not be grow up and drops often after few minutes. I know if a file was deleted, that the same segment in the FS-Cache will also be cleared. But i am not sure if this is the... (8 Replies)
Discussion started by: -=XrAy=-
8 Replies

7. Shell Programming and Scripting

Strange behavior on one of my server

I am not sure what is wrong, but I have some strange behavior when printing things out. I do create a file with only one word test, no space, no new line etc. nano file<enter> test<ctrl x>y<enter> Server 1 gets (fail) awk '{print "+"$0"*"}' file *test Server 2 gets (OK) awk '{print... (9 Replies)
Discussion started by: Jotne
9 Replies

8. Shell Programming and Scripting

Strange behavior of grep

Hi All, I am facing a strange problem while grepping for a process. Here is the small script that i have written. It will look for any process running with the parameter passed to the script. If no process is running it should print appropriate message. $ cat t.ksh #!/bin/ksh set -x ... (9 Replies)
Discussion started by: veeresh_15
9 Replies

9. Shell Programming and Scripting

Strange Ctrl+C behavior

Hello All, I have a strange issue. I've created a shell script which connects to RMAN (Oracle Recovery Manager) and executes full DB backup. I then executed this script with nohup and in the background: $ nohup my_script.sh > logfile.log 2>&1 &The issue is that when I tried to take a look into... (6 Replies)
Discussion started by: JackK
6 Replies
SCRIPT(1)							   User Commands							 SCRIPT(1)

NAME
script - make typescript of terminal session SYNOPSIS
script [options] [file] DESCRIPTION
script makes a typescript of everything displayed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). If the argument file is given, script saves the dialogue in this file. If no filename is given, the dialogue is saved in the file type- script. OPTIONS
-a, --append Append the output to file or to typescript, retaining the prior contents. -c, --command command Run the command rather than an interactive shell. This makes it easy for a script to capture the output of a program that behaves differently when its stdout is not a tty. -e, --return Return the exit code of the child process. Uses the same format as bash termination on signal termination exit code is 128+n. -f, --flush Flush output after each write. This is nice for telecooperation: one person does `mkfifo foo; script -f foo', and another can supervise real-time what is being done using `cat foo'. --force Allow the default output destination, i.e. the typescript file, to be a hard or symbolic link. The command will follow a symbolic link. -q, --quiet Be quiet (do not write start and done messages to standard output). -t[file], --timing[=file] Output timing data to standard error, or to file when given. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can be used to replay typescripts with realistic typing and output delays. -V, --version Display version information and exit. -h, --help Display help text and exit. NOTES
The script ends when the forked shell exits (a control-D for the Bourne shell (sh(1)), and exit, logout or control-d (if ignoreeof is not set) for the C-shell, csh(1)). Certain interactive commands, such as vi(1), create garbage in the typescript file. script works best with commands that do not manipulate the screen, the results are meant to emulate a hardcopy terminal. It is not recommended to run script in non-interactive shells. The inner shell of script is always interactive, and this could lead to unexpected results. If you use script in the shell initialization file, you have to avoid entering an infinite loop. You can use for example the .profile file, which is read by login shells only: if test -t 0 ; then script exit fi You should also avoid use of script in command pipes, as script can read more input than you would expect. ENVIRONMENT
The following environment variable is utilized by script: SHELL If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically). SEE ALSO
csh(1) (for the history mechanism), scriptreplay(1) HISTORY
The script command appeared in 3.0BSD. BUGS
script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects. script is primarily designed for interactive terminal sessions. When stdin is not a terminal (for example: echo foo | script), then the session can hang, because the interactive shell within the script session misses EOF and script has no clue when to close the session. See the NOTES section for more information. AVAILABILITY
The script command is part of the util-linux package and is available from Linux Kernel Archive <https://www.kernel.org/pub/linux/utils /util-linux/>. util-linux June 2014 SCRIPT(1)
All times are GMT -4. The time now is 06:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy