Text-mode user interfaces can be built with ncurses or curses C libraries. To speed up the process, one can use the "dialog" utility that is based on ncurses and is very stable but has its limitations. Basic UIs can easily be built using bash and dialog. Here are some sample screen-shots from the script that we soon go through:
Lets call this script "dlg". It works with FIFO/Pipes instead of reading and writing to/from files, which every other howto on dialog programming seems to do.
- Log to syslog using 'logger' utility
- Reads user selection from menu (many other howtos do not cover this!)
- Traps keyboard interrupts using 'trap'
#!/usr/bin/env bash
# Global Variables
DIALOG=/usr/bin/dialog;
WARNING_TEXT=`/bin/cat ./warning.txt`;
declare -i _OPTION_;
FIFO_FILE="/tmp/pipefile.$$";
DTITLE='Dialog-Creator';
# Syslog Variables
LOG_TAG="DLG";
LOG_DIR="/var/log";
LOG_PARAMS="-i -t $LOG_TAG -f /var/log/dlg.log";
LOG_PROG="/bin/logger";
LOG="$LOG_PROG $LOG_PARAMS";
# FIFO and File Descriptor code is based on my previous post on async write/read to pipes
# Functions
set_output_fd () {
# Assigning file descriptor 3 to the pipe
exec 3> $FIFO_FILE || {
echo "$(basename $0): getoptions: Error creating named pipe. Exiting." >&2;
exit 2;
}
}
close_output_fd () {
exec 3>&-;
/bin/cat /dev/null > $FIFO_FILE;
}
read_user_input () {
_OPTION_=`/bin/cat $FIFO_FILE`;
}
exit_dialog_ () {
$DIALOG --backtitle $DTITLE --msgbox "Received Termination Signal, Exiting!" 6 40;
exit;
}
_MAIN_ () {
$LOG "Initializing DLG";
set_output_fd;
# Output to File Descriptor 3
$DIALOG --backtitle $DTITLE --menu "Choose one:" 15 30 6 \
1 "User Management" \
2 "Network Management" \
3 "Service Status" \
4 "Lock Terminal" \
5 "Access Control" \
6 "Quit" 2>&3;
read_user_input;
case "$_OPTION_" in
"1")
_OPTION_1_;
exit 1
;;
"2")
_OPTION_2_;
exit 1
;;
"3")
_OPTION_3_;
exit 1
;;
"4")
_OPTION_4_;
exit 1
;;
"5")
_OPTION_5_;
exit 1
;;
"6")
_OPTION_6_;
;;
esac
}
_OPTION_1_ () {
$DIALOG --backtitle $DTITLE --msgbox "Inside _OPTION_1 ()" 22 70;
}
_OPTION_2_ () {
$DIALOG --backtitle $DTITLE --msgbox "Inside _OPTION_2 ()" 22 70;
}
_OPTION_3_ () {
$DIALOG --backtitle $DTITLE --msgbox "Inside _OPTION_3 ()" 22 70;
}
_OPTION_4_ () {
$DIALOG --backtitle $DTITLE --msgbox "Inside _OPTION_4 ()" 22 70;
}
_OPTION_5_ () {
$DIALOG --backtitle $DTITLE --msgbox "Inside _OPTION_5 ()" 22 70;
}
_OPTION_6_ () {
$DIALOG --backtitle $DTITLE --msgbox "Inside _OPTION_6 ()" 22 70;
close_output_fd;
}
_QUIT_ () {
close_output_fd;
}
#########################################################################
# Code Starts Here
# Catch Signals and Disable Interrupts
trap exit_dialog_ SIGINT SIGTERM SIGTSTP;
$DIALOG --backtitle $DTITLE --msgbox "$WARNING_TEXT" 22 70;
_MAIN_;
_QUIT_;
Here is the text for "warning.txt" if you need it:
This system contains data belonging to Weqaar A. Janjua. This computer system, including all related equipment, networks, and network devices (specifically including Internet access) are provided only for authorized use. Computer systems may be monitored for all lawful purposes, including to ensure that their use is authorized, for management of the system, to facilitate protection against unauthorized access, and to verify security procedures, survivability, and operational security. Monitoring includes active attacks by authorized entities to test or verify the security of this system. During monitoring, information may be examined, recorded, copied and used for authorized purposes. All information, including personal information, placed or sent over this system may be monitored.
Use of this computer system, authorized or unauthorized, constitutes consent to monitoring of this system. Unauthorized use may subject you to criminal prosecution. Evidence of unauthorized use collected during monitoring may be used for administrative, criminal, or other adverse action. Use of this system constitutes consent to monitoring for these purposes.
Use of this system implies understanding of these terms and conditions.
This is just the basic version of the script. I have to run, will continue next week. We will cover text boxes, check lists, etc; not to forget the Python-dialog version of the script!
No comments:
Post a Comment