Novell Home

HowTo: Use tput in a shell data entry system - Part 3

Novell Cool Solutions: Feature
By Stomfi

Digg This - Slashdot This

Posted: 15 Sep 2005
 

StomfiLearning to use Linux at Home and Work
Welcome to my ongoing series of HowTo articles designed to help Linux newbies get comfortable with Linux. Before trying any of these HowTos, take a few minutes to study the prerequisites so you can hit the ground running.
--Stomfi
The first two articles in this series showed you how to use the "tput" command to develop a terminal based data entry system.

This article continues with the development by using the "dialog" command as a replacement for "tput" in the same scripts.

The best way to understand how to use "dialog" is to look at the examples which you can find in the source code package.

I have modified some of the examples for use in this development, leaving the heading intact.

At the risk of being boring, I'll show you the development design again, as it is important to keep referring to it to make sure we keep on the right track.

This is the new goods menu screen.

How do you like that? Actually if you have the mouse configured to work in a terminal screen, you can also use it to activate things as well as the ones shown in the above screen.

Before you can use "dialog" in your scripts you have to have a ".dialogrc file in your home folder. Create this with the command:
dialog ?create-rc $HOME/.dialogrc

You can change settings in this file to suit your own preferences.

Here is the new shell script which I've called "goods" without the ".sh" extension. I've used this convention for all the other other scripts, using the same names without the extension.

Since this is a script created by a professional, I've made a few comments to help you figure out what is being done.

#!/bin/sh

# $Id: menubox,v 1.4 2003/08/15 19:40:37 tom Exp $

#Colon does nothing and the rest sets a variable for the dialog program

: ${DIALOG=dialog}

#This line sets a path to tempfile. The $$ is the current shell ID.

tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$

#We learned about trap. This line makes sure the tempfile is deleted

trap "rm -f $tempfile" 0 1 2 5 15

#This is the dialog script. Each line, except the last line, is terminated by backslash, which means

#treat the script as being all on one line.

$DIALOG --clear --title "GOODS SYSTEM" \

   --menu "You can use the UP/DOWN arrow keys, the first \n\

   letter of the choice as a hot key, or the \n\

   number keys 1-2 to choose an option.\n\n\

   Choose a Goods System Item:" 20 51 4 \

   "Inwards" "Inwards Goods"\

   "Outwards" "Outwards Goods" 2> $tempfile

#The standard error is directed to tempfile.

#Here is another new construct. The bash man page states that $? expands to the

#status of the most recently executed foreground pipeline, in this case the dialog.

retval=$?

#tempfile is going to contain the choice from the list of displayed menu options

choice=`cat $tempfile`

#A case and a sub case let us perform the desired choice or exit the program

case $retval in

   0) case "$choice" in

         "Inwards") $HOME/GOODS/bin/inwards;;

         "Outwards") $HOME/GOODS/bin/outwards;;

      esac;;

   1) exit;;

   255) exit;;

esac

exit

The two sub menus are very similar except they exit back to the calling script. Here is one of them:

#!/bin/sh

# $Id: menubox,v 1.4 2003/08/15 19:40:37 tom Exp $

: ${DIALOG=dialog}


tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$

trap "rm -f $tempfile" 0 1 2 5 15


$DIALOG --clear --title "INWARDS GOODS SYSTEM" \

   --menu "You can use the UP/DOWN arrow keys, the first \n\

   letter of the choice as a hot key, or the \n\

   number keys 1-2 to choose an option.\n\n\

   Choose a Goods System Item:" 20 51 4 \

   "Entry" "Enter New Inwards Goods"\

   "Adjust" "Adjust Existing Inwards Goods Entry" 2> $tempfile


retval=$?

choice=`cat $tempfile`

case $retval in

   0) case "$choice" in

         "Entry") $HOME/GOODS/bin/inentry ;;

         "Adjust") $HOME/GOODS/bin/inadjust ;;

      esac ;;

   1) $HOME/GOODS/bin/goods;;

   255) $HOME/GOODS/bin/goods;;

esac 

The entry screens are quite a bit different as they allow the arrow keys to be used to navigate. Here is an entry screen:

The submit button is clicked when the record is complete. This is the script:

#! /bin/sh

# $Id: form1,v 1.6 2004/03/13 16:06:51 tom Exp $

: ${DIALOG=dialog}

#Initialise variables

EDATE=`date +%d/%m/%y`

DOCNUM=""

SUPPCODE=""

CARRIER=""

GOODSCODE=""

GOODSNAME=""

GQUANT=""

GMEAS=""

RECDBY=""

#Sets the top title

backtitle="New Inwards Goods Entry Form"

#Set the return code

returncode=0

#This script uses the word test which is the same as its alias brackets [ ]

while test $returncode != 1 && test $returncode != 250

do

   #Exec means replace this shell with the following command. The following argument

   #concatenates file descriptor 3 (the current file) and file descriptor 1 (the standard output)

   exec 3>&1

   value="`$DIALOG --ok-label "Submit" \

      --backtitle "$backtitle" \

      --form "Enter values in all empty fields" \

      20 50 0 \

      "Date:" 1 1 "$EDATE" 1 10 10 0 \

      "DOCNUM:" 2 1 "$DOCNUM" 2 10 8 0 \

      "SUPPCODE:" 3 1 "$SUPPCODE" 3 10 8 0 \

      "CARRIER:" 4 1 "$CARRIER" 4 10 30 0 \

      "GOODSCODE:" 5 1 "$GOODSCODE" 5 10 8 0 \

      "GOODSNAME:" 6 1 "$GOODSNAME" 6 10 40 0 \

      "GQUANT:" 7 1 "$GQUANT" 7 10 6 0 \

      "GMEAS:" 8 1 "$GMEAS" 8 10 10 0 \

      "RECDBY:" 9 1 "$RECDBY" 9 10 20 0 \

      2>&1 1>&3`"

      returncode=$?

      #This one concatenates the current file with the input

   exec 3>&-

   
   show=`echo "$value" |sed -e 's/^/ /'`

   #Some more dialogs

   case $returncode in

      1) "$DIALOG" \

         --clear \

         --backtitle "$backtitle" \

         --yesno "Really quit?" 10 30

         case $? in

            0) break ;;

            1) returncode=99 ;;

         esac ;;

      0) $DIALOG --title "POST THIS RECORD ENTRY?" \

         --yesno "$show" 15 40

         case $? in

            0) #Check that all fields are filled before writing record, or give an error message

               #Create the record string from $value, deleting the last #

               NRECORD=`echo "$value"|awk 'BEGIN{ORS="#"}{print $0}'|sed -e 's/#$//'`

               #Count the number of fields

               NUMFLDS=`echo "$NRECORD" | awk -F"#" 'END{print NF}'`

               if [ $NUMFLDS -lt 9 ]

               then

                  $DIALOG --title "INPUT ERROR" --clear \

                  --msgbox "You must fill in all the fields.\n\

                  This record will not be saved" 10 41

                  case $? in

                     0) $HOME/GOODS/bin/inwards ;;

                     255) $HOME/GOODS/bin/inwards ;;

                  esac

               else

                  echo $NRECORD >> $HOME/GOODS/data/ingoods.txt
				  
               fi ;;

            1) $HOME/GOODS/bin/inwards ;;

            255) $HOME/GOODS/bin/inwards ;;

         esac;;

      *) $HOME/GOODS/bin/inwards ;;

   esac

done

$HOME/GOODS/bin/inwards

This script includes three different types of dialog scripts, a form, a yesno, and a msgbox. This and the "tput" script should give you a very good idea how to use dialog and the shell to create the script code for the adjustment scripts. You might like to present the user with a list of existing records to choose one to adjust.

This is the sample script for a radio list which lets you choose one from many.

#! /bin/sh

# $Id: radiolist,v 1.7 2003/08/15 19:40:37 tom Exp $

: ${DIALOG=dialog}


tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$

trap "rm -f $tempfile" 0 1 2 5 15


$DIALOG --backtitle "Red Hat Software Linux" \

   --title "RADIOLIST BOX" --clear \

   --radiolist "Hi, this is a radiolist box. You can use this to \n\

   present a list of choices which can be turned on or \n\

   off. If there are more items than can fit on the \n\

   screen, the list will be scrolled. You can use the \n\

   UP/DOWN arrow keys, the first letter of the choice as a \n\

   hot key, or the number keys 1-9 to choose an option. \n\

   Press SPACE to toggle an option on/off. \n\n\

   Which of the following are fruits?" 20 61 5 \

   "Apple" "It's an apple." off \

   "Dog" "No, that's not my dog." ON \

   "Orange" "Yeah, that's juicy." off \

   "Chicken" "Normally not a pet." off \

   "Cat" "No, never put a dog and a cat together!" off \

   "Fish" "Cats like fish." off \

   "Lemon" "You know how it tastes." off 2> $tempfile


retval=$?


choice=`cat $tempfile`

case $retval in

   0) echo "'$choice' chosen.";;

   1) echo "Cancel pressed.";;

   255) echo "ESC pressed.";;

esac

Obviously the "tom" who wrote this and the other examples, works for the opposition. Seems to have a similar sense of humour though.

I shall show you my solution in the next article, where we shall extend the functionality of the goods system to include some other needed features.


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell