Conditional output redirection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional output redirection
# 1  
Old 05-11-2016
Conditional output redirection

Hi there,

I'm in a coding mood!
I've come through many ways of conditionally redirect the output of a script like:
Code:
[[ $verbose ]] || exec &> /dev/null

But my goal is slightly different. I would like to conditionally redirect the output of a command depending on the status of the command itself:
Code:
if ! mycommand; then exec &>/dev/null; fi

Obviously this does NOT work. The output of mycommand is printed whatever the status of mycommand is. The output is silenced if mycommand fails but it only applies to subsequent commands.

Any idea how I can achieve this?

Best regards
Santiago

---------- Post updated at 15:24 ---------- Previous update was at 15:15 ----------

I have two extra conditions:

1. I cannot store the output of mycommand in a variable:
Code:
if output=$(mycommand); then echo "$output"; fi

The problem is that mycommand outputs binary data including the null character.

2. I' rather not use a temporary file:
Code:
if mycommand > /tmp/file; then cat /tmp/file; fi

Just because I don't like it

Last edited by chebarbudo; 05-11-2016 at 10:25 AM.. Reason: syntax error
# 2  
Old 05-12-2016
You can't have your cake and eat it. The exit code of a program is available when the program is finished running. The output redirection has to be done before the program produces its output.

You could try to write a wrapper around your program, which catches it standard output internally, and after the program is executed, decides what to do with it. Such a wrapper could, for example, be written with `expect`, if the output is large, or you can stuff it into a shell variable. Of course in both cases it means that the interim output is stored somewhere, be it in a variable or in a file.
# 3  
Old 05-12-2016
Thanks rovf for the explanation about the fact that redirecting has to be done before the program produces its output.

Again I had a problem using a variable because myprogram produces binary output including the nul character. So I came up with this wrapper that stores the output in a variable in plain hexadecimal.

Code:
if output="$(
    (
        (
            (
                mycommand && echo 0 >&3 || echo $? >&3
            ) | xxd -p | tr -d $'\n' >&4
        ) 3>&1 | ( read xs; exit $xs )
    ) 4>&1
)"; then
    echo -ne "$output" | xxd -r -p
else
    return $?
fi

What do you think?
# 4  
Old 05-12-2016
It is a bit odd (though not forbidden) to use stdout as a binary stream. What are you doing with the data afterwards? It is most likely that this data - being not human-readable - will be processed by another program eventually, and since it is binary data, it will be read blockwise. Unless you pipe it immediately into another program, the most likely case is to store it into a file. That's what files are for, after all.

The NUL character by itself should not be a problem, because every program where the stdout of your original program is piped in, should set its stdin to binary and read the data blockwise.
# 5  
Old 05-12-2016
mycommand outputs a list of files in a human readable form (newline separated). But I might run into weird filenames so I added the option --null to mycommand so it prints files separated by the NUL character. What I do with the data is:
Code:
mycommand --null | xargs --null cp --target-directory="$mydir"

mycommand fails in specific situations and I don't want any output to be piped to cp in such a case.

I could for sure use a temporary file but I like (as a personal fanciness) to avoid that.

Does it make any sense in your opinion?

Last edited by chebarbudo; 05-12-2016 at 09:48 AM..
# 6  
Old 05-12-2016
Quote:
Originally Posted by chebarbudo
Does it make any sense in your opinion?
Not really. As I said before, in order to recommend any approach, one needs to know what you are eventually going to do with the binary output of your program. You didn't say anything about this.
# 7  
Old 05-12-2016
Quote:
Originally Posted by chebarbudo
mycommand outputs a list of files in a human readable form (newline separated). But I might run into weird filenames so I added the option --null to mycommand so it prints files separated by the NUL character. What I do with the data is:
Code:
mycommand --null | xargs --null cp --target-directory="$mydir"

mycommand fails in specific situations and I don't want any output to be piped to cp in such a case.

I could for sure use a temporary file but I like (as a personal fanciness) to avoid that.

Does it make any sense in your opinion?
Running a command substitution invoking three external processes storing an unspecified amount of data into a variable that might cause an ARG_MAX overflow error when decoding it seems to me to be a slow, convoluted, and dangerous replacement for a much simpler script using a temp file as in:
Code:
$!/bin/bash
mycommand --null > mycommand.tmp.$$ && xargs --null cp --target-directory="$mydir" < mycommand.tmp.$$
xs=$?
rm -f mycommand.tmp.$$
exit $xs

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk conditional output

Hello, How can I use a conditional to produce an output file that varies with respect to the contents of column #4 in the data file: Data file: 9780020080954 9.95 0.49 AS 23.3729 9780020130857 9.95 0.49 AS 23.3729 9780023001406 22.20 0.25 AOD ... (12 Replies)
Discussion started by: palex
12 Replies

2. Shell Programming and Scripting

output redirection

Hi all I was wondering if there was a slicker way of doing this without the file - awk '{print $2}' FS=":" "${FILE}" > "${TMPFILE}" { read M_GRP_ID || m_fail 1 "Error: Read failed 1 (${FUNCNAME})" read M_GRP_WAIT || m_fail 1 "Error: Read failed 2 (${FUNCNAME})" }... (6 Replies)
Discussion started by: steadyonabix
6 Replies

3. Shell Programming and Scripting

Redirection of ls -l output

Hi I am making a script where i want to redirect the output of ls -l to a file Example #ls -l fil1.txt > /opt/temp/a.txt ac: No such file or directory I want to capture output of this command like here output is ac: No such file or directory can anyone help (4 Replies)
Discussion started by: anish19
4 Replies

4. UNIX for Dummies Questions & Answers

Output redirection

Hello i am trying to write a script that will redirect the output to a certain file. Here is the code so far: #!/bin/bash ps -e | sort | more > psfile When I execute the script nothing happens since i assume the output was redirected to the file called psfile. When I try to look at the... (1 Reply)
Discussion started by: mfruiz34
1 Replies

5. Shell Programming and Scripting

Output redirection

We have an application here that does some table queries and then prints the result on screen. I do not have the code of this application (which i will just call "queryCommand"), but what it does is that you call it with some parameters and it prints some info about the query and then the... (5 Replies)
Discussion started by: jolateh
5 Replies

6. Shell Programming and Scripting

Redirection output

Hi there I have a script that runs but it outputs everything onto the screen instead of a file. I've tried using the > outputfile.txt however all it does is dump the output to the screen and creates an outputfile.txt but doesn't put anything in that file. Any help would be appreciated ... (6 Replies)
Discussion started by: kma07
6 Replies

7. Shell Programming and Scripting

redirection and output

I'm redirecting the output of a command to a logfile, however, if the user is on a terminal I would also like the output to be displayed on the screen. tar tvf some_tarfile >Logfile if the user is on a term then have the output to the Logfile and also be displayed on the screen at the same... (2 Replies)
Discussion started by: nck
2 Replies

8. Shell Programming and Scripting

redirection output in two files

I want to redirect output of my script to two logfiles. I tried this : ./run.sh -c APP | tee >(grep " " > AppLogs.log) >(grep "XYZ" > xyz.log) >dev/null but this does not solve my purpose because logfile xyz.log remains empty untill i stop my script, i want both files, AppLogs.log and... (4 Replies)
Discussion started by: cmdup
4 Replies

9. UNIX for Dummies Questions & Answers

Output file redirection

Suppose I have a file named a When I write cat a>a The following error message is displayed cat: a: input file is output file and my file a is truncated to zero size. Also the exit status of the last command is 1 Can someone tell me what actually happens when I do so? (1 Reply)
Discussion started by: aagajaba
1 Replies

10. Shell Programming and Scripting

how to get desired output after redirection

hi i am running script which contains the commmnds and i am redirecting the script output to a file. like ./script 1> result.txt 2>&1 the above redirection is not working for commands when run in background in a script. but the problem here result.txt containg output which is repeated.... (3 Replies)
Discussion started by: raji
3 Replies
Login or Register to Ask a Question