5

I stumbled across this script containing:

LOGFILE=${1:-/var/log/syslog}

What does this line do?

6

The command: LOGFILE=${1:-/var/log/syslog} is shorthand for:

if [[ "$1" == "" ]]               # if parameter 1 is blank
then
    LOGFILE="/var/log/syslog"     # LOGFILE set to /var/log/syslog
else
    LOGFILE="$1"                  # LOGFILE set to parameter 1
fi

If parameter 1 is not passed you see:

yad-logfile 1.png

If you pass paraemeter 1:

journalctl -b > /tmp/messages
yad-logfile /tmp/messages

you see:

yad-logfile 2.png


The original code in question link was modified:

#!/bin/bash

# NAME: yad-logfile
# DATE: May 19, 2019.

# From: https://sourceforge.net/p/yad-dialog/wiki/LogViewer/

# This script demonstrates new features of list dialog. Script displays content
# of specified log file and mark some special strings: with word "kernel" by
# setting italic font, with word "error" by light yellow background and with
# word "warn" by pink background 

LOGFILE=${1:-/var/log/syslog}

 PARSER='{font=""; color="#FFFFFF"}; \
/CRON/   {font="italic"}; \
/smartd/ {color="#FFF4B8"}; \
/upower/ {color="#FFD0D8"}; \
OFS="\n" {print $1 " " $2, $3, $4, substr($5,0,index($5,":")-1), \
substr($0,index($0,$6)), font, color; fflush()}'

cat $LOGFILE | awk "$PARSER" | \
yad --title="Log viewer" --window-icon=logviewer \
    --button=gtk-close --geometry 600x350 \
    --list --text="Content of $LOGFILE" \
    --column Date --column Time --column Host \
    --column Tag --column Message:TIP \
    --column @font@ --column @back@

exit $?
  • 1
    IMHO if [[ "$1" == "" ]] is not entirely equivalent. This tries to expand $1, whereas parameter expansion is intended for cases where $1 was not defined or unset. It is far more useful with environment variables like HOME for cases when they may be missing. Setting a variable to empty string in this code would produce similar effect but is not the same no variable in environment. – Sergiy Kolodyazhnyy May 20 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.