Home > Power Computing > Bash and Shell Dialog Examples

Bash and Shell Dialog Examples

January 26th, 2010 Jimmy Leave a comment Go to comments

Below are some basic examples of how to create Dialog based Bash scripts for the Linux console. Dialog provides a method of displaying several different types of dialog boxes from shell scripts. The bash dialog example below walks you through a basic “Hello World” shell script using Dialog.

#!/bin/sh
rm /tmp/h.out
while [ 0 ]; do

Create a two option dialog box 11 by 40 characters in size.

dialog --title "Hello World" --menu \
"Select Interface or <Cancel> to exit" \
   11 40 4  \
   "1" "Say Hi!" \
   "2" "Quit" 2>/tmp/h.out

Trap what the user selects into the SEL variable. Values will either be a 1 or a 2 based on our menu above.

SEL="`cat /tmp/h.out`"


If our selection (SEL) is a 1 then show a simple dialog box with a message. Otherwise if it is a 2 then the user selected quit, so ext.

if [ $SEL = "1" ]; then
  echo "Hello, welcome to Bash Dialog programming">/tmp/h.out
  dialog --textbox /tmp/h.out 22 70
fi

if [ $SEL = "2" ]; then
  clear
  exit 0;
fi

done

Notes:
You’ve probably noticed by now that Shell Dialog scripting uses temporary files as a way to store variables. The line: echo “Hello, welcome to Bash Dialog programming”>/tmp/h.out does just what is says. It’s echoing (or outputting) the “Hello, welcome to…” string to a temporary file called h.out in the /tmp folder.

Then the line dialog –textbox /tmp/h.out 22 70 calls the dialog application with a –textbox parameter which is just a simple file viewer.

Screenshots

Digg: DIGG ME
  1. No comments yet.
  1. No trackbacks yet.