1

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
Douglas B
  • 585
  • 2
  • 13
  • 1
    Did you try adding `-echo` to your first attempt as an additional parameter for `stty`? That should prevent it from echoing. – Jardel Lucca Mar 14 '23 at 18:32
  • @JardelLucca just tried that, it does seem to be closer to my desired behavior (confusingly enough, from the man page: "[-]echo echo input characters", not what I would have expected that to do) although I still don't seem to be able to consistently capture the prompt, I believe cat may not be moving onto that line as it does not generally end with a newline. Thank you for that tip though – Douglas B Mar 14 '23 at 19:21
  • 2
    Please read man pages completely, this is always a good idea: the minus character inverts the option, so `-echo` means _no echo_. – the busybee Mar 14 '23 at 20:25
  • 1
    @thebusybee, thanks for clarifying. The `stty` parameters syntax is quite confusing. I remember the first time I realized `-echo` actually means `no echo` it was so baffling. – Jardel Lucca Mar 14 '23 at 23:37
  • 1
    @thebusybee good catch. While I try to be thorough, its hard to digest multiple relatively dense pages at once for a larger project like I'm working on, so thank you for drawing my attention to that detail – Douglas B Mar 15 '23 at 00:49

0 Answers0