Issue
I have a shell script stored in Azure storage. My application running on Azure calls this script from the Azure storage. It wasn't running as expected. After a lot of troubleshooting, I figured out it runs when I add an extra echo statement at the end.
My Original Script would look like this
#!/bin/bash
echo "Configuring my App"
jetpack download my_binary_file
chmod u+x my_binary_file
MY_FILE=$(jetpack config somedir.somepath) ./my_binary_file -clusterInit
My updated script with an extra echo looks like this
#!/bin/bash
echo "Configuring my App"
jetpack download my_binary_file
chmod u+x my_binary_file
MY_FILE=$(jetpack config somedir.somepath) ./my_binary_file -clusterInit
echo ""
With the first script, I get:
Failed to execute cluster-init script my_script.sh. return code: 1
Detail: Script output: Configuring my App 2021/05/18 13:25:43 exit status 2 exit status 2
With the second script (with eco "" at the end of the file) I get the succesful script execution:
Configuring my App
Why is the script runs with echo "" at the end but not without that?
Solution
Your script and all your commands run exactly the same in both cases, it's just that you've changed your success criteria from "the cluster was initialized" to "a blank line was written".
"Failed to execute cluster-init script my_script.sh. return code: 1" does not mean "the script did not run". Instead it means "my_script.sh was executed and ran to completion, but it reported that it failed to do its job".
You can use exit 0
to exit with self-reported success, and exit 1
(or up) to exit with self-reported failure. Without an explicit exit
, the exit code is that of the last executed command.
When you add another command like echo ""
, that becomes the last one, and therefore determines the exit code. It doesn't help or solve any problems, it just hides the failure so that the workflow thinks that the script succeeded when it didn't.
Answered By - that other guy