Imagine a call like this:
./generator.sh | head -1
This script is embedded in a larger context and it might happen that parts of it fail due to wrong configuration, etc. In such a case we do not want to continue with the script, so we set the pipefail
option. But now we obviosuly face the problem that when head
closes the receiving end, generator will fail. How can we mitigate the problem?
Is there a way to tell head
to keep going, but to discard the input (this would be ideal, as we do not even want the early-exit semantics here).
I know that we can just disable/reenable pipefail
for that piece, but I wonder if there is a shorter option.
? Is there a way to tell head to keep going, but to discard the input (this would be ideal, as we do not even want the early-exit semantics here).
There is sed
: delete all except first line:
sed '1!d'
if you do want the early exit semantics,
Then use Bash. Will read one line, ignore the rest.
./generator.sh | \
if IFS= read -r line; then printf "%s\n" "$line"; fi