如何停止并强制用户在bash脚本中输入特定值? [英] How to stop and force user to enter specific values in bash script?
问题描述
我正试图迫使用户在bash脚本中输入特定的值,但是它没有提示,有人可以建议吗?
I'm trying to force user to enter specific values in bash scripting, but it does not prompt, can someone advice?
while IFS= read -r line;
do
until [ "$CONTINUE_EXECUTION" = "Y" -o "$CONTINUE_EXECUTION" = "N" ]; do
echo "Please press Y to continue or N to exit"
read CONTINUE_EXECUTION
done
echo $line
done < xx2.txt
推荐答案
脚本中的问题是 read -r行
和 read CONTINUE_EXECUTION
从xx2.txt读取文件.现在, stdin
映射到 xx2.txt
,并且两个 read
都从那里获取输入.
The problem in your script is both read -r line
and read CONTINUE_EXECUTION
reads from xx2.txt file. The stdin
is now mapped to xx2.txt
and both read
takes input from there.
一种方法是使用 exec
创建一个新的fd,然后使用 read -u
从该fd中读取.
One way is to use exec
to create a new fd and use read -u
to read from that fd.
exec 5<xx2.txt
while read -u 5 line # read from xx2.txt
do
until [ "$CONTINUE_EXECUTION" = "Y" -o "$CONTINUE_EXECUTION" = "N" ]; do
read -p "Please press Y to continue or N to exit: " CONTINUE_EXECUTION
# read from stdin, which is the user's terminal at this point
done
echo $line
CONTINUE_EXECUTION=""
done
PS:因为我想解释所需更改的主要部分,所以我删除了部分代码,例如 IFS
.
PS: I have removed few parts of your code like IFS
since i wanted to explain the main part of the required change.
这篇关于如何停止并强制用户在bash脚本中输入特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!