读取键在读取循环中失败

时间:2014-12-08 01:24:30

标签: shell stdin io-redirection

这是我按检测键的脚本:

#!/bin/bash
echo "Reading keys."
while [ "x$keypress" = "x" ]; do
  read -n 1 -t 1 -s keypress
  printf "."
done
printf "\n"
echo "Pressed key: "$keypress

似乎工作正常(例如,在一段时间后按“q”键):

$ ./InputKey.sh
Reading keys.
.............................................
Pressed key: q

但是如果我将放在一个循环中,它会读取我的File.txt并等待每行的一个键:

#!/bin/bash
MyList=File.txt
while IFS=, read -ra ListLine
do
    echo "Processing line: "$ListLine
    echo "Press [S] to count as Secret or [P] to count as Public."
  while [ "x$keypress" = "x" ]; do
        read -n 1 -t 1 -s keypress
    printf "."
    done
    printf "\n"
    echo "Pressed key: "$keypress
done < "$MyList"

程序根本不等待进行按键操作,这是奇怪的结果:

$ ./prueba07.sh
Processing line: d:\Temp
Press [S] to count as Secret or [P] to count as Public.
.
Pressed key: c
Processing line: :\Temp
Press [S] to count as Secret or [P] to count as Public.

Pressed key: c
Processing line: e:\Temp
Press [S] to count as Secret or [P] to count as Public.

Pressed key: c
Processing line:
Press [S] to count as Secret or [P] to count as Public.

Pressed key: c

这是File.txt内容:

d:\Temp
c:\Temp
e:\Temp 

我在某处读到问题来自<"$MyList"重定向器,它消耗了所有 stdin

所以,我找到了this thread并添加了&3重定向器:

#!/bin/bash
MyList=File.txt
while IFS=, read -ra ListLine <&3
do
    echo "Processing line: "$ListLine
    echo "Press [S] to count as Secret or [P] to count as Public."
  while [ "x$keypress" = "x" ]; do
        read -n 1 -t 1 -s keypress
    printf "."
    done
    printf "\n"
    echo "Pressed key: "$keypress
done 3< "$MyList"

现在程序等待一键按下。但其余部分会自动重复(例如,只按 p 一次):

$ ./prueba07.sh
Processing line: d:\Temp
Press [S] to count as Secret or [P] to count as Public.
..
Pressed key: p
Processing line: c:\Temp
Press [S] to count as Secret or [P] to count as Public.

Pressed key: p
Processing line: e:\Temp
Press [S] to count as Secret or [P] to count as Public.

Pressed key: p
Processing line:
Press [S] to count as Secret or [P] to count as Public.

Pressed key: p

怎样才能解决这个问题? 谢谢你。

EDIT-01:测试了读取键行的这一变化,没有结果:

read -n 1 -t 1 -s keypress </dev/tty

this other主题中提取。

EDIT-02:在Ubuntu Desktop v12和CygWin(Windows 7)上测试。

EDIT-03:以下答案是正确的。我为脚本选择的最后一个更改是(最后只有一行):

#!/bin/bash
MyList=File.txt
while IFS=, read -ra ListLine <&3
do
    echo "Processing line: "$ListLine
    echo "Press [S] to count as Secret or [P] to count as Public."
  while [ "x$keypress" = "x" ]; do
        read -n 1 -t 1 -s keypress
    printf "."
    done
    printf "\n"
    echo "Pressed key: "$keypress
    unset keypress
done 3< "$MyList"

2 个答案:

答案 0 :(得分:1)

它在第一个循环之后工作,因为keypress为空,但在后续调用中,keypress仍然设置为第一次循环期间设置的值。 您只需将keypress变量设置为空字符串即可。

答案 1 :(得分:1)

您可以从不同的文件描述符中读取,并重构内部循环

while IFS=, read -r -u 3 -a ListLine
do
  echo 'Processing line:' "$ListLine"
  echo 'Press [S] to count as Secret or [P] to count as Public.'
  until read -n 1 -t 1 -s keypress
  do
    printf .
  done
  echo
  echo 'Pressed key:' $keypress
done 3< File.txt

Does bash support doing a read nested within a read loop?