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
NPM-RUN-SCRIPT(1)                                                                                                                NPM-RUN-SCRIPT(1)

NAME
npm-run-script - Run arbitrary package scripts SYNOPSIS
npm run-script <command> [--silent] [-- <args>...] alias: npm run DESCRIPTION
This runs an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts. run[-script] is used by the test, start, restart, and stop commands, but can be called directly, as well. When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts. As of ` https://blog.npmjs.org/post/98131109725/npm-2-0-0, you can use custom arguments when executing scripts. The special option -- is used by getopt https://goo.gl/KxMmtG to delimit the end of the options. npm will pass all the arguments after the -- directly to your script: npm run test -- --grep="pattern" The arguments will only be passed to the script specified after npm run and not to any pre or post script. The env script is a special built-in command that can be used to list environment variables that will be available to the script at run- time. If an "env" command is defined in your package, it will take precedence over the built-in. In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix. For example, if there is a devDependency on tap in your package, you should write: "scripts": {"test": "tap test/*.js"} instead of "scripts": {"test": "node_modules/.bin/tap test/*.js"} to run your tests. The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of ` https://github.com/npm/npm/releases/tag/v5.1.0 you can customize the shell with the script-shell configuration. Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run. npm run sets the NODE environment variable to the node executable with which npm is executed. Also, if the --scripts-prepend-node-path is passed, the directory within which node resides is added to the PATH. If --scripts-prepend-node-path=auto is passed (which has been the default in npm v3), this is only performed when that node executable is not found in the PATH. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install, just in case you've forgotten. You can use the --silent flag to prevent showing npm ERR! output on error. You can use the --if-present flag to avoid exiting with a non-zero exit code when the script is undefined. This lets you run potentially undefined scripts without breaking the execution chain. SEE ALSO
o npm help 7 scripts o npm help test o npm help start o npm help restart o npm help stop o npm help 7 config January 2019 NPM-RUN-SCRIPT(1)
All times are GMT -4. The time now is 01:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy