Table of Contents

The Shell and User Interfaces


Zenity and YAD

zenity --info --text="This is an information box."
#!/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\!"

Whiptail

whiptail --title "Example Dialog" --msgbox "This is an example of a message box. You must hit OK to continue." 8 78
#!/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
#!/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


Zenity example: makescript

Modify the cat2script script to use zenity dialogs.

Link to makescript code: makescript.


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.

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
}