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.
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&)
는을 ( wait
를 pipeline
) 대체 pipeline
하므로 백그라운드에서 실행하는 목적을 상당히 무너 뜨 립니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다