2

I am trying to use regex for capturing some text pattern in an ouput of a command and create an array list_mqs. The problem is that this previous command get stuck and never terminates. So, when I use a pipeline | egrep, never starts the egrep command because the other routine did not finish.

The patterns that I am trying to capture are: MQ_1A, MQ_F5,MQ_H7.

[user@host] list_mqs=($(command_never_ends | egrep -io "MQ_*"))

Is there any way that I can redirect the output of the previous commands even it never ends(or maybe set a counter and if reached exit), and pass those content to be analyzed by the egrep routine?

1
  • What do you mean by command_never_ends? Is it literally? Because if this one never ends then the array list_mqs should not be necessary. Or you are saying command_never_ends because this one gets stuck and never pipe the output to egrep? Commented Oct 19, 2022 at 20:31

1 Answer 1

0

When you pipe a command/application and this one doesn't show any output it should be due to the buffer.

You have two solution (I guess there are more) to can pipe and get an output:

Solution 1: Using unbuffer

unbuffer command_never_ends | egrep -io "MQ_*"
#Or if the 'command_never_ends' does really finish:
list_mqs=("$(unbuffer command_never_ends | egrep -io "MQ_*")")

Solution 2: Using named pipes

#Create the named pipe
mkfifo pipe1
#Redirecting 'command_never_ends' output (stdout,stderr) to named pipe
command_never_ends &> pipe1

When you use this: command_never_ends &> pipe1 the program will not be started until the named pipe pipe1 has been read. So now you will have to do something like this:

#Reading line per line the output of 'command_never_ends'
while read -r line; do 
   egrep -io "MQ_*" <<< "$line"
done < pipe0

You can see my answer about named pipes using a Java application to send data to a pipe.

Note
I tried using stdbuf command but it seems doesn't work. I know that stdbuf and unbuffer work in different ways. If someone knows how can achieve this using stdbuf it should be useful for me too.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.