I'm in a coding mood!
I've come through many ways of conditionally redirect the output of a script like:
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:
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:
The problem is that mycommand outputs binary data including the null character.
2. I' rather not use a temporary file:
Just because I don't like it
Last edited by chebarbudo; 05-11-2016 at 10:25 AM..
Reason: syntax error
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.
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.
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.
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: 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..
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.
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: 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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)