[stdin / stdout] Strategies for redirecting outputs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [stdin / stdout] Strategies for redirecting outputs
# 1  
Old 04-09-2017
[stdin / stdout] Strategies for redirecting outputs

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

Last edited by Marmz; 04-10-2017 at 12:09 PM..
# 2  
Old 04-10-2017
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum under special homework rules.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

If you did not post homework, please explain the company you work for and the nature of the problem you are working on. And, tell us what operating system you're using, tell us what shell you're using, and tell us how redirecting STDIN (i.e. Standard Input) has anything to do with controlling where normal and diagnostic output should be written?
# 3  
Old 04-10-2017
I apologize if this is not the right place for my question.

This is not a real job .. i'm developing this not for money, but only for personal study.
My intent was to ask for some opinion and see how different people approach this common situation.

For what concerns STDIN redirection... well it was my error Smilie
of course i meant STDOUT (just edited..)

If i should move / delete this post, just tell me and i will do.
Sorry again for misunderstanding the rules
# 4  
Old 04-10-2017
Since this isn't a homework assignment, this is a perfectly reasonable forum for this question.

Have you looked at the man page for the logger utility on your system?

Do you really want output from your script to be physically directed to the console when you specify that the output should go to the console, or do you just want output you specified as going to the console to be sent to the script's standard output (so whoever invokes your script can redirect the output wherever they want it to go)?

If you want output to appear in two places, you should consider looking at the man page for the tee utility.
# 5  
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

# 6  
Old 04-10-2017
My take on this is to first lay down some rules:

1) I do not want to interfere with normal redirection, i.e. if i enter the command:

Code:
# myscript.sh > /some/log

I expect the script not to "overrule" this and put the output to stdout somewhere else than /some/log. It might write it somewhere else too, but finally whatever the output is should go to the file i specified. Analogous for stderr

2) I would like to encapsulate this whole functionality as much as possible. It would be OK to call some_function instead of, say echo, but i would try the utmost to avoid to "decorate" every single output-statement like this:

Code:
if [ $VERBOSE -eq 0 ] ; then
    if [ $LOG -eq 0 ] ; then
       .....
    else
       .....
    fi
else
     if [ $LOG -eq 0 ] ; then
       .....
    else
       .....
    fi
fi

3) If i already go through the trouble of writing a wrapper function for my output i could as well automatically add more than the "naked" output i send as an argument: a timestamp, maybe the process number, name of the calling function or similar information might come in handy when trying to figure out why a script has failed and what exactly has and has not done. If i write in a script:

Code:
....
if do_something ; then
     out_function "successfully executed the something step"
else
     out_function "executing the something step FAILED"
fi

I would like to see exactly this at the screen output (or the file i redirected it at) but maybe something like:

Code:
1.1.2017 00:10 (123456) ERROR: executing the something step FAILED

where (123456) is the process number of the script this was part of.

Having said all this i don't want to spoil your fun of writing such a function, so i will stop here. Post your production once you have it and i will be glad to discuss it with you. In fact i have written (and used) exactly such a function(-set) in my scripts for years, so kudos for your insight to feel the necessity for such a thing.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question