ctubbsii commented on code in PR #5750:
URL: https://github.com/apache/accumulo/pull/5750#discussion_r2229400576
##########
assemble/bin/accumulo-service:
##########
@@ -230,16 +231,21 @@ function list_processes() {
local pid_file
local pid
local port
- pid_file="${ACCUMULO_PID_DIR}/accumulo-${process}.pid"
- pid=$(<"$pid_file")
- port=$(netstat -tnlp 2>/dev/null | grep "$pid" | awk '{print $4}' | awk -F
':' '{print $NF}' | paste -sd "," -)
- # check that only a single port was seen
- if (($(echo "$port" | grep -c -E '^[0-9]+(,[0-9]+)*$') != 1)); then
- echo "ERROR unexpected ports $(hostname) process:$process pid:$pid
ports:$port" >&2
- else
+ local loop_err
+ pid_file="$ACCUMULO_PID_DIR/accumulo-$process.pid"
+ pid=$(<"$pid_file") # read the contents of the file into the $pid variable
+ port=$(ss -tnlp 2>/dev/null | grep -wF "pid=$pid" | awk '{print $4}' | awk
-F : '{print $NF}' | paste -sd,)
+
+ if [[ $port =~ ^[1-9][0-9]{1,4}(,[1-9][0-9]{1,4})*$ ]]; then
echo "$process $pid $port"
+ else
+ echo "ERROR unexpected port format $(hostname) process:$process pid:$pid
ports:$port" >&2
+ loop_err=1
fi
done
+ if [[ $loop_err == 1 ]]; then
+ exit 1
+ fi
Review Comment:
This works, but could be simplified to:
```suggestion
[[ -z $loop_err ]] # return non-zero if any errors occurred during the loop
```
Since it is the last command of the function, its return value becomes the
return value of the function. In the calling code, the return value of the
function becomes the return value of the case statement, which becomes the
return value of the main function, which becomes the exit code of the script.
Doing it this way, allows the exit code to be naturally handled, rather than
forcing the script to exit early. Also, by checking for any nonzero length
string, you're avoiding any possible shellcheck concerns about doing a string
comparison on a numeric value, and avoids any misunderstanding that the numeric
value of the variable has any inherent meaning itself.
It's also slightly more succinct.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]