在linux中使用bash脚本通过轮询按键启动和停止tail

在linux中使用bash脚本通过轮询按键启动和停止tail,linux,bash,keypress,interrupt,tail,Linux,Bash,Keypress,Interrupt,Tail,这就是我要做的,我有一个arduino,带有温度信息,通过串口传输到linux设备上。我正在成功地读取序列号并用cat将其记录到一个文件中,然后在该文件后面添加tail-f。效果很好。我想在一些控制广告,能够跟踪文件,停止跟踪,但继续录制,或关闭程序一起。这就是我在这里成功做到的,但是我从linux中得到了最奇怪的错误。我讨厌手动杀死所有的猫,我想要一个简单的界面。我尝试了许多不同的方法来实现这些结果,我对新的方法持开放态度 最初,当我开始我的程序时,出现以下错误 ./arduinodue.sh

这就是我要做的,我有一个arduino,带有温度信息,通过串口传输到linux设备上。我正在成功地读取序列号并用cat将其记录到一个文件中,然后在该文件后面添加tail-f。效果很好。我想在一些控制广告,能够跟踪文件,停止跟踪,但继续录制,或关闭程序一起。这就是我在这里成功做到的,但是我从linux中得到了最奇怪的错误。我讨厌手动杀死所有的猫,我想要一个简单的界面。我尝试了许多不同的方法来实现这些结果,我对新的方法持开放态度

最初,当我开始我的程序时,出现以下错误

./arduinodue.sh: 20: ./arduinodue.sh: [[: not found
./arduinodue.sh: 25: ./arduinodue.sh: [[: not found
如果我按下任何键,屏幕就会开始向下滚动

Press S to start and stop tail

Press X to close and stop recording
最后,在点击s或x足够多次后,我得到了这个

Segmentation fault (core dumped)
我的代码如下:

#!/bin/sh

#below opens serial connection

stty -F /dev/ttyACM0 cs8 19200 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

cat /dev/ttyACM0 > $HOME/Due.log &     #starts cat recording serial data to Due.log as a background task
PID1=$!     #records PID of cat

nottailing2() {     #prints a message with instructions
echo "\n"
echo "Press S to start and stop tail\n"
echo "Press X to close and stop recording\n"
nottailing
}

nottailing() {     #starts a while loop waiting for a keypress I attempted running without while loop and instead used function calls to redirect the run sequence with simular results
if [ -t 0 ]; then stty -echo -icanon time 0 min 0; fi     #makes a keypress interrupt I believe but I've also attempted to run without that
while [ "x$keypress" = "x" ]; do
keypress=''
read keypress
if [[ "$keypress" == [X,x]* ]]; then     #press x to kill the script cleanly
if [ -t 0 ]; then stty sane; fi
kill $PID1
exit 1
fi
if [[ "$keypress" == [S,s]* ]]; then     #press s to start tail
if [ -t 0 ]; then stty sane; fi     #close interrupt?
break
fi
done
if [ -t 0 ]; then stty sane; fi
tailing
}

tailing() {     #same as function nottailing except it stops the tailing instead of starting it
if [ -t 0 ]; then stty -echo -icanon time 0 min 0; fi
while [ "x$keypress" = "x" ]; do
keypress=''
read keypress
if [[ "$keypress" == [X,x]* ]]; then
if [ -t 0 ]; then stty sane; fi
kill $PID1
exit 1
fi
if [[ "$keypress" == [S,s]* ]]; then
if [ -t 0 ]; then stty sane; fi
break
fi
done
if [ -t 0 ]; then stty sane; fi
nottailing2
}

nottailing2     #start the looping process
这:

这是一个很好的迹象,表明您正在使用为Bash或其他shell编写的代码,但在POSIX简约shell中运行它。您的脚本在顶部显示/bin/sh,因此您不能使用[[真的。如果您的系统有Bash,请更改顶部的shebang行。如果没有,您需要找到另一种方法在没有fancier[[语法]的情况下执行这些检查。可能是这样的:

if [ "$keypress" = S -o "$keypress" = s ]

这很好,我在bash中运行了更新并链接到它,问题解决了。谢谢。
if [ "$keypress" = S -o "$keypress" = s ]