I have a script which performs various operations on a list of hosts. However one of the loops only applies the operation to the first entry in the file, and I can't see why.
# this loop shows multiple entries in the file...
while read -r target_host ; do
echo "%% read $target_host"
done <"${PHASE_TWO_FILE}"
# this loop only goes around once then exits....
while read -r target_host ; do
cmd="ssh user@cmk.southwold.net 'applyrrdupg.sh ${target_host}'"
echo "DEBUG: Running $cmd"
$cmd >>"$ERRORS_FILE"
RC=$?
if [ 0 -eq "$RC" ]; then
echo "SUCCESS: convert ${target_host}"
echo "$target_host" >>"${PHASE_THREE_FILE}"
echo -e "HOST: $target_host\nSUCCESS\n--" >>"$ERRORS_FILE"
else
echo "ERROR: rrd conversion failed for $target_host"
REVIEW_ERRORS=1
echo -e "HOST: $target_host\nERROR: Conversion failed\nDETAILS: see above\n--" >>"$ERRORS_FILE"
fi
done <"${PHASE_TWO_FILE}"
echo "%% loop exited"
# and just to make sure - lets check again....
while read -r target_host ; do
echo "%% read $target_host"
done <"${PHASE_TWO_FILE}"
This produces the output:
%% read centos5.southwold.net
%% read centos7.southwold.net
%% read cmk-satellite.southwold.net
DEBUG: Running ssh user@cmk.southwold.net 'applyrrdupg.sh centos5.southwold.net'
SUCCESS: convert centos5.southwold.net
%% loop exited
%% read centos5.southwold.net
%% read centos7.southwold.net
%% read cmk-satellite.southwold.net
What am I missing?
applyrrdupg.sh
read from stdin? – Chris Davies Jan 29 '24 at 12:41$cmd >>"$ERRORS_FILE"
toecho "" | $cmd >>"$ERRORS_FILE"
the loop iterates as epxected and my sanity is restored. If you copy and paste your comment as an answer, I'll tick the box. – symcbean Jan 29 '24 at 12:47while …; do IFS= read -r …
,ssh -qn …
, and importantly don't use scalar variables to hold code.) – Chris Davies Jan 29 '24 at 12:49