Capturing multiple paralleled grep outputs in bash

VY Canis Majoris

My quandary lies with piping grep results to a variable for later usage. Normally, this wouldn't be an issue and I would use $( ) and push the results to the variable as is.

The problem comes in when I need to have multiple instances of ping running in parallel using the & operator. A code snippet:

google_ping=$(ping -c 2 www.google.com | grep -oP '\d+(?=% packet loss)') &
bing_ping=$(ping -c 2 www.bing.com | grep -oP '\d+(?=% packet loss)') &
custom_ping=$(ping -c 2 10.1.1.1 | grep -oP '\d+(?=% packet loss)') &

wait

printf "Google Ping: \t\e[32m %3d\e[0m\n " $google_ping
printf "Bing Ping:\t\e[32m %3d\e[0m\n " $bing_ping
printf "Custom Ping:\t\e[32m %3d\e[0m\n " $custom_ping

I have more pings to run, and this has been shortened to make the code less jumbled than it already is. Essentially, ping each destination twice, retrieve the packet loss for each, and post all results at the same time, rather than as they become available.

The results I get appear to be the error output (which has been 0) rather than the packet loss. For custom_ping, which is a controlled test (always returns 100% packet loss), the result is still 0 rather than the 100 these greps should be retrieving.

Running the commands in line with the printf like so:

printf "Google Ping: \t\e[32m %3d\e[0m\n " $(ping -c 2 www.google.com | grep -oP '\d+(?=% packet loss)') &
#etc...

Occasionally works, but more often than not distorts the output by placing ping results next to one another or within one another, etc. Not to mention I need to use the values of the results later to perform certain actions based on how high or low the packet loss is, I will eventually need to pull the milliseconds the packets took as well.

I have tried different methods of running the pings in parallel; including using the backticks in place of $( ) and redirecting the output via 2>$1. I want to avoid using temporary files as much as possible, if at all.

Is there another method that would be more efficient? Or perhaps a way to place all of the ping results from one site into a variable and use a command or function similar to grep to extract the information I need?

For reference, the entire script is located on pastebin here.

rici

There is simply no way of doing what you want to do without using something like a temporary file. (Or similar semi-persistent storage like memcached or redis.) Fortunately, all of those solutions are reasonably fast. There's a good chance that your /tmp directory is actually a ramdisk, and if not, you could make one or use one of the in-memory cache solutions.

The command you're using:

var=$(pipeline) &

causes var=$(pipeline) to be run in a background subshell. In that shell, the pipeline will be executed and its output captured by the subshell's $var. But that's not useful, since the subshell is about to terminate, taking its variables with it.

&내부에를 넣을 수 있으며이 $(...)경우 값이 $var외부 셸에 할당됩니다 . 그러나 명령 대체 $(pipeline&)는을 ( waitpipeline) 대체 pipeline하므로 백그라운드에서 실행하는 목적을 상당히 무너 뜨 립니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Creating a method with multiple outputs

분류에서Dev

Shiny - multiple outputs to mainPanel

분류에서Dev

Apply multiple conditions to a capturing group

분류에서Dev

Print or capturing multiple objects in R

분류에서Dev

Better way of capturing multiple same tags?

분류에서Dev

Capturing kernel error/stack trace after running a command in bash

분류에서Dev

Bash : ""사이에 Grep

분류에서Dev

Grep square brackets, unix bash

분류에서Dev

Python function with multiple URLs as input, which outputs multiple DataFrames

분류에서Dev

Using grep command for multiple strings

분류에서Dev

grep multiple patterns with different context

분류에서Dev

How do I enable multiple audio outputs on Windows 7?

분류에서Dev

Use one QPainter to paint multiple outputs at once: SVG and QImage

분류에서Dev

bash : ps grep for process with Umlaut (OS X)

분류에서Dev

Bash : grep 고유 라인

분류에서Dev

bash 및 grep으로 libnotify

분류에서Dev

bash, regex의 grep 문제

분류에서Dev

Unsure of grep output because of bash wildcards

분류에서Dev

BASH Grep for Specific Email Address in CSV

분류에서Dev

BASH grep 스크립트

분류에서Dev

Selecting multiple files with in bash

분류에서Dev

bash with multiple long options

분류에서Dev

Multiple String Comparison in Bash

분류에서Dev

bash script: capturing tcp traffic on a remote server sometimes works, sometimes fails. No errors

분류에서Dev

How to grep for two patterns in multiple files

분류에서Dev

just grep multiple pattern on different row

분류에서Dev

bash 내에서 grep 출력을 grep으로 파이핑

분류에서Dev

need to explain bash grep regular expression grep -E '(^|[^0-9.])'2 *.c

분류에서Dev

bash grep 정규식 설명 필요 grep -E '(^ | [^ 0-9.])'2 * .c

Related 관련 기사

뜨겁다태그

보관