I have a pair of linux C programs that use pseudo terminals /dev/pts/*
to communicate to each other. The pty on which can communicate is passed as command line argument to these programs.
I can create a pair of pty devices using socat as follows:
socat -d -d pty,raw,echo=0 pty,raw,echo=0
The output of above gives as:
2018/07/05 17:56:54 socat[58319] N PTY is /dev/pts/1
2018/07/05 17:56:54 socat[58319] N PTY is /dev/pts/3
2018/07/05 17:56:54 socat[58319] N starting data transfer loop with FDs [7,7] and [9,9]
how can I extract the pty nodes, /dev/pts/*
from socat
's output and pass to my application via command line in a shell script:
$./test_pty_app /dev/pts/1 &
$./test_pty_app /dev/pts/2 &
I saw a similar question that can do this in python here Thanks!
Updated Answer
It looks like you'll have to use a file if socat
must be backgrounded.
( socat ... 2>&1 | grep -Eo "/dev/pts/\d+" > /tmp/a ) &
portA=$(head -n 1 /tmp/a)
portB=$(tail -n 1 /tmp/a)
Original Answer
@jeremysprofile 's answer is probably more sensible but, just for fun, you could do either of these also:
socat ... | grep -Eo "/dev/pts/\d+" | { read portA; read portB; }
Or, using bash's "process substitution", you could do:
{ read portA; read portB; } < <(socat ... | grep -Eo "/dev/pts/\d+")
Then you would do this after either of them:
./test_pty_app $portA &
./test_pty_app $portB &