BASH同时读取循环早期破 [英] BASH while read loop breaking early

查看:89
本文介绍了BASH同时读取循环早期破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在敲打我的头靠在墙与这一个了一段时间。

I've been banging my head against for wall for a while with this one.

我想SSH到一套机器和检查它们是否可用(接受连接和未使用)。我创建了一个小脚本,tssh,它不只是:

I want to SSH into a set of machines and check whether they are available (accepting connections and not being used). I have created a small script, tssh, which does just that:

#!/bin/bash

host=$1
timeout=${2:-1}

ssh -qo "ConnectTimeout $timeout" $host "[ \`who | cut -f1 | wc -l \` -eq 0 ] && exit 0 || exit 1"

该脚本正常工作。返回255,如果有一个连接问题,1如果机器忙0,如果一切都很好。如果有人知道更好的方法来做到这一点,请让我知道。

This script works correctly. Returning 255 if there was a connection problem, 1 if the machine is busy and 0 if everything is good. If anyone knows a better way to do this please let me know.

所以下次我尝试打电话tssh在我的组使用​​了一段时间读循环机,而这正是这一切都错了。循环尽快退出为tssh返回0,并且永远不会完成全套。

So next I try and call tssh on my set of machines using a while read loop, and this is where it all goes wrong. The loop exits as soon as tssh returns 0 and never completes the full set.

while read nu ; do tssh "MYBOXES$nu" ; done < <(ruby -e '(0..20).each { |i| puts i }')

起初我以为这是一个子shell问题,但显然不是。
任何帮助,对风格/内容所发表的意见,将是非常美联社preciated!
我知道我要踢自己,当我找出原因...

At first I thought this was a subshell problem but apparently not. Any help, along with comments on style/content, would be much appreciated! I know I'm going to kick myself when I find out why...

推荐答案

不知道是否会有所帮助,但写的更清洁的方式,这将是

Don't know if it would help, but a cleaner way of writing that would be

for nu in `ruby -e '(0..20).each { |i| puts i}'`; do
  tssh "MYBOXES$nu" 
done

这篇关于BASH同时读取循环早期破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆