While循环在Bash中的第一行之后停止读取 [英] While loop stops reading after the first line in Bash

查看:57
本文介绍了While循环在Bash中的第一行之后停止读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下shell脚本.目的是循环遍历目标文件的每一行(其路径是脚本的输入参数),并针对每一行工作.现在,它似乎只适用于目标文件的第一行,并在处理完该行后停止.我的脚本有什么问题吗?

I have the following shell script. The purpose is to loop thru each line of the target file (whose path is the input parameter to the script) and do work against each line. Now, it seems only work with the very first line in the target file and stops after that line got processed. Is there anything wrong with my script?

#!/bin/bash
# SCRIPT: do.sh
# PURPOSE: loop thru the targets 

FILENAME=$1
count=0

echo "proceed with $FILENAME"

while read LINE; do
   let count++
   echo "$count $LINE"
   sh ./do_work.sh $LINE
done < $FILENAME

echo "\ntotal $count targets"

do_work.sh 中,我运行了几个 ssh 命令.

In do_work.sh, I run a couple of ssh commands.

推荐答案

问题是 do_work.sh 运行 ssh 命令,默认情况下运行 ssh 从您的输入文件stdin中读取.结果,您仅看到第一行已处理,因为 ssh 占用了文件的其余部分,而while循环终止.

The problem is that do_work.sh runs ssh commands and by default ssh reads from stdin which is your input file. As a result, you only see the first line processed, because ssh consumes the rest of the file and your while loop terminates.

为防止这种情况,请将 -n 选项传递给您的 ssh 命令,以使其从/dev/null 而不是stdin读取.

To prevent this, pass the -n option to your ssh command to make it read from /dev/null instead of stdin.

这篇关于While循环在Bash中的第一行之后停止读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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