Using command outputs in another scripts

hal_sk

New Member
Joined
May 25, 2020
Messages
19
Reaction score
3
Credits
188
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:
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
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:
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
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:
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 :)
 


Grep is a nice tool, but Awk is grep on steroids.

I would learn the following tools to really help you. There are a lot more, but these can help you with text processing a majority of the time.

  • grep
  • awk
  • sed
  • cut
  • head
  • tail
  • sort
  • uniq
As noted, awk is incredibly powerful in what you are asking.
 
Moving this to Command Line, where scripts are also handled.

Wizard
 
Try running this in the background (apply to others)

Code:
#! /bin/bash

while [ 1 ]
do

# Gives you the integer (%d) value of your Temps)
iiTemps=$(sensors | grep core | cut -d+ -f2 | cut -d. -f1)

if [ $iiTemps -gt 60 ]
then
# What commands to execute
fi

# Decrease to whatever N seconds precision you need.
sleep 5

done

Sorry for lack of indentation/typos, on my phone.
 
Most of above mentioned command were not know to me. Thanks. I will put them into use.
And is there more reliable method to get system data in general? It would be nice if outputed data from commands:
sensors
or
iw dev wlan0 station dump
or similar... could be retrieved in JSON format, let's say using flag "-json" in command.
 
IDK about json objects. That's up to software and most lower/lowish-level stuff only outputs to stdout. But you can redirect it to a text file if you feel you can't parse everything at once:

Code:
# Dump sensors output to file

# Create or overwrite file
sensors > /tmp/dump.txt

# Create or append to file
sensors >> /tmp/dump.txt

You can't jq it, but you could parse it with your own script using grep, awk, etc. or get fancy and bring Python into the mix.
 

Members online


Top