Sponsored Content
Top Forums Shell Programming and Scripting File descriptors, redirecting output, and stdout Post 302675703 by Corona688 on Monday 23rd of July 2012 12:07:51 PM
Old 07-23-2012
Instead of redirecting to 3 and 4 and using 'print -u4', you could've redirected stdout and stderr themselves. with 'exec 1>' and 'exec 2>' respectively, and not modified your print commands at all.

Of course, since you seem to wish to preserve stdout/stderr now, it'd be better to keep stdout/err as is.

A redirection can't cause something to print twice. If you want to print twice, you have to print twice. You can make a function to do so, however, simplifying things for you.

Code:
function output
{
        print "$@" >&3
        [ ! -z "$DEBUG" ] && print "$@"
}

function debug
{
        print "$@" >&4
        [ ! -z "$DEBUG" ] && print "$@" >&2
}

If you want to see the output appear on the screen, run it like DEBUG=1 ./myscript

or export the DEBUG variable in your shell or profile.

Last edited by Corona688; 07-23-2012 at 01:13 PM..
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Linux

vgscan produces no output using file descriptors on Oracle Enterprise Linux.

I wrote a simple program which will create a child process to execute a command and the output will be redirected to the file. Please have a look at the following code -> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> void execute(char **argv) { ... (0 Replies)
Discussion started by: sandiworld
0 Replies

2. Shell Programming and Scripting

Redirecting STDERR message to STDOUT & file at same time

Friends I have to redirect STDERR messages both to screen and also capture the same in a file. 2 > &1 | tee file works but it also displays the non error messages to file, while i only need error messages. Can anyone help?? (10 Replies)
Discussion started by: vikashtulsiyan
10 Replies

3. Shell Programming and Scripting

implicitly redirecting stdout to a file

Is there a way to redirect all stdout to a file implicitly - like defining stdout=/home/me/process.log - so that all "echo" commands in several scripts/subscripts are written to that file; instead of having to edit all scripts to redirect the "echo" (e.g. echo 'This is a test ' >>... (1 Reply)
Discussion started by: ALTRUNVRSOFLN
1 Replies

4. Shell Programming and Scripting

Redirecting part of output to stdout

Hi, I am trying to execute a command like this: find ./ -name "*.gz" -exec sh -c 'zcat {} | awk -f parse.awk' \; >> output If I want to print the filename, i generally use the -print argument to the find command but when I am redirecting the output to a file, how can I print just the... (2 Replies)
Discussion started by: Legend986
2 Replies

5. UNIX for Dummies Questions & Answers

Redirecting several outputs to /dev/stdout

I have an executable that, depending on its input, outputs to either one file or several. It usually prints nothing on screen. The usual way to call this program is to specify an input and output filenames, like this: ./executable.exe -i inputfile -o outputfileIt will then try to use the output... (1 Reply)
Discussion started by: aplaydoc
1 Replies

6. Shell Programming and Scripting

Preserve output order when redirecting stdout and stderr

Hi, I already searched through the forum and tried to find a answer for my problem but I didn't found a full working solution, thats way I start this new thread and hope, some can help out. I wonder that I'm not able to find a working solution for the following scenario: Working in bash I... (8 Replies)
Discussion started by: Boemm
8 Replies

7. Shell Programming and Scripting

Redirecting stdout problem

I have a simple bash script that prints sth every 5 seconds. What I do is the following. I redirect the output of the script to a file, tail the file and see that it works and then from another console I delete the file where the output is redirected to. Even though I have deleted the file, the... (2 Replies)
Discussion started by: igurov
2 Replies

8. Shell Programming and Scripting

Redirecting stdout continously to a file

I have a C program that continously outputs info to stdout. The problem is that I am redirecting the stdout and stderr to a file and stdout is written at the end of the problem rather than continously to the file. This could be a problem if for example the program is killed and the stdout output is... (3 Replies)
Discussion started by: igurov
3 Replies

9. Shell Programming and Scripting

Redirecting STDERR to file and screen, STDOUT only to file

I have to redirect STDERR messages both to screen and also capture the same in a file but STDOUT only to the same file. I have searched in this formum for a solution, but something like srcipt 3>&1 >&2 2>&3 3>&- | tee errs doesn't work for me... Has anyone an idea??? (18 Replies)
Discussion started by: thuranga
18 Replies

10. Shell Programming and Scripting

Redirecting stdout output to whiptail menu box

As a result of whiptail menu option I am getting a data from a file. Naturally it is output to terminal as stdour. I like to redirect the output back to the menu. It can be done with single input of line of text , see attached. I just cannot see where or how the sample... (0 Replies)
Discussion started by: annacreek
0 Replies
MakeMethods::Template::PackageVar(3pm)			User Contributed Perl Documentation		    MakeMethods::Template::PackageVar(3pm)

NAME
Class::MakeMethods::Template::PackageVar - Static methods with global variables SYNOPSIS
package MyObject; use Class::MakeMethods::Template::PackageVar ( scalar => [ 'foo' ] ); package main; MyObject->foo('bar') print MyObject->foo(); $MyObject::foo = 'bazillion'; print MyObject->foo(); DESCRIPTION
These meta-methods provide access to package (class global) variables. These are essentially the same as the Static meta-methods, except that they use a global variable in the declaring package to store their values. Common Parameters: The following parameters are defined for PackageVar meta-methods. variable The name of the variable to store the value in. Defaults to the same name as the method. Standard Methods The following methods from Generic should all be supported: scalar string string_index (?) number boolean bits (?) array (*) hash (*) tiedhash (?) hash_of_arrays (?) object (?) instance (?) array_of_objects (?) code (?) code_or_scalar (?) See Class::MakeMethods::Template::Generic for the interfaces and behaviors of these method types. The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass. The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. PackageVar:vars This rewrite rule converts package variable names into PackageVar methods of the equivalent data type. Here's an example declaration: package MyClass; use Class::MakeMethods::Template::PackageVar ( vars => '$DEBUG %Index' ); MyClass now has methods that get and set the contents of its $MyClass::DEBUG and %MyClass::Index package variables: MyClass->DEBUG( 1 ); MyClass->Index( 'foo' => 'bar' ); perl v5.10.1 2004-09-06 MakeMethods::Template::PackageVar(3pm)
All times are GMT -4. The time now is 07:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy