cs370:cs_370_-_shell_scripts_and_user_interfaces
Table of Contents
The Shell and User Interfaces
- The shell (and shell scripts) can display simple user interfaces and dialogs for applications that demand them.
zenity
andyad
can be used to generate GUI dialogs.whiptail
can be used to generate TUI (text-based user interface) dialogs.fzf
is a newer tool that can be used to generate TUIs.
Zenity and YAD
- Zenity - http://en.wikipedia.org/wiki/Zenity
- Cross platform
- Create dialogs with the zenity command:
zenity --info --text="This is an information box."
- Commonly used in shell scripts:
#!/bin/sh if zenity --question --text="Please press a button."; then zenity --info --text="You pressed Yes\!" else zenity --error --text="You pressed No\!" fi
Also works with conditional chaining:
#!/bin/sh zenity --question --text="Please press a button." && zenity --info --text="You pressed Yes\!" || zenity --error --text="You pressed No\!"
- Linux GDM login banner example: http://tinyurl.com/nqgmptp
-
- Yet Another Dialog
- A more featureful fork of Zenity
Whiptail
- Create dialogs with the whiptail command:
whiptail --title "Example Dialog" --msgbox "This is an example of a message box. You must hit OK to continue." 8 78
- Can also be used with shell scripts:
#!/bin/sh if (whiptail --title "Example Dialog" --yesno "This is an example of a yes/no box." 8 78) then echo "User selected Yes, exit status was $?." else echo "User selected No, exit status was $?." fi
- Can be used to gauge progress in a shell script:
#!/bin/bash { for ((i = 0 ; i <= 100 ; i+=5)); do sleep 0.1 echo $i done } | whiptail --gauge "Please wait while we are sleeping..." 6 50 0
fzf: fuzzy finder
- The
fzf
command is a commandline filter that can generate minimalistic TUI dialogs quickly.
- Designed to be compatible with the UNIX philosophy, it can be used in command pipelines, functions and shell scripts to display and quickly filter the results sent to its standard input.
**(Do in class)** Zenity example: makescript.sh
Modify the cat2script.sh script to use zenity
dialogs. Add makescript.sh
to your ~/bin
directory.
Link to makescript.sh
code: makescript.sh.
**(Do in class)** fzf example: fzcd
The fzcd
shell function is a version of the ''findcd'' function. It displays all possible directory choices to cd to, based on the find
results. Add this to your ~/.bashrc
file.
The fzcd
code is shown below:
# Use fzf to display directories that I can 'cd' to, # based on a find name pattern: function fzcd { # Note the long command substitution that starts on # the next line with $(find -iname ...: findresults=$(find -iname "*$1*" -printf '%P\n' | while read path; do # If $path is a file, can't cd to it, so have to # trim $path to a directory using rev|cut|rev, # else just echo $path: [[ -f "$path" ]] && (echo "$path" | rev | cut -d/ -f2- | rev) || echo "$path" done | sort | uniq) # <= command substitution finally ends with uniq # As a result of the above, $findresults should contain a list # of directories that I can cd to. # If $findresults actually contains something, cd using fzf: [[ -n "$findresults" ]] && cd "$(echo "$findresults" | fzf)" # Must use "" around the echo argument; see http://tiny.cc/2g78vz # Must use "" around the cd argument; see http://tiny.cc/84a8vz }
cs370/cs_370_-_shell_scripts_and_user_interfaces.txt · Last modified: 2024/06/18 21:42 by jchung