我正在尝试创建自定义过滤器,我可以管道拖尾文件。我遇到的问题是接受用户输入逐行进行。
我理解我的第一个“读取行”是从文件尾部获取输入...但是我正在努力用'read / dev / tty0'从用户那里抓取'y或n'继续。
问题是我的_response变量似乎没有设置。这是我的几个循环:
#!/bin/bash
ECHO_CMD=/bin/echo
READ_CMD=/usr/bin/read
#_input_stream=$0
_input_file=/file.log
tail -f ${_input_file} | {
while IFS= read -r _line
do
lastline="$_line";
echo ${_line} ;
${READ_CMD} -r -p "Are you sure? [y/N] " _response </dev/tty
${ECHO_CMD} "_response=${_response}"
_rtn=`echo ${_response} | grep -e y`
#if [[ $_response =~ ^([yY][eE][sS]|[yY])$ ]]
echo "_rtn= $_rtn "
if [[ ${_rtn} = 0 ]]
then
echo "Continuing"
continue
else
echo "Ending"
break
fi
done
}
exit 1
答案 0 :(得分:0)
我不清楚哪个答案应该是继续&#39;一。假设它是y
,那么试试:
#!/bin/bash
_input_file=/file.log
tail -f ${_input_file} | {
while IFS= read -r _line
do
lastline="$_line";
echo ${_line} ;
read -r -p "Are you sure? [y/N] " _response </dev/tty
case "$_response" in
[yY]*)
echo "Continuing"
;;
*)
echo "Ending"
break
;;
esac
done
}
exit 1
read
和echo
是bash内置的。除非另有明确原因,否则应优先使用外部二进制文件。
虽然可以使用if-then-else
语句,但case
语句提供了一种根据globs测试变量并执行适当命令的简便方法。
与[[...]]
不同,case
语句为POSIX,因此是可移植的。