Quantcast
Channel: User ozzy - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 31

Answer by ozzy for Getting the last sentence of a running program from stdout/stderr

$
0
0

This should work:

grep -m1 -oP '(\d{1,3}\.?){4}:\d{1,5}' <( your-qemu-command 2>&1 )
  • <( your-qemu-command 2>&1 ) is a process substitution that launches qemu with stdout/stderr redirected into an anonymous pipe or FIFO;
  • grep reads from the pipe/FIFO, roughly looking for the pattern IP:PORT, with IP refering to an IPv4 address. The -o option to grep ensures that it will only print the IP:PORT combination of a matching line; the -m1 option ensures that grep will return to the prompt once it has found what it was looking for (i.e. exit on first match). The -P option indicates use of Perl regular expression syntax.
  • note that qemu remains running in the background until it finishes. Any output on stdout/stderr by qemu after termination of the grep command is lost, but since the IP:PORT combination is supposed to be the last output, this should be of no concern.

If you want to capture the output of the above command, simply wrap it in a command substitution $(...) like so:

ip_port=$( grep ...etc )

Viewing all articles
Browse latest Browse all 31

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>