Create bash scripts that utilize the best dialog system that is available. Intended for Linux, but has been tested on macOS and Windows, and should work on other unix-like OSs.
- If it's launched from a GUI (like a
.desktop
shortcut or thedolphin
file manager),- It will prefer kdialog in Qt-based desktops and zenity in other environments.
- If neither of those are available, then
relaunch-if-not-visible
will relaunch the app in a terminal so that a terminal UI can be used.
- If it's launched in a terminal,
- It will use whiptail or dialog.
- If neither of those are available, then it will fallback to basic terminal input/output with tools like
read
andecho
.
Source script-dialog.sh
. Then use the library's variables and functions. The test.sh
script will contain examples of each feature.
The following example assumes it's in the same folder as your script:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${SCRIPT_DIR}"/script-dialog.sh
APP_NAME="My Utility Script"
ACTIVITY="Intro"
message-info "Hello!"
Scripts open in a text editor instead of running, what gives? One of 2 things has happened:
-
The script has not been marked as "executable" in it's permission properties.
That's the execute bit,
chmod +x test.sh
, for you terminal folks. -
Some desktop environments do this as default script behavior, assuming scripts are only run from terminal and edited from GUIs.
You may have to change your file manager preferences on scripts or "Executable Text Files", or right click and choose to "Run As Program".
The latter option may run the script in a terminal instead of GUI. Not much can be done about "GNOME choses to force scripts to open via terminal" type issues. If this is for a permanent script, create an application shortcut (by creating a
.desktop
file in$HOME/.local/share/applications/
).
Name | Use | Description |
---|---|---|
APP_NAME | User-defined | The script's app name, for title bars |
ACTIVITY | User-defined | The current activity, for title bars |
INTERFACE | Override or Detected | the GUI or TUI to use |
GUI | Detected | Whether the interface is a GUI not TUI |
DETECTED_DESKTOP | Detected | Desktop in use |
NOCOLORS | Optional override | disables otherwise-detected use of colored/bolded text basic CLI |
NOSYMBOLS | Optional override | disables otherwise-detected use of unicode symbols in TUIs |
ZENITY_HEIGHT | Optional override | height of zenity dialogs |
ZENITY_WIDTH | Optional override | width of zenity dialogs |
Name | Description | Arguments | Output or Return |
---|---|---|---|
superuser | Attempts to run a priviledged command (sudo or eqiuvalent) | Command to run with elevated priviledge | return code 0 if success, non-zero otherwise |
relaunch-if-not-visible | if neither GUI nor terminal interfaces can be used, relaunch the script in a terminal emulator | ||
message-info | Display an 'info' message box | The text to display | |
message-warn | Display a 'warning' message box | The text to display | |
message-error | Display an 'error' message box | The text to display | |
messagebox | Display a message box | The text to display | |
yesno | Display a yes-no decision message box | The text to display | return code 0 if yes, 1 if no |
inputbox | Display a text input box |
|
the entered text |
userandpassword | Display a (single or series of) input box(es) for entering a username and a password |
|
|
password | Display an input box for entering a password | The text to display for password entry | the entered text |
display-file | Display the contents of a file |
|
|
checklist | Display a list of multiply-selectable items |
|
Value text of the selected item (or the default item) |
radiolist | Display a list of singularly-selectable items |
|
Value text of the selected item (or the default item) |
progressbar | A pipe that displays a progressbar | the current value of the bar (repeatable, should be piped) | |
progressbar_update | Updates the value of the progressbar (call from within the progressbar piped block) |
|
|
progressbar_finish | Completes the the progressbar (call from within the progressbar piped block) | ||
filepicker | Display a file selector dialog |
|
Path to selected file |
folderpicker | Display a folder selector dialog | The starting folder | Path to selected folder |
datepicker | Display a calendar date selector dialog | The starting folder | Selected date text (DD/MM/YYYY) |