Ika Ika - 2 months ago 4x
Bash Question

Linux shell scipting: How to store output from terminal in integers (but only numbers)?

I'm new to shell scripting and here is my problem:

I want to store PID's from output of

airmon-ng check
to some variables (for ex: $1, $2, $3) so that I can execute
kill $1 $2 $3
here is sample output of
airmon-ng check

Found 3 processes that could cause trouble.
If airodump-ng, aireplay-ng or airtun-ng stops working after
a short period of time, you may want to kill (some of) them!

PID Name
707 NetworkManager
786 wpa_supplicant
820 dhclient

I want to grab numbers 707, 786, 820.

I tried using
set 'airmon-ng check'
and then using for loop:

set `airmon-ng check`
for (( i=0; i<=n; i++ ))
echo $i

it outputs 1,2,3,...36
not words or numbers so I couldn't figure out how I should do it.

So please help me.


airmon-ng check | egrep -o '\b[0-9]+\b' | xargs kill

egrep is grep with extended regular expressions (like grep -E), -o says to extract only the matching parts, \b matches word boundaries so you don't get any numbers accidentally occuring in process names or something, [0-9]+ matches one or more decimal digit, xargs kill passes all the matches as arguments to the kill command.

Note that parsing output intended to be read by humans might not always be a good idea. Also, just killing all those processes doesn't sound too smart either, but proper usage of airocrack is beyond this question.