Friday, November 9, 2018

BASH: Assign Output of Command to a Variable

In Powershell it is pretty easy to assign output to a variable, but not being a BASH scripter I needed to look up how to do it there. I found a good article at CyberCiti.

The goal was to create a cron job to update one of my Raspberry Pi systems. As I am a packager I like to create logs, so I want to create a log with the date and time of the update job. To automate this, I needed to get the date from the system and parse it. I also want to see when ClamAV's definitions were last updated. So I have two instances where I turn command output into a string variable:

#!/bin/bash
LOG=$(date +/home/david/logs/%Y%m%d_at_%H%M.log)
touch $LOG
BAR="--------------------------------------------------"
echo $BAR >> $LOG
sudo apt-get update >> $LOG
echo $BAR >> $LOG
sudo apt-get upgrade -y >> $LOG
echo $BAR >> $LOG
sudo apt-get dist-upgrade -y >> $LOG
echo $BAR >> $LOG
sudo apt-get autoremove -y >> $LOG
echo $BAR >> $LOG
echo Last /var/log/clamav/freshclam.log >> $LOG
CLAM=$(tail -n 1 /var/log/clamav/freshclam.log)
DATE="${CLAM:0:24}"
echo "${DATE}" >> $LOG

My Log then looks like this (I shortened it a bit here) with above examples highlighted:

david@rpi3b ~/bin $ cat ~/logs/20181109_at_0923.log 
--------------------------------------------------
Hit:1 http://archive.canonical.com/ubuntu xenial InRelease
[...]
Hit:17 https://deb.etcher.io stable Release
Reading package lists...
--------------------------------------------------
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
--------------------------------------------------
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
--------------------------------------------------
Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
--------------------------------------------------
Last /var/log/clamav/freshclam.log
Fri Nov  9 08:26:33 2018

Another nifty page from CyberCiti details the below technique:

The last trick there was to get just the timestamp from the ClamAV log. The log in it's raw form looks like:

Fri Nov  9 08:26:33 2018 -> --------------------------------------

I could leave that as it is (it is just a log, after all), but getting a substring is what the boss would want if this was for her, so I parse it out. The desired string is 24 characters long, so we get the string and parse it:

CLAM=$(tail -n 1 /var/log/clamav/freshclam.log)

Then create a masked version with substring expansion in a new variable from $CLAM and character position 0 and with a length of 24 (so, characters 0:23):

DATE="${CLAM:0:24}"

Then echo that into my log:

echo "${DATE}" >> $LOG

No comments: