【发布时间】:2019-02-06 23:34:10
【问题描述】:
我正在尝试遍历来自 grep 的每一行输出,并根据交互式提示有条件地做一些事情。
我看过这些: Iterating over each line of ls -l output
bash: nested interactive read within a loop that's also using read
Does bash support doing a read nested within a read loop?
但我仍然无法实现我想要的。我认为关键区别在于大多数示例都是从文件中读取输入-我想使用命令。我尝试了以下多种变体:
#! /bin/bash
function test1()
{
local out=$(grep -n -a --color=always "target" file.txt)
while read -u 3 line; do
echo -e "$line"
read -r -p "ok? " response
echo "you said: $response"
if [[ "$response" == 'n' ]]; then
return
fi
done 3<&0 <<< $out
}
function test2()
{
grep -n -a --color=always "target" file.txt | while read line; do
echo -e "$line"
read -r -p "ok? " response
echo "you said: $response"
if [[ "$response" == 'n' ]]; then
return
fi
done
}
在test1() 中,FD 重定向似乎没有达到我想要的效果。 test2() 似乎只是(不出所料)踩过我的第二个 read。我认为这是因为我需要切换正在使用的 FD。我尝试将来自test1 的FD 重定向组合到test2,但我无法让它与管道一起使用。我正在使用 bash 4.4 版。我错过了什么?
这是上面两个函数的运行示例:
[~]$ cat file.txt
ksdjfklj target alsdfjaksjf alskdfj asdf asddd
alsdfjlkasjfklasj
asdf
asdfasdfs
target
assjlsadlkfjakls target
target aldkfjalsjdf
[~]$
[~]$ test1
you said: 1:ksdjfklj target alsdfjaksjf alskdfj asdf asddd 6:target 7:assjlsadlkfjakls target 8:target aldkfjalsjdf
you said:
you said:
you said:
you said:
^C
[~]$
[~]$
[~]$ test2
1:ksdjfklj target alsdfjaksjf alskdfj asdf asddd
you said: 6:target
7:assjlsadlkfjakls target
you said: 8:target aldkfjalsjdf
[~]$
【问题讨论】:
-
如果您向我们展示一些输入和输出,那就太好了。
标签: bash