`LOGFILE=${1:-/var/log/syslog}` 起什么作用?

`LOGFILE=${1:-/var/log/syslog}` 起什么作用?

我偶然发现这个脚本包含:

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

这条线起什么作用?

答案1

该命令:LOGFILE=${1:-/var/log/syslog}是以下内容的简写:

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

如果未传递参数 1,则您会看到:

yad-日志文件 1.png

如果传递参数 1:

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

你看:

yad-日志文件 2.png


原始代码在问题链接已修改:

#!/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 $?

相关内容