📜 ⬆️ ⬇️

Pseudographics in Unix / Linux console. A little more user friendly than you could afford

Hello! It happens, I want to forget about the “boring” console, dilute it with at least a bit of interactive, especially if you often have to do the same operations and even more so if you write scripts to facilitate your fate. So, pseudographics. Behind me reader, I will show you such a console!

Surely, many thought when running make && make install on FreeBSD or apt-get install, for example on Debian, how nice it would be to use such configuration dialogs. They are unduly forgotten and are almost never used in user scripts. Very, very vain.

This can be fixed by a great dialog utility.
It is present in the seventh, eighth branch of FreeBSD, and in Debian, for example, you can do apt-get install dialog. Actually, these are different programs.
FreeBSD is a bit simpler version:
$ dialog

dialog version 0.3, by Savio Lam (lam836@cs.cuhk.hk).
patched to version 0.4 by Stuart Herbert (S.Herbert@shef.ac.uk)
Changes Copyright (C) 1995 by Andrey A. Chernov, Moscow, Russia
patched by Anatoly A. Orehovsky (tolik@mpeks.tomsk.su)

* Display dialog boxes from shell scripts *

Usage: dialog --clear
dialog --create-rc <file>
dialog [--title <title>] [--clear] [--hline <line>] [--hfile <file>]\
<Box options>

Box options:

--yesno <text> <height> <width> [yes|no]
--msgbox <text> <height> <width>
--prgbox "<command line>" <height> <width>
--infobox <text> <height> <width>
--inputbox <text> <height> <width> [<init string>]
--textbox <file> <height> <width>
--menu <text> <height> <width> <menu height> <tag1> <item1>...
--checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...
--radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...
--ftree <file> <FS> <text> <height> <width> <menu height>
--tree <FS> <text> <height> <width> <menu height> <item1>...

In the Linux version there are more features:
$ dialog
cdialog (ComeOn Dialog!) version 1.1-20080316
Copyright 2000-2006,2007 Thomas E. Dickey
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

* Display dialog boxes from shell scripts *

Usage: dialog <options> { --and-widget <options> }
where options are "common" options, followed by "box" options

Special options:
[--create-rc "file"]
Common options:
[--ascii-lines] [--aspect <ratio>] [--backtitle <backtitle>]
[--begin <y> <x>] [--cancel-label <str>] [--clear] [--colors]
[--cr-wrap] [--default-item <str>] [--defaultno] [--exit-label <str>]
[--extra-button] [--extra-label <str>] [--help-button]
[--help-label <str>] [--help-status] [--ignore] [--input-fd <fd>]
[--insecure] [--item-help] [--keep-tite] [--keep-window]
[--max-input <n>] [--no-cancel] [--no-collapse] [--no-kill]
[--no-label <str>] [--no-lines] [--no-ok] [--no-shadow] [--nook]
[--ok-label <str>] [--output-fd <fd>] [--print-maxsize] [--print-size]
[--print-version] [--separate-output] [--separate-widget <str>]
[--shadow] [--single-quoted] [--size-err] [--sleep <secs>] [--stderr]
[--stdout] [--tab-correct] [--tab-len <n>] [--timeout <secs>]
[--title <title>] [--trace <file>] [--trim] [--version] [--visit-items]
[--yes-label <str>]
Box options:
--calendar <text> <height> <width> <day> <month> <year>
--checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...
--dselect <directory> <height> <width>
--editbox <file> <height> <width>
--form <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
--fselect <filepath> <height> <width>
--gauge <text> <height> <width> [<percent>]
--infobox <text> <height> <width>
--inputbox <text> <height> <width> [<init>]
--inputmenu <text> <height> <width> <menu height> <tag1> <item1>...
--menu <text> <height> <width> <menu height> <tag1> <item1>...
--mixedform <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>...
--mixedgauge <text> <height> <width> <percent> <tag1> <item1>...
--msgbox <text> <height> <width>
--passwordbox <text> <height> <width> [<init>]
--passwordform <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
--pause <text> <height> <width> <seconds>
--progressbox <height> <width>
--radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...
--tailbox <file> <height> <width>
--tailboxbg <file> <height> <width>
--textbox <file> <height> <width>
--timebox <text> <height> <width> <hour> <minute> <second>
--yesno <text> <height> <width>

')
Actually, already from the list of options, it is clear what this program will do, but for clarity, I will show an example.
Backup files and force dump to the dump disk of the selected database (or all) MySQL:
$ cat ./xbackup.sh
#!/bin/sh

SRC = "/usr/home/dev/"
SITE = "${SRC}_Web/"
PROJECT = "${SRC}_Project/"

#
DST = "/usr/home/share/BACKUP/"

# Mysql
SQLLOGIN = "root"
SQLPASS = "TopSecretPassword"
SQLHOST = "localhost"

/* --------------------------------------------------------------------------------------------------------- */
#
DATE = ` date "+%Y.%m.%d_%H-%M-%S"`
DSRC = ""
# ( )
backupType = 0

#
DIALOG = dialog
#
tempfile = ` mktemp /tmp/bkp.XXXXXX `
trap "rm -f $tempfile" 0 1 2 5 15

#
InfoDialog () {
${ DIALOG } --msgbox "$1" 5 70
}

#
BackupFiles () {
if [ ! -d ${ 2 } ] ; then
mkdir -p ${ 2 }
fi
tar -c --gzip -f "${2}${DATE}.tar.gz" "${1}"
InfoDialog "OK -> ${2}${DATE}.tar.gz"
}

#
BackupMySQL () {
case ${ 1 } in
"All_Database" )
mysqldump -R -E --triggers -u "${SQLLOGIN}" -p "${SQLPASS}" --all-databases > "${tempfile}"
;;
"" )
InfoDialog "Backup has been canceled"
exit
;;
* )
mysqldump -R -E --triggers -u "${SQLLOGIN}" -p "${SQLPASS}" ${ DBase } > "${tempfile}"
;;
esac
cp "${tempfile}" "${DST}${1}-${DATE}.sql"
# gzip, —
echo tar -c --gzip -f "${2}${DATE}.dump" "${tempfile}"
InfoDialog "OK -> ${2}${DATE}.dump"
}

#
ChooseDatabase () {
#
DBases = ` echo "SHOW DATABASES" | mysql -u "${SQLLOGIN}" -p "${SQLPASS}" `
# All_Database
List = "All_${DBases}"
#
$DIALOG --title "Please choose a Database " --tree "FS" "List of existing databases" 14 78 7 ${ List } 2> ${ tempfile }

case $? in
0 )
#
DBase = ` cat ${ tempfile } `
BackupMySQL ${ DBase }
;;
1|255 )
# ( )
InfoDialog "Backup has been canceled"
exit
;;
esac
}

# ( )
ChooseDomain () {
Paths = ` ls ${ 1 } `
List = ""
isEmpty = true
for dName in ${ Paths }
do
# www ( )
if [ -d "${1}${dName}/www" ] || [ -d "${1}${dName}/htdocs" ] ; then
List = "${List} ${dName}"
isEmpty = false
fi
done


# ,
if ${ isEmpty } ; then
Count = ` echo ${ SRC } | wc -c | tr -d ' '`
DSRC = ` echo ${ 1 } | awk "{ print substr($ 1, ${Count}) }"`
return 0;
fi

#
$DIALOG --title "Please choose a path " --tree "FS" "List of existing domains" 14 78 7 ${ List } 2> ${ tempfile }

case $? in
0 )
DName = ` cat ${ tempfile } `
ChooseDomain "${1}${DName}/"
;;
1|255 )
InfoDialog "Backup has been canceled"
exit
;;
esac

}

ProcessBackup () {
case $1 in
Site )
ChooseDomain ${ SITE }
BackupFiles "${SRC}${DSRC}" "${DST}${DSRC}"
;;
MySQL )
ChooseDatabase
;;
* )
;;
esac
}

TypeDialog () {
${ DIALOG } --title "Choose backup type:" --menu "This is backup types" 12 50 4 \
"Site" "backup web-site" \
"MySQL" "Mysql database" 2> ${ tempfile }

case $? in
0 )
ProcessBackup ` cat ${ tempfile } `
TypeDialog
;;
1|255 )
InfoDialog "Backup has been canceled"
exit
;;
esac

}

TypeDialog


It looks like this:
Main menu
Site selection for backpap
Select base for backup
The base has succumbed


That's all. Now, when the script is running, you can admire interactive “windows”. In the same way, it is easy to implement the execution of any console commands, and combining this with running putty from the command line with authorization by key, you can give a nice console interface for, for example, restarting Apaches on a dev server (of course through sudo with hard restrictions), to all developers in a team, even unfamiliar with * nix systems.
I am sure that the knowledge presented to someone will come in handy in order to make the console of your server “more User Friendly”.

PS In the process of writing I came across good examples http://www.oszone.net/2833/ , I immediately implemented something from there.

Source: https://habr.com/ru/post/104981/


All Articles