I have a following script.
#!/bin/bash
set -e
# set -x
##### Utility functions #######
is_pkg_installed() {
local pkg_name="$1"
if [[ -n "$VIRTUAL_ENV" ]]; then
if pip show "$pkg_name" &>/dev/null; then
return 0
elif [[ -n "$POETRY_ACTIVE" ]]; then
if python -c "import $pkg_name" &>/dev/null; then
return 0
else
return 1
fi
else
return 1
fi
else
return 1
fi
}
is_python_project() {
local dir=$1
local filename
filename=$(basename "$dir")
if [[ -f "$dir"/requirements.txt || -f "$dir"/pyproject.toml || "$filename" == *.py ]]; then
return 0
else
for file in "$dir"/*; do
if [[ -d "$file" ]]; then
is_python_project "$file"
fi
done
fi
return 1
}
As you can see, this script only has functions, which aren’t called. When I try to source this file set -e
, it exits the shell immediately.
I am not sure if my understanding of e
option is correct, but I assumed, it exits the shell when command returns a non exit status and do get the exit status, a command should be executed first. But in my case, since commands are inside functions, they are not executed until I call the function, so when I source
the script, it should source it without exiting.
If this is the intended behaviour and my understanding of e
option is wrong, what is the work around to this i.e. exit the shell when an error is encountered and only when commands are executed. Preferably, I would just want to stop the script and not exit the shell, if that’s possible.