I am running bash scripts and executing bash commands on remote systems using Connectwise Automate (formerly LabTech). These systems are both Linux and MacOS. Connectwise Automate (CWA) has some peculiar behavior with regards to *nix systems, wherein if a command or expression exits with a status code of 0 all output, regardless of stream (stdout or stderr), is replaced with the word "OK". When a non-0 exit status is returned the output is properly displayed in the CWA's remote terminal and script output. I have not received definitive confirmation that the output replacement behavior is based on exit code, however I believe it to be highly likely.

For a simplistic example, echo "Test" returns OK, whereas ls ./no/such/directory yields the appropriate exception. This is a long-standing issue with CWA and working with the vendor yields no workarounds or positive results without piping the command output to a file on disk and reading that file with a function particular to CWA. That yields inconsistent results, however, as often the command output that gets written to file is simply OK.

What I've tried

I've found and attempted many possible solutions. I found this question, specifically geared towards less, and was reliant on less's -K argument. I attempted to adapt part of the solution with no success invoking bash -c and trap, though my implementation could have been incorrect. I also attempted something along the lines of echo "Test" && false, which returns OK, likely (in my mind) due to the first command returning with an exit code of 0 before evaluating false.

Question restated

Is it possible to "coax" (so to speak) arbitrary utilities, particularly when invoked via bash or sh, into returning with an exit status of my choosing, or otherwise any non-0 exit status, and still have a given utility's output hit stdout/stderr?

  • 2
    You could do (command_you_want && exit 1) which will return an exit code of 1 if the command is successful, and leave it unchanged if it fails. – Stephen Harris Feb 6 '19 at 0:56
  • 1
    Since echo "Test" && false certainly does exit with a non-zero return code, something else about how the commands are running is getting in the way of achieving what you want (and it may not be what you think). How does it run the commands? Can you see what is actually executed? Is bash -c 'echo "Test" && false different, or is that what you already tried? – Michael Homer Feb 6 '19 at 1:33
  • @MichaelHomer I have already tried bash -c 'echo "Test" && false' to no avail. Correct me if I'm wrong, but my understanding of how the above command would work is the bash -c 'echo "test" portion will return with an exit status of 0 before proceeding. The flow control operator && actually relies on the the command to the left of it to return successful before proceeding. Do I have that right? – Scott Carlow Feb 6 '19 at 18:42
  • @StephenHarris Unfortunately, echo still returns with an exit code of 0 before moving on to exit 1. With a simplistic test of (echo "Test" && echo $? && exit 1) I get the output Test 0 then it exits and sets $? to 1. – Scott Carlow Feb 6 '19 at 18:46
  • echo test && false (in every permutation posted so far) unequivocally exits 1. I don't know how this tool launches its programs, or how it decides what the result was, but something is not happening in the way you think it is. – Michael Homer Feb 6 '19 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.