I have the following in a python script (using python 3.4), if there is any failure:
exit(1)
The purpose of this python script is to print()
some value, so that it may be assigned to a shell variable like so:
#!/bin/bash
set -e
result=$(python my-script.py)
echo $?
However, if the script fails, the echo
above returns 0. I want set -e
to immediately interrupt and fail the script if the python script fails (i.e. returns non zero exit code).
How can I make this work? I tried set -eo pipefail
as well in my bash script and I've noticed no difference.
I'm running Ubuntu 15.
EDIT
My python script (verbatim) just in case it is the suspect here...
import re
import sys
version_regex = r'(?:(?:release|hotfix)\/|^)([\d.]+)-\d+-g[a-f\d]+$'
result = re.search(version_regex, sys.argv[1])
if not result:
exit(1)
print(result.group(1))
Note that I have tried both sys.exit(1)
and exit(1)
in my experiments but I never saw my bash script fail.
EDIT 2
parsed_version=$(python parse-git-describe.py $describe_result; echo $?)
echo $parsed_version
echo $?
The above bash script gives me the following output:
1
0
Note that the script parse-git-describe.py
is the same as the python script provided earlier.
EDIT 3
Apparently local
causes this to break. EDIT 2 above was wrong, that is the result with local
inside a shell function:
foo()
{
local parsed_version=$(python parse-git-describe.py $describe_result; echo $?)
echo $parsed_version
echo $?
}
Result is:
1
0
But if I remove local
it works fine?
foo()
{
parsed_version=$(python parse-git-describe.py $describe_result; echo $?)
echo $parsed_version
echo $?
}
Result is:
1
1
Why does local
matter?