传递参数时脚本不会停止

时间:2014-12-12 19:33:37

标签: bash

我有以下功能完美无缺。

#!/bin/bash

killall java
#program USB
make iris install.1 mib510,/dev/ttyUSB0
#listen serial port and write to file
java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSB1:iris > foo.txt &
sleep 2
#if "Erase done" is printed to file, stop
if tail -f foo.txt | grep -n "Erase done" -q; then echo "Write ok";fi
killall java

但是当我更改我的脚本以接收下面的参数(sh test.sh USB0 USB1 foo.txt)时,它并没有结束。虽然它写入文件,但过程不会结束

#!/bin/bash

killall java
#program USB
make iris install.1 mib510,/dev/tty$1
#listen serial port and write to file
java net.tinyos.tools.PrintfClient -comm serial@/dev/tty$2:iris > $3 &
sleep 2
#if "Erase done" is printed to file, stop
if tail -f $3 | grep -n "Erase done" -q; then echo "Write ok";fi
killall java

我做错了吗?

1 个答案:

答案 0 :(得分:1)

当grep退出时,看起来tail -f将退出。所以问题可能出在:

if tail -f $3 | grep -n "Erase done" -q; then echo "Write ok";fi

您可以使用以下内容替换它:

tail -f $3 | while read LOGLINE
do
   [[ "${LOGLINE}" == *"Erase done"* ]] && echo "Write ok" && pkill -P $$ tail
done