package cmd impor
The present invent
/* * Copyright (c
A new technique fo
The use of recombi
Practical, accurat
Q: What is best p
Category: Uncatego
The Billion-Doll
--- abstract: 'Rec

This is a great wa
Q: How do I conve
Liquid Crystal Dis
Differences betwee
Q: How do I insta
The effect of low
1. Introduction {#
A woman arrested o
The use of ultraso
Q: Pyspark - Usin
Q: How to read the output of exec? Is it possible to read the output of a command and only do something if there is no output. If there is no output, then I'd like it to do nothing, not print an error. This command fails when run with sh: /home/ubuntu/bin/trap 'echo '' "$?" > /dev/null 2>&1' EXIT if you get some output, you'll be happy. However, if you don't have any output, you'll fail. I'd like it to just not have any output in that case. What is the simplest way to implement this? I thought I'd be able to do something like /home/ubuntu/bin/trap 'if [ -z "$(echo '' "$?")" ]; then echo ' "nothing" > /dev/null 2>&1'; fi' EXIT but this fails. Why? (or even when it works, why would it matter that I do a variable lookup inside the trap command? Shouldn't this be global to that instance of the script?) A: bash (4.4) has an undocumented option -o for the interpreter, that will cause it to simply exit if there is any output. There is an example on the bash man page: $ /bin/sh -c 'echo hi > tmp; false | command > /dev/null' 2>/dev/null hi $ /bin/sh -o -c 'echo hi > tmp; false | command > /dev/null' 2>/dev/null exit status 1 $ shopt -oq failglob; /bin/sh -c 'echo hi > tmp; false | command > /dev/null' 2>/dev/null hi I can't seem to find a way to turn it off without a debugger. However, in practice, it appears to work like a charm, with or without -o. I strongly suggest you use this instead of the shell trap. A: One alternative is to use the command built-in. exec 3>&1 command echo $? > /dev/null exec 3>&- The third line is necessary in case you want to return success even if it produces no output. The second one prevents the "command exists but returns non-zero exit status" warning, and the third line erases the output. To be clear, your original doesn't work because the command is already executed at this point. A: While Bash will not do that, sh does: sh -c 'echo hi > tmp; false | command > /dev/null' 2>/dev/null However, this runs the command in a sub-shell which makes it not very nice if you just want to perform some actions if there is no output. There is another way, though: status=$(command > /dev/null 2>&1) If you would want to capture the output, you could do: status=$(echo >&2) Finally: this one may be interesting: status=$(set -o err; command | tail -n 1 > /dev/null) This way, if the command doesn't produce any output, it exits with a status of zero, but otherwise it will print something like No such file or directory to stderr and exit with a status of 1. A: Instead of if [ -z "$(echo '' "$?")" ]; then echo ' "nothing" > /dev/null 2>&1'; fi' Try this: if [ -n "$(echo '' "$?")" ]; then echo ' "nothing" > /dev/null 2>&1'; fi' [ "$?" ] is a list of one word containing the status of the last command to complete. The test is true if the result contains something other than an empty string. [ -z foo ] : (the empty string) is false if foo is not empty [ foo ] : is true if the value of foo is the empty string. So [ -n '' ] will evaluate to true if the value of '' is the empty string. [ -z "$(echo '' "$?")" ] is true if echo '' "$?" is non-empty [ '' "$?" ] is true if '' "$?" is the empty string. I hope that helps you out. Also if you want the exit status of the last command to be returned to the calling script/process you can use this: if [ -n "$(echo '' "$?")" ]; then echo ' "nothing" > /dev/null 2>&1'; echo $? > $pid_file; fi' (where $pid_file is a file like /tmp/run_status where you can see the value of $? at a glance.) I've also seen [ -e "$?" ]; where the intention is to check if the variable contains anything but an empty string. I find that harder to read, and easier to make a mistake on, than [ -n $string ]. If you really want to do exactly what you are asking for, and capture the status in a variable, try: status=$? if [ "$status" != 0 ]; then echo ' "nothing" > /dev/null 2>&1'; fi'