User Tools

Site Tools


cs370:cs_370_-_shell_scripts_and_user_interfaces

The Shell and User Interfaces


  • The shell (and shell scripts) can display simple user interfaces and dialogs for applications that demand them.
  • zenity and yad 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 --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\!"

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.

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
}


cs370/cs_370_-_shell_scripts_and_user_interfaces.txt · Last modified: 2023/06/22 22:01 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki