Hello,
Sometime I need to extract specific value (or more values) from my system using command outputs (in order to use this values for conditional testing in scripts).
For example: Get my CPU temperature and if it is above 60 °C then do something.
For this scenario I use command "watch -n 2 sensors" which will output something like this:
Let's say I want to extract temperature value in second line from bottom. Should I use some grep pattern search and other text-tweaking methods or is there cleaner way to get CPU temperature (just the integer)?
Or another scenario: I use my Linux PC as wifi access point and I want to know when specific device connects to my AP. For this I use command "sudo iw dev wlan0 station dump" which outputs something like this:
I would like to test it if MAC address (station) matches "2f:ba:a6:a1:cb:31" and "authorized: yes" is present. Should I again use some string processing functions or is there better way to get information I need in much cleaner way?
Right now I evaluating for MAC address using this command:
But I have trouble evaluating for string "authorized: yes" because string contains some strange whitespace characteres.
Are there nicer solutions for issues mentioned above? I hope so, because these do not feel right to me
Sometime I need to extract specific value (or more values) from my system using command outputs (in order to use this values for conditional testing in scripts).
For example: Get my CPU temperature and if it is above 60 °C then do something.
For this scenario I use command "watch -n 2 sensors" which will output something like this:
Code:
coretemp-isa-0000
Adapter: ISA adapter
Package id 0: +34.0°C (high = +100.0°C, crit = +100.0°C)
Core 0: +33.0°C (high = +100.0°C, crit = +100.0°C)
Core 1: +34.0°C (high = +100.0°C, crit = +100.0°C)
Core 2: +34.0°C (high = +100.0°C, crit = +100.0°C)
Core 3: +34.0°C (high = +100.0°C, crit = +100.0°C)
pch_skylake-virtual-0
Adapter: Virtual device
temp1: +41.0°C
BAT0-acpi-0
Adapter: ACPI interface
in0: 14.40 V
asus-isa-0000
Adapter: ISA adapter
cpu_fan: 1700 RPM
temp1: +33.0°C
nouveau-pci-0100
Or another scenario: I use my Linux PC as wifi access point and I want to know when specific device connects to my AP. For this I use command "sudo iw dev wlan0 station dump" which outputs something like this:
Code:
Station 2f:ba:a6:a1:cb:31 (on wlan0)
inactive time: 3000 ms
rx bytes: 71000
rx packets: 1564
tx bytes: 735079
tx packets: 3305
tx failed: 0
tx bitrate: 54.0 MBit/s
rx bitrate: 24.0 MBit/s
authorized: yes
authenticated: yes
associated: yes
WMM/WME: no
TDLS peer: yes
DTIM period: 2
beacon interval:100
short slot time:yes
connected time: 212 seconds
Right now I evaluating for MAC address using this command:
iw dev wlan0 station dump | grep -c '2f:ba:a6:a1:cb:31'
which outputs "1" if there is match.But I have trouble evaluating for string "authorized: yes" because string contains some strange whitespace characteres.
iw dev wlan0 station dump | grep -c 'authorized:yes'
outputs "0"iw dev wlan0 station dump | grep -c 'authorized: yes'
outputs "0"iw dev wlan0 station dump | grep -c 'authorized: yes'
outputs "0"Are there nicer solutions for issues mentioned above? I hope so, because these do not feel right to me
