I am writing a bash script where one goal is to monitor serial port output from an embedded device for a target string (the terminal prompt, to detect when the device is booted fully). The data is coming over an RS232 serial to USB cable and is available in a /dev/ttyUSB#
file.
My first attempt was
grep --max-count=1 -F "root@ramfs:~#" /dev/ttyUSB0
This work only sometimes for me, I am assuming due to buffering or colored printing and fancy formatted output from the device, but I would like to see the terminal output to confirm if the device is booting properly and the grep is failing, or if grep is failing to match because the device does not fully boot.
Having looked at a few other answers, I have tried a few variations of
#!/bin/bash
stty -F /dev/ttyUSB0 115200
cat /dev/ttyUSB0 | tee | grep --max-count=1 -F "root@ramfs:~#"
This method does print what I am looking for initially, but as soon as the device boots far enough to have a UBOOT prompt, it starts getting input back from my PC, I assume based on this output:
Unknown command 'nnnown' - try 'help'
ZynqMP> yqnknown commweP>nnown comm 'ocand' - try 'help'
Unknown command 'yqnknown' - try 'help'
That the command I have created is essentially echoing back the serial output because tee is treating that as the stdout.
I am hoping someone can point out a simple way to fix my attempt, or point me towards a better option for parsing serial input/waiting for a boot from the embedded device. I am somewhat comfortable with redirection, but not enough to make use of some of the answers here without further explanation.
EDIT: ===========================================================
Using the hint from Jardel Lucca in the comments, I was able to solve the echo issue, and this answer pointed me to how to solve what I now understand was a line buffering issue (note stty options -echo
to disable echoing and raw
to disable line buffering):
#!/bin/bash
stty -F /dev/ttyUSB0 115200 -echo -icrnl raw
cat /dev/ttyUSB0 | tee /dev/tty | grep --max-count=1 -F "root@ramfs:"
I am still trying to work out the issue of the targeted prompt not being detected/ending that command correctly. I can now see that I receive the prompt I am searching for with grep
but the test script does not exit at this point. I am not sure if this is because cat
is still running and holding it open or another issue however as I am not well versed in pipelines like this, but I am wondering if I will need a different way to read out the 'file' to grep.
New output showing the target prompt is received correctly:
Starting internet superserver: inetd.
... ^[[55;100R[ 8.153567] macb ff0c0000.ethernet eth0: Link is Up - 1Gbps/Full - flow control tx
[ 8.161182] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
root@ramfs:~#
Another example:
[[55;100Rroot@ramfs:~# [ 17.369525] macb ff0c0000.ethernet eth0: Link is Up - 1Gbps/Full - flow control tx
[ 17.377132] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
root@ramfs:~# [ 17.369525] macb ff0c0000.ethernet eth0: Link is Up - 1Gbps/Full - flow control tx
^[[55;100R