whileループはファイルの最初の行でのみ実行されます。

whileループはファイルの最初の行でのみ実行されます。

何らかの理由で、私のwhileループは一度だけ実行されます。他の投稿も見ましたがよくわかりませんでした。単純なスクリプトでなければなりませんが、一度だけ実行されます。画面出力は+xbashスクリプトにあります。

LICENSE="1010110.license"
PASS="somepassword"
SSHPASS="/usr/bin/sshpass"
SERVERFILE="/home/user/server.txt"

while read SERVER
do
    if sshpass -p $PASS ssh root@$SERVER '[ -d /etc/something/licenses/ ]'
         then
            echo "The directory is present"
            sshpass -p "$PASS" scp $LICENSE "root@$SERVER":/etc/something/licenses/
         else
            echo "$SERVER does not contain the correct folder, trying 2nd option" >> failures.txt
            if sshpass -p $PASS ssh root@$SERVER '[ -d /etc/something/else/licenses/ ]' 
                then
                    sshpass -p "$PASS" scp $LICENSE "root@$SERVER":/etc/something/else/licenses/
                else
                    echo "$SERVER does not contain either directory" >> failures.txt
            fi

    fi

done < "$SERVERFILE"

これが出力です+x

+ read SERVER
+ sshpass -p 'somepassword' ssh root@server1 '[ -d /etc/something/licenses/ ]'
+ echo 'server1 does not contain the correct folder, trying 2nd option'
+ sshpass -p 'somepassword' ssh root@server1 '[ -d /etc/something/else/licenses/ ]'
+ sshpass -p 'somepassword' scp 1010110.license root@server1:/etc/something/else/licenses/
+ read SERVER

もう一度読もうとするようですが、SERVER実際には読みません。このserver.txtファイルはサーバー名のリストです。

ベストアンサー1

おすすめ記事