在bash shell脚本中,循环遍历文件行并使用输出为每行的值调用ssh命令?

时间:2018-06-28 14:20:12

标签: bash shell unix

如果我使用以下循环运行脚本,则该循环的第一次迭代后它将停止处理。

但是,如果我删除ssh命令,它将遍历整个文件。
#ssh -q ${host} ./fgrepOne.sh "${searchTerm}" '\*.log\*' >> ${outputFile}

处理文件中的第一行后循环停止:

while IFS=: read -r host; do
    if [[ "$host" == *#* ]] ;
    then
        echo Skipping $host
        continue
    fi
    echo ssh "${host} ./fgrepOne.sh "'"'${searchTerm}'"'
    ssh -q ${host} ./fgrepOne.sh "${searchTerm}" '\*.log\*' >> ${outputFile}
done < $serverList
输出:
ssh servera ./fgrepOne.sh "ERROR"

循环在单独的一行上处理每个单词:

for host in $(cat ${serverList}); do
    if [[ "$host" == *#* ]] ;
    then
        echo Skipping $host
        continue
    fi
    echo ssh "${host} ./fgrepOne.sh "'"'${searchTerm}'"'
    ssh -q ${host} ./fgrepOne.sh "${searchTerm}" '\*.log\*' >> ${outputFile}
done
输出:
ssh servera ./fgrepOne.sh "ERROR"
ssh serverb ./fgrepOne.sh "ERROR"
ssh serverc ./fgrepOne.sh "ERROR"
Skipping #COMMENT
Skipping #server5
Skipping #server6
Skipping #Long
ssh Comment ./fgrepOne.sh "ERROR"  <<< Note that this was intended to be part of the previous command.
ssh  ./fgrepOne.sh "ERROR"

0 个答案:

没有答案