Sponsored Content
Top Forums Shell Programming and Scripting [stdin / stdout] Strategies for redirecting outputs Post 302995680 by MadeInGermany on Monday 10th of April 2017 02:48:58 PM
Old 04-10-2017
Quote:
Originally Posted by Marmz
Well.. let's say i need to write a pretty simple script.

In my script i have 2 variables which can have value of 0 or 1.
  • $VERBOSE
  • $LOG

I need to implement these cases:
  1. ($VERBOSE = 0 && $LOG = 0) => ONLY ERROR output (STDERR to console && STDOUT to /dev/null)
  2. ($VERBOSE = 1 && $LOG = 0) => VERBOSE output (ALL to console)
  3. ($VERBOSE = 0 && $LOG = 1) => LOG output (ALL to file.log)
  4. ($VERBOSE = 1 && $LOG = 1) => BOTH output (ALL to console && ALL to file.log)


For now i got to understand i can do the following to put EVERYTHING to a file
Code:
exec > file.log                                                                    
exec 2>&1

Sure it is a very known argument.. but i'd like to hear some opinion Smilie
Code:
logfile=file.log
if [ $VERBOSE = 0 -a $LOG = 0 ] ; then
  exec 2>> /dev/null
elif [ $VERBOSE = 1 -a $LOG = 0 ] ; then
  :
elif [ $VERBOSE = 0 -a $LOG = 1 ] ; then
  >> $logfile 2>&1
else
  echo "output goes to $logfile"
  >> $logfile
fi

For the latter case to go to *both* file and console, you would need a pipe to tee for the following code, e.g. by means of enclosing the code with { }:
Code:
{
# block starts, see redirection at the end
: code
# redirect this block to
} 2>&1 | tee -a $logfile

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting to stdin

Hi, I'm having trouble with my script. I have to select different choices without any interaction from a menu that looks like : a - xxxxxxxxxxxxxx b - xxxxxxxxxxxxxx c - xxxxxxxxxxxxxx d - xxxxxxxxxxxxxx I tried things like : echo "a" >&0 read < echo "a" but none worked. Any... (4 Replies)
Discussion started by: flame_eagle
4 Replies

2. Programming

stdout/stdin + flushing buffers

Hi all I've run into a snag in a program of mine where part of what I entered in at the start of run-time, instead of the current value within printf() is being printed out. After failing with fflush() and setbuf(), I tried the following approach void BufferFlusher() { int in=0;... (9 Replies)
Discussion started by: JamesGoh
9 Replies

3. 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

4. Shell Programming and Scripting

Redirecting stdin from fd 3-9?

Hi I'm trying to do something on the bash command line that I will later put into a bash shell script. I'm trying to take a program that reads stdin (using getline) and be able to keep it running in the background and fire "commands" to it. So what I thought I should do was to try taking... (3 Replies)
Discussion started by: niceguyeddie
3 Replies

5. UNIX for Dummies Questions & Answers

Redirect stdin stdout to multiple files

Hi, i know how to a) redirect stdout and stderr to one file, b) and write to two files concurrently with same output using tee command Now, i want to do both the above together. I have a script and it should write both stdout and stderr in one file and also write the same content to... (8 Replies)
Discussion started by: ysrini
8 Replies

6. Shell Programming and Scripting

can't close stdin/stdout in shell

#!/bin/sh exec 0</dev/null exec 1>/dev/null ls -l /proc/self/fd >&2 produces total 0 lr-x------ 1 tyler users 64 Feb 18 10:38 0 -> /proc/7886/fd lrwx------ 1 tyler users 64 Feb 18 10:38 1 -> /dev/pts/4 lrwx------ 1 tyler users 64 Feb 18 10:38 2 -> /dev/pts/4 I've verified the shell is... (10 Replies)
Discussion started by: Corona688
10 Replies

7. Shell Programming and Scripting

Redirecting stdin/stdout to/from command from/to string

Hi, I am working on a project where I have to generate and execute nasm code on-the-fly. I generate the code in a file program.asm and then execute it.This output is to stdout which i redirect to an output file which i read back to compare results: system("nasm -f elf program.asm >... (5 Replies)
Discussion started by: doc_cypher
5 Replies

8. Programming

read and write stdin/stdout in unix

Hi, i am using the below program to read from the standard input or to write to standard out put. i know that using highlevel functions this can be done better than what i have done here. i just want to know is there any other method by which i find the exact number of characters ( this... (3 Replies)
Discussion started by: MrUser
3 Replies

9. UNIX for Dummies Questions & Answers

multiple variables assignement (stdout/stderr outputs)

Hi all, I've been looking around for this for a while and can't seem to find a satifactory way to do what I want: I would like to assign the output of stdout to a variable and that of stderr to another one, and this without using temporary files/named pipes. In other words be able to assign... (4 Replies)
Discussion started by: anthalamus
4 Replies

10. UNIX for Dummies Questions & Answers

STDIN and STDOUT

Hallo, i have a script like: if ;then echo "OK" else echo "ERROR $2 is missing" fi; if ;then touch $2 fi; if ;then cat $1 | grep xy > $2 (1 Reply)
Discussion started by: eightball
1 Replies
CPANPLUS::Error(3)					User Contributed Perl Documentation					CPANPLUS::Error(3)

NAME
CPANPLUS::Error - error handling for CPANPLUS SYNOPSIS
use CPANPLUS::Error qw[cp_msg cp_error]; DESCRIPTION
This module provides the error handling code for the CPANPLUS libraries, and is mainly intended for internal use. FUNCTIONS
cp_msg("message string" [,VERBOSE]) Records a message on the stack, and prints it to "STDOUT" (or actually $MSG_FH, see the "GLOBAL VARIABLES" section below), if the "VERBOSE" option is true. The "VERBOSE" option defaults to false. msg() An alias for "cp_msg". cp_error("error string" [,VERBOSE]) Records an error on the stack, and prints it to "STDERR" (or actually $ERROR_FH, see the "GLOBAL VARIABLES" sections below), if the "VERBOSE" option is true. The "VERBOSE" options defaults to true. error() An alias for "cp_error". CLASS METHODS
CPANPLUS::Error->stack() Retrieves all the items on the stack. Since "CPANPLUS::Error" is implemented using "Log::Message", consult its manpage for the function "retrieve" to see what is returned and how to use the items. CPANPLUS::Error->stack_as_string([TRACE]) Returns the whole stack as a printable string. If the "TRACE" option is true all items are returned with "Carp::longmess" output, rather than just the message. "TRACE" defaults to false. CPANPLUS::Error->flush() Removes all the items from the stack and returns them. Since "CPANPLUS::Error" is implemented using "Log::Message", consult its manpage for the function "retrieve" to see what is returned and how to use the items. GLOBAL VARIABLES
$ERROR_FH This is the filehandle all the messages sent to "error()" are being printed. This defaults to *STDERR. $MSG_FH This is the filehandle all the messages sent to "msg()" are being printed. This default to *STDOUT. perl v5.16.3 2013-05-20 CPANPLUS::Error(3)
All times are GMT -4. The time now is 11:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy