The ls is totally superfluous. The shell looks up the file when expanding the pattern, so all we need to do is:
- check whether the pattern matched anything
- check/get rid of any additional matches if there is more than one (not taken care of in the question either, but it's another potential problem).
The first part is trivial using test or it's [ alias. Unfortunately the second is slightly non-trivial, because printf "%s" pattern alone does not work, so one has to resort to a helper function that simply outputs it's first argument.
The solution is to write a short helper shell function like this:
first() { printf "%s" "$1"; }
PID_FILE=$(first $PROFILES_PATH*/*/*/*.pid)
[ -f "$PID_FILE" ] || echo "Couldn't find PID."
In this case the PID_FILE will be equal to pattern if it's not found and the -f test will tell you whether it matched. Alternatively you can move the test into the helper function:
matches() { [ -f "$1" ] && printf "%s" "$1"; }
PID_FILE=$(matches $PROFILES_PATH*/*/*/*.pid)
[ $? != 0 ] && echo "Couldn't find PID."
This way you still get empty PID_FILE and non-zero exit status if the file is not found.