通过在bash文件行循环,不使用标准输入 [英] Looping through lines in a file in bash, without using stdin

查看:142
本文介绍了通过在bash文件行循环,不使用标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对下面的情况大感迷惑。

I am foxed by the following situation.

我有,我想通过线线运行,在一个循环,在bash文件LIST.TXT。在LIST.TXT一个典型的行有空间,但问题是该循环包含一个读命令。我想写这个循环在bash,而不是像perl的。我不能做到这一点: - (

I have a file list.txt that I want to run through line by line, in a loop, in bash. A typical line in list.txt has spaces in. The problem is that the loop contains a "read" command. I want to write this loop in bash rather than something like perl. I can't do it :-(

下面就是我通常会写一个循环从文件里逐行读取:

Here's how I would usually write a loop to read from a file line by line:

while read p; do
  echo $p
  echo "Hit enter for the next one."
  read x
done < list.txt

这不工作,虽然自然是因为读X将从LIST.TXT阅读,而不是键盘。

This doesn't work though, because of course "read x" will be reading from list.txt rather than the keyboard.

和这不工作之一:

for i in `cat list.txt`; do
    echo $i
    echo "Hit enter for the next one."
  read x
done

由于LIST.TXT线路有空格

because the lines in list.txt have spaces in.

我有两个提出的解决方案,这两个臭:

I have two proposed solutions, both of which stink:

1),我可以LIST.TXT编辑,并在全球以THERE_SHOULD_BE_A_SPACE_HERE全部替换空间。然后我可以使用类似中美战略经济对话,我的循环中,用空格来代替THERE_SHOULD_BE_A_SPACE_HERE,我会准备就绪。如果任何LIST.TXT的线路包含短语THERE_SHOULD_BE_A_SPACE_HERE(这样可以恶意用户乱了我),我不喜欢这样的愚蠢的原因,它会失败。

1) I could edit list.txt, and globally replace all spaces with "THERE_SHOULD_BE_A_SPACE_HERE" . I could then use something like sed, within my loop, to replace THERE_SHOULD_BE_A_SPACE_HERE with a space and I'd be all set. I don't like this for the stupid reason that it will fail if any of the lines in list.txt contain the phrase THERE_SHOULD_BE_A_SPACE_HERE (so malicious users can mess me up).

2)我可以使用whil​​e循环:标准输入,然后在每个循环我其实例如推出新的终端,这将是不受影响的事情上,在原来的壳,涉及标准输入。我想这和我没有得到它的工作,但它是丑:我想换这一切了一个shell脚本,我不希望这样的shell脚本是随机打开新窗口。什么是好的,什么,都可能是这个问题的答案,将是如果我能想出​​如何以某种方式在调用命令和饲料的命令给它一个新的外壳,而不饲养标准输入到它,但我不能让它的工作。例如,这不工作,我真的不知道为什么:

2) I could use the while loop with stdin and then in each loop I could actually launch e.g. a new terminal, which would be unaffected by the goings-on involving stdin in the original shell. I tried this and I did get it to work, but it was ugly: I want to wrap all this up in a shell script and I don't want that shell script to be randomly opening new windows. What would be nice, and what might somehow be the answer to this question, would be if I could figure out how to somehow invoke a new shell in the command and feed commands to it without feeding stdin to it, but I can't get it to work. For example this doesn't work and I don't really know why:

while read p; do 
  bash -c "echo $p; echo ""Press enter for the next one.""; read x;"; 
done < list.txt

这似乎尝试,失败,因为读X,尽管在不同的外壳是弄好了,还是看似从LIST.TXT阅读。但我觉得我可能会关闭这一个 - 谁知道

This attempt seems to fail because "read x", despite being in a different shell somehow, is still seemingly reading from list.txt. But I feel like I might be close with this one -- who knows.

帮助!

推荐答案

您必须为不同的文件描述符打开

You must open as a different file descriptor

while read p <&3; do
    echo "$p"
    echo 'Hit enter for the next one'
    read x
done 3< list.txt

更新:只要忽略下面的评论中长时间的讨论。它无关的问题或这个答案。

Update: Just ignore the lengthy discussion in the comments below. It has nothing to do with the question or this answer.

这篇关于通过在bash文件行循环,不使用标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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