Home  >  Q&A  >  body text

centos - shellscript 循环执行代码,但是每次执行一次以后就退出了?

while read $HOSTNAME
do
    ssh root@$HOSTNAME "poweroff"
done < host.log

host.log 中的内容:
192.168.122.1
192.168.122.2
192.168.122.3

机器配置过ssh互信

执行那个后发现每次只能关闭一台机器脚本就退出了。需要再次执行才能关闭后面的机器,请问是为什么?谢谢

过去多啦不再A梦过去多啦不再A梦2733 days ago855

reply all(4)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-04-25 09:03:06

    Try writing this line like this: ssh root@$HOSTNAME "poweroff" < /dev/null

    reply
    0
  • 漂亮男人

    漂亮男人2017-04-25 09:03:06

    or write ssh -n. The reason this happens is that ssh reads the rest of your stdin.

    So I try not to use while + redirection to read data. If I want to read, I also use another file descriptor to read. stdin can easily be read by other commands.

    reply
    0
  • 黄舟

    黄舟2017-04-25 09:03:06

    perl -ne 'system q/ssh/, q/root@/.$_,q/poweroff/' host.log #This should be fine

    reply
    0
  • 怪我咯

    怪我咯2017-04-25 09:03:06

    Remove the $ in while read $

    HOSTNAME

    while reading HOSTNAME
    do
    ssh root@$HOSTNAME "poweroff"
    done < host.log

    reply
    0
  • Cancelreply