while循环读取文件结束prematurely [英] while loop to read file ends prematurely
问题描述
最终的目标是让我的bash脚本在多个服务器上执行命令。我几乎有它成立。我的SSH认证工作,但这个简单的while循环是我的命。当我执行while循环,读我的文件主机名称,它工作正常,当我运行一个
The eventual goal is to have my bash script execute a command on multiple servers. I almost have it set up. My SSH authentication is working, but this simple while loop is killing me. When I execute the while loop, reading my file for host names, it works fine when I run a
SSH $ HOST的uname -a
ssh $HOST "uname -a"
但是当我试图运行另一个ssh命令,
but when I attempt to run another ssh command,
SSH $ HOSToslevel -s
ssh $HOST "oslevel -s"
while循环提前结束!我不明白。 为什么会在阅读时做循环运行的第一个命令完全正常的,但不能当第二个被添加?
the while loop ends early! I can't figure it out. Why would the while read do loop run perfectly fine with the first command, but not when the second is added?
我有一个名为hosts.list一个简单的文本文件,它有4个主机名,每行一个。
I have a simple text file called hosts.list that has 4 hostnames, one per line.
$ cat hosts.list
pcced1bip04
pcced1bit04
pcced1bo02
pcced1bo04
$ cat getinfo.bash
#!/bin/bash
set -x
while read HOST
do
echo $HOST
ssh $HOST "uname -a"
#ssh $HOST "oslevel -s"
echo ""
done < hosts.list`
当它运行时,它工作正常。它通过文件,一行一行地获得的的uname -a
的结果。所以,一切都很好,对不对? (很抱歉,但我打开设置-x
)。
When it runs, it works fine. It goes through the file, line by line and gets the results of "uname -a"
. So everything is fine, right? (Sorry, but I turned on set -x
).
$ ./getinfo.bash
+ read HOST
+ echo pcced1bip04
pcced1bip04
+ ssh pcced1bip04 'uname -a'
AIX pcced1bip04 1 6 0001431BD400
+ echo ''
+ read HOST
+ echo pcced1bit04
pcced1bit04
+ ssh pcced1bit04 'uname -a'
AIX pcced1bit04 1 6 0001431BD400
+ echo ''
+ read HOST
+ echo pcced1bo02
pcced1bo02
+ ssh pcced1bo02 'uname -a'
AIX pcced1bo02 1 6 0009FE2AD400
+ echo ''
+ read HOST
+ echo pcced1bo04
pcced1bo04
+ ssh pcced1bo04 'uname -a'
AIX pcced1bo04 1 6 0009FE2AD400
+ echo ''
+ read HOST
$
当我使行 [SSH $ HOSToslevel -s]时出现问题。当我这样做,剧本只读取文件的第一行,然后停止。 为什么不是去到其他行?
The problem occurs when I enable the line [ssh $HOST "oslevel -s"]. When I do, the script only reads the first line of the file, and then stops. Why won't it go onto the other lines?
$ ./getinfo.bash
+ read HOST
+ echo pcced1bip04
pcced1bip04
+ ssh pcced1bip04 'uname -a'
AIX pcced1bip04 1 6 0001431BD400
+ ssh pcced1bip04 'oslevel -s'
6100-06-02-1044
+ echo ''
+ read HOST
$
如果我有我的剧本有问题,为什么会被工作只用在while循环[SSH $ HOST的uname -a]
完美的罚款?
If I had a problem with my script, why would it be working perfectly fine with just the [ssh $HOST "uname -a"]
in the while loop?
推荐答案
如果您运行从标准输入读取命令(如 SSH
)在循环中,你需要确保之一:
If you run commands which read from stdin (such as ssh
) inside a loop, you need to ensure that either:
- 您的循环没有遍历标准输入
- 您的指令有它的标准输入重定向:
...否则,该命令可以消耗输入打算用于循环,使其结束。
...otherwise, the command can consume input intended for the loop, causing it to end.
前者:
while read -u 5 -r hostname; do
ssh "$hostname" ...
done 5<file
...其中,使用bash 4.1或更高版本,可以使用自动文件描述符分配改写成这样:
...which, using bash 4.1 or newer, can be rewritten with automatic file descriptor assignment as so:
while read -u "$file_fd" -r hostname; do
ssh "$hostname" ...
done {file_fd}<file
后者:
while read -r hostname; do
ssh "$hostname" ... </dev/null
done <file
...还可以,单独SSH,也可以用 -n
近似参数(也是从的/ dev /标准输入重定向空
)
while read -r hostname; do
ssh -n "$hostname"
done <file
这篇关于while循环读取文件结束prematurely的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!