Is it possible to “coax” arbitrary core *nix utilities into returning a non-zero exit status even when...












0















This is my first time posting to StackExchange. If there is an issue with my question please let me know so that I can correct it. :) I have done quite a bit of searching on the topic, as well as attempted some things on my own, to no avail.



Background



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? Thanks in advance for your time!










share|improve this question




















  • 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 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 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 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 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 at 19:07
















0















This is my first time posting to StackExchange. If there is an issue with my question please let me know so that I can correct it. :) I have done quite a bit of searching on the topic, as well as attempted some things on my own, to no avail.



Background



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? Thanks in advance for your time!










share|improve this question




















  • 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 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 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 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 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 at 19:07














0












0








0








This is my first time posting to StackExchange. If there is an issue with my question please let me know so that I can correct it. :) I have done quite a bit of searching on the topic, as well as attempted some things on my own, to no avail.



Background



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? Thanks in advance for your time!










share|improve this question
















This is my first time posting to StackExchange. If there is an issue with my question please let me know so that I can correct it. :) I have done quite a bit of searching on the topic, as well as attempted some things on my own, to no avail.



Background



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? Thanks in advance for your time!







bash shell exit-code






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 6 at 0:53







Scott Carlow

















asked Feb 6 at 0:47









Scott CarlowScott Carlow

11




11








  • 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 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 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 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 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 at 19:07














  • 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 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 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 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 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 at 19:07








2




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 at 0:56





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 at 0:56




1




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 at 1:33





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 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 at 18:42





@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 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 at 18:46





@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 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 at 19:07





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 at 19:07










0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498935%2fis-it-possible-to-coax-arbitrary-core-nix-utilities-into-returning-a-non-zero%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498935%2fis-it-possible-to-coax-arbitrary-core-nix-utilities-into-returning-a-non-zero%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

第一次世界大戦

Touch on Surface Book