Bash: Watching selected log file.

Reference: http://askubuntu.com/questions/1705/how-can-i-create-a-select-menu-in-a-shell-script

There is an answer inside which actually works for my situation.

Previous bash script which was not working

#!/bin/bash

LIST=”syslog dmesg kern boot dpkg mintsystem Exit”
PS3=’Choose a log file to follow: ‘

select var in $LIST
do
if [ “$LIST” = “syslog” ]
then
watch tail /var/log/syslog
elif [ “$LIST” = “dmesg” ]
then
watch tail /var/log/dmesg
elif [ “$LIST” = “kern” ]
then
watch tail /var/log/kern.log
elif [ “$LIST” = “boot” ]
then
watch tail /var/log/boot.log
elif [ “$LIST” = “dpkg” ]
then
watch tail /var/log/dpkg.log
elif [ “$LIST” = “mintsystem” ]
then
watch tail /var/log/mintsystem.log
elif [ “$LIST” = ‘Exit’ ]
then
exit
fi
done

When executed there was menu, however nothing came out of the choice I made.

Current working script using Select Case

#!/bin/bash

#filename:follow.sh
#Menu script for watching selected log file.

LIST=(“syslog” “dmesg” “boot.log” “kern.log” “auth.log” “mintsystem.log” “Quit”)
PS3=’Please choose a log to watch: ‘

select var in “${LIST[@]}”
do
case $var in
“syslog”) watch tail /var/log/syslog
;;
“dmesg”) watch tail /var/log/dmesg
;;
“boot.log”) watch tail /var/log/boot.log
;;
“kern.log”) watch tail /var/log/kern.log
;;
“auth.log”) watch tail /var/log/auth.log
;;
“mintsystem.log”) watch tail /var/log/mintsystem.log
;;
“Quit”) exit;break
;;
*) echo invalid choice
;;
esac
done

Why PS3?

~/script $ set |grep PS
GROUPS=()
PS1=’${debian_chroot:+($debian_chroot)}\[33[01;32m\]\u@\h\[33[01;34m\] \w \$\[33[00m\] ‘
PS2=’> ‘
PS4=’+ ‘

Notice PS1, PS2 and PS4 all are set, PS3 is not set.

PS1 is like for example root@linux# the shell prompt is always PS1.

Setting PS3 will make the menu having a more interactive shell:

~/script $ ./follow.sh
1) syslog 3) boot.log 5) auth.log 7) Quit
2) dmesg 4) kern.log 6) mintsystem.log
Please choose a log to watch:

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s