0

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 [email protected] '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 [email protected] '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?

3
  • 1
    Does applyrrdupg.sh read from stdin? Commented Jan 29, 2024 at 12:41
  • Well spotted. It shouldn't, however when I changed $cmd >>"$ERRORS_FILE" to echo "" | $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. Commented Jan 29, 2024 at 12:47
  • I was writing an answer addressing this and other issues with the code, but it's been closed. (For example, while …; do IFS= read -r …, ssh -qn …, and importantly don't use scalar variables to hold code.) Commented Jan 29, 2024 at 12:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.