[kwlug-disc] possible presentation ideas

Khalid Baheyeldin kb at 2bits.com
Sat Dec 15 19:48:56 EST 2018


On Fri, Dec 14, 2018 at 9:46 PM <tomg at sentex.ca> wrote:

Some comments, hope they help.

> AWK.  I haven't used this yet.

Awk is pretty useful, and I use it a lot.

In its simplest form, I just use it as an alternative to the command: cut.

awk '{print $2}'

You can pass in a field delimiter, such as a comma or colon:

awk -F, '{print $2}'

You need to understand that its 'code' is 3 sections:
BEGIN (once before processing any input lines)
END (once after processing all input lines)
The above two are optional.

In between is logic for every input line

awk 'BEGIN { stuff to do initially}
{ stuff for every input line }
END {stuff after all lines were processed }'

Here is an example snippet from a script I wrote today as a minimalist
radio stream player, which runs on the Raspberry Pi as well as on a
laptop with a GUI:

DATA="$HOME/data/radio-list"

while true
do
  echo " "
  awk -F, '{print $1 ". " $2}' $DATA
  echo -n "Select a channel from the above list: "
  read SEL

  STREAM=`grep "^$SEL," $DATA | awk -F, '{print $3}'`
  if [ "$STREAM" != "" ]; then
    mpg123 -quiet $STREAM
  fi
done

The data file is like this:

1,Channel A,URL
2,Channel B,URL
...

Note that unlike the shell, the variables are not limited to $9. You
can have $14, $23, ...etc.
And you can even reference the last field using $NF without counting
where it is.

For example to get the extension of a file, you do this:

$ echo file.ext | awk -F. '{print $NF}'
ext

To calculate rate in MB per second when you have a file with a list,
the first being MB, and the second being seconds, you do this:

awk '{ size_mb += $1; total_secs += $2 } END { print size_mb /
total_secs * 60 * 60 }'

Note the END means do this once you are done with the input lines.

You can format numbers as decimals, ...etc.

awk  '{printf "%4.2f\n", $SIZE_OUT/$SIZE_IN*100}'

To sum the first field in a file, you do:

 awk '{ sum += $1 } END { print sum }'

To pass variables from the shell to awk you use:

awk -v var=$SOMETHING '{ print var}'

Obviously, you can check field values, and other conditions:

awk '{if ($11 == "something") { do stuff here }}'

By now, you know that the STREAM line in the media player script
above, can be re-written without grep, as:

STREAM=`awk -F, -v sel=$SEL '{ if ($1 == sel) {print $3}}' $DATA`

> Advanced SED.

Likewise, something I use a lot, usually to replace strings or strip
strings in shell scripts.

> Advanced VI or VIM.

I have been using Vi for 30+ years now, and Vim is the natural
extension. I think I only use around 40% or so of its features.

> Advanced BASH.

Contrary to what has been said about sh/bash syntax being odd, it is
just a matter of getting used to it. It no more odd than regular
expressions. Try to explain that to someone who programs Windows.

> Even RCS or CVS.

Ignore both of these, since they are obsolete, by two generations. The
first generation was SVN, which was in turn obsoleted by Git. Go for
git if you need it.

> How (U)EFI works.

I installed 18.04 LTS on a laptop that has UEFI, and just ignored the
fact that it is so, and everything worked as expected. Sometimes,
things are best left alone as long as they work.

> How GUID partition tables work on Linux.

I don't know how it works, but it is just a way of using a unique ID
for a partition on a particular disk. So instead of using /dev/sda1 it
is replaced by a long ID.

Example from my /etc/fstab:

UUID=4c612617-b1ea-4e71-xxxx-blah / ext4
discard,noatime,nodiratime,errors=remount-ro 0 1

> What sysfs is and how it works and why it exists.

The sysfs is very useful, since it abstracts certain hardware and
operating system stuff.

Here is an example. I use a script to check the battery of my laptop
and it warns me when it reached a certain threshold:

# Battery threshold, as a percent
THRESHOLD=10
# Number of seconds to check when on A/C power, or battery not low
LONG=30
# Number of seconds to check when on battery and battery is low
SHORT=5
# Sound to play
SOUND="/usr/share/sounds/ubuntu/notifications/Amsterdam.ogg"
PROG=`basename $0`

# Check if other instances are running
NUM_PROCS=`ps -ef | grep "$PROG" | egrep -vw "vi|grep" | wc -l`
if [ "$NUM_PROCS" -gt 2 ]; then
  exit
fi

# Main loop
while true
do
  SLEEP_INTERVAL=$LONG
  STATE=`cat /sys/class/power_supply/BAT0/status`
  if [ "$STATE" = 'Discharging' ]; then
    CAPACITY=`cat /sys/class/power_supply/BAT0/capacity`
    if [ "$CAPACITY" -le "$THRESHOLD" ]; then
      SLEEP_INTERVAL=$SHORT
      logger -i -t "$PROG" "Battery level is $CAPACITY %, and no A/C
power connected"
      notify-send 'Connect A/C Power' "Battery level is $CAPACITY %.
Connect A/C power immediately"
      ogg123 -q $SOUND
    fi
  fi
  sleep $SLEEP_INTERVAL
done

Note how status and capacity are extracted from sysfs.

Again, this is one thing where I use only 2% of what it does or can do.




More information about the kwlug-disc mailing list