Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

for loop - shell iteration while skipping nodes

I have 20 nodes out of which 5 does not ssh(this is expected). These values are dynamically pulled from a source. Below is my code trying to check if they are able to SSH:

for skip_node in "${skip_nodes[@]}"
do 
    for node in "${nodes[@]}"
    do

        if [[  $node != $skip_node ]]; then continue; fi

        <I have my ssh logic working here>
        if [ $? -ne 0 ]; then
            UNABLE_TO_SSH+=" ${node}"
            (( ERROR_COUNT += 1 ))
        fi
    done
done

This script is failing because array nodes again contains those nodes which are in skip_nodes. Any help on how should can I solve this problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have succeeded in achieving this. I have substracted 2 lists and then iterated over the final list for SSH check.

nodes=($(comm -3 <(printf "%s
" "${nodes[@]}" | sort) <(printf "%s
" "${skip_nodes[@]}" | sort) | sort -n))
for node in "${nodes[@]}"
do
    <my code for checking ssh>
    if [ $? -ne 0 ]; then
        UNABLE_TO_SSH+=" ${node}"
        (( ERROR_COUNT += 1 ))
    fi
done

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...