Bash 传递参数时,脚本不会停止
我有以下几点,效果很好Bash 传递参数时,脚本不会停止,bash,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
#!/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
我做错什么了吗?当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
./script.sh arg1 arg2 arg3。我看不出你在用arg2USB1,这是我的第二个argument@SandyElms
$2
位于-comm
选项的参数中。请在“”中调用您的参数。谢谢您的回答,但没有成功。