T
Task•3mo ago
grumpper

What environment anad shell are task commands executed in?

I am trying to evaluate the result of a command which pipes through tee so that output of the command is logged to the STDOUT and saved in a file at the same time. The idea is to then invoke a script that logs the outcome of the command depending on the exit code in a file.
task:
cmds:
- |
pylint --recursive=y . | tee reports/python/pylint.log
logger.sh $?
task:
cmds:
- |
pylint --recursive=y . | tee reports/python/pylint.log
logger.sh $?
BUT: If there are pylint findings the exit code is 16 so a non-zero one but the end result of the pipe is 0 (due to the tee command)... Now how can I catch that in taskfiles? If I try with $? it's 0 ... in order to correctly catch it I must use set: [pipefail]. But if I use it, the task errors out and never reaches the script that logs the result (not to mention that it interrupts the whole task execution)... Ok so I will ignore the error instead using ignore_error: true. Still problem though as ignore_error is not actually ignoring anything - it is instead overriding the exit code to be 0 instead of 16. More appropriate name should have been override_error 🙂 Ok so let's use the appropriate way of handling this by utilizing ${PIPESTATUS[0]} (${pipestatus[1]} in zsh):
task:
cmds:
- |
pylint --recursive=y . | tee reports/python/pylint.log
logger.sh ${PIPESTATUS[0]}
task:
cmds:
- |
pylint --recursive=y . | tee reports/python/pylint.log
logger.sh ${PIPESTATUS[0]}
This way the execution will not fail but I will still catch that the exit code of the pylint command is non zero. Awesome! But whatever environment or shell this is running in does not have pipe status being set at all... In fact if you simply run set it outputs nothing... Soo... how can I handle that error handling mess?
Solution:
found it! if you put everything you want in a script and invoke the script as follows: ```shell...
Jump to solution
1 Reply
Solution
grumpper
grumpper•3mo ago
found it! if you put everything you want in a script and invoke the script as follows:
#!/usr/bin/env bash

set -o pipefail

pylint --recursive=y . | tee reports/python/pylint.log
uci_logger $?
#!/usr/bin/env bash

set -o pipefail

pylint --recursive=y . | tee reports/python/pylint.log
uci_logger $?
and then setup the task simply like this:
test_py:
cmds:
- bash ./script.sh
test_py:
cmds:
- bash ./script.sh
everything works as expected: pipefail and $? work as intended without having to ignore any errors or break the execution.
Want results from more Discord servers?
Add your server