How to compare output of Run Shell Script action to a value

My Shortcut uses the Run Shell Script action which invokes a shell script which outputs either a positive integer for success or 0 for failure. How do I test the output for 0? It seems the only choices for comparison are whether there is or is not a value, but not to test the actual value.

MacBook Pro 16″

Posted on Oct 19, 2023 10:47 AM

Reply
10 replies

Oct 19, 2023 11:10 AM in response to Ken Nellis

The do shell script stuff signals if the shell script returns a non-zero value, which means try-end is useful.


AppleScript: Working with Errors

try
    do shell script "scriptname"
    it-worked
on error
    it-failed
end try



If the script works, then the command it-worked will be invoked. If the script fails, the command it-worked will be skipped, and the command it-failed will be invoked.


As for the value: “Signals an error if the shell script exits with a non-zero status. The error number will be the status, the error message will be the contents of stderr.“


This try example seems to do what you want for the shell script status, too:

https://www.macscripter.net/t/do-shell-script-get-standard-output-into-a-variable-when-error/71437

Oct 19, 2023 11:24 AM in response to Ken Nellis

Do you want to test for success or failure in the actual script and perform some other action on success, or are you passing the value out of the Run Shell Script action onto a subsequent action which then tests if it received the correct value.


If testing within the Run Shell Script, assign the result to a variable and test it.

success=$(some result producing code)
# test for non-zero integer result
(( ${success} )) || { echo "Failed branch"; exit 1 }
echo "Success branch"
exit 0


If you are passing the success code on to a subsequent action, make the following your last statement in the Run Shell Script:

printf '%d' ${success}





Oct 19, 2023 12:19 PM in response to Ken Nellis

Shortcuts has an If action that could follow your Run Shell Script action. It has two tests for the Shell Script Result:

  1. has any value (includes 0 or any greater integer)
  2. does not have any value (which means the script passes no value of any kind to it)
success=


So, if the Run Shell Script is successful and you have the If shell script result does not have any value, then it will take the first If clause branch and run that action.




Oct 19, 2023 1:12 PM in response to Ken Nellis

You would have to use another Run Shell Script action following the existing one where you would pass the result code from the previous action as an argument and do this test:


(( ${1} )) || { echo "zero value"; exit 0}
echo "non-zero value"

There just is no Shortcut action specifically designed to test for zero. That is why it is important to do that test while in the original Run Shell Script.



Oct 19, 2023 1:22 PM in response to VikingOSX

While that may work, after getting non-responses here, I also asked ChatGPT, which didn't quite give me the direct answer, but guided me to it. The correct answer is to use the Get Numbers From action, which converts the shell script output to a number, and then from there, an IF action can be used to compare its value. Works great, thank you very much. It would have been insane, in my book, for Shortcuts to require a second shell script to perform this (IMHO) simple task.

How to compare output of Run Shell Script action to a value

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.