Novell Home

HowTo: Create a Dialog Console-Based Configuration Helper - Part 1

Novell Cool Solutions: Feature
By Stomfi

Digg This - Slashdot This

Posted: 6 Oct 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

This howto uses dialog to develop a shell-based utility that will display configuration files, display help, and launch the relevant shell based configuration tool for SUSE. The utility can be modified and developed for other distributions.

With SUSE the configuration is usually performed by YaST and SaX. In the main, help is extracted from system documentation.

The configuration programs should only be run as root, so the utility needs to have a test to make sure only root can run it. As the utility can be run only by root, the program needs a link to be installed somewhere in root's path, which often doesn't include /usr/local/bin. This can be problematic when upgrading a system, as the link may be deleted, so an install script is provided. The script can be extended to look for the correct configuration files and programs in a non SUSE distribution. For this reason, these files and programs are identified with variable name holders, which if set out side the program (i.e. by the install script) are not reset inside it.

This is a top down development picture:

This is a view of the folders and help files in the fixit system:

The Fixit menu is in /usr/local/FIXIT/bin and has a list of items which can be selected. This is a shell script:

#!/bin/sh

#fixit

#Menu to choose fixit dialog box

#Must run as root

if [ $USER != "root" ]

then

echo "You must be root to run this program"

exit

fi

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

: ${DIALOG=dialog}

#delete old tempfiles

rm -f /tmp/fixit*

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

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

$DIALOG --clear --title "FIXIT MENU" \

--menu "Use the UP/DOWN arrow keys, the first \n\

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

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

Choose an Item:" 20 51 4 \

"Xconf" "X Windows Configuration" \

"Netcard" "Network Card Configuration" \

"Mouse" "Mouse Configuration" \

"Runlevel" "Run Level" \

"Services" "Run Level Services" \

"Printer" "Printer Configuration" 2> $tempfile

retval=$?

choice=`cat $tempfile`

case $retval in

0)

case $choice in

"Xconf") /usr/local/FIXIT/xconf/bin/conview;;

"Netcard") /usr/local/FIXIT/netcard/bin/conview;;

"Mouse") /usr/local/FIXIT/mouse/bin/conview;;

"Runlevel") /usr/local/FIXIT/runlevel/bin/conview;;

"Services") /usr/local/FIXIT/services/bin/conview;;

"Printer") /usr/local/FIXIT/printer/bin/conview;;

*) exit;;

esac;;

1)

exit;;

255)

exit;;

esac

#delete old tempfiles

rm -f /tmp/fixit*

Before "dialog" can be used, you will need 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, but it will work as is.

Use the "chmod +x ./fixit" shell command to make the fixit shell script executable, and run it as the root user.

This shows fixit running in a GUI console. The window is a lot smaller than it will be in a non GUI console, where the whole menu can be displayed. Dialog shows all available navigation keys in this picture.

Looking at the script, the case statement controls what will happen when a selection is highlighted with the navigation key and the OK highlighted and activated with the Enter key.

Each of the selection items has its own folder tree containing the subfolders "bin" "share" and "tmp".

The "share" folder contains the "doc" folder which contains the help files as shown for the xconf sub folder above. The contents of the other folders will be obvious from the various scripts.

File names are the same in all these item folders, although the contents reflect the actions that have to be performed for each item selected from the fixit menu.

This is the X windows "conview" script:

#! /bin/sh

#xconf

#list xconfigs

#fixit dir

FIXITC="/usr/local/FIXIT/xconf"

# tmp file

rm -f $FIXITC/tmp/clist.txt

#delete old temp files

rm -f $FIXITC/tmp/ftext*

#set X11 Config file

if [ ${#XCONFG} -lt 1 ]

then

   if [ ! -e /etc/X11/xorg.conf ]

   then

      XCONFG="/etc/X11/XF86Config"

   else

      XCONFG="/etc/X11/xorg.conf"

   fi

fi

#Create a list from Sections

SLIST=`grep "^Section" $XCONFG |awk '{print $2}'|uniq`

#Format list for dialog

#Insert Overview as first item

echo "Overview \"\" off \\" > $FIXITC/tmp/clist.txt

#Only display useful sections

for I in $SLIST

do

   if [ "$I" = "\"ServerFlags\"" -o "$I" = "\"Module\"" -o "$I" = "\"DRI\"" -o "$I" = "\"Extensions\"" ]

   then

      : #Do nothing

   else

      echo "$I \"\" off \\" >> $FIXITC/tmp/clist.txt

   fi

done

cat $FIXITC/share/dial1 $FIXITC/tmp/clist.txt $FIXITC/share/dial2 > $FIXITC/tmp/fixitl

sh $FIXITC/tmp/fixitl

This script makes a list of named sections from the X windows config file and formats it to become valid dialog radiolist items. An "Overview" item is placed at the top of the list. This makes sure that dialog displays a relevant list if no item is selected when the list button is clicked. Two parts of a previously constructed dialog are concatenated with the radio list items into a temporary file which is subsequently executed.

This is the xconf/share/dial1 script:

#! /bin/sh

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

: ${DIALOG=dialog}

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

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

$DIALOG --backtitle "SUSE Fixit System" \

--title "Fixit Selection" \

--ok-label "List" \

--help-button \

--help-label "HELP" \

--extra-button \

--extra-label "Run Config" \

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

Choose item?" 20 61 5 \

This part of the dialog script is the same for every item. The OK button label is set to read "List", which lists the item configuration file. A help button is included and an extra button to run the relevant configuration program. The help button is keyed to the selected item or a general help if none is selected.

This is the second part of the dialog script which is xconf/share/dial2.

2> $tempfile

retval=$?

choice=`cat $tempfile`

case $retval in

   0)

      rm -f $tmpfile

      sh /usr/local/FIXIT/xconf/bin/conf $choice;;

   1)

      rm -f $tmpfile

      sh /usr/local/FIXIT/bin/fixit;;

   2) temptext=/usr/local/FIXIT/xconf/tmp/ftext$$

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

      if [ ${#choice} -gt 0 ]

      then

         HELP=`echo $choice |cut -d" " -f2`

         TEXT="/usr/local/FIXIT/xconf/share/doc/$HELP"

      else

         TEXT="/usr/local/FIXIT/xconf/share/doc/help"

      fi

      test -f $TEXT

      cat $TEXT | expand >> $temptext

      rm -f $tmpfile

      $DIALOG --keep-window --title "HELP MESSAGE" --textbox "$temptext" 22 77

      case $? in

         0) rm -f $temptext

            sh /usr/local/FIXIT/xconf/tmp/fixitl;;

         255) rm -f $temptext

            sh /usr/local/FIXIT/xconf/tmp/fixitl;;

      esac

      ;;

   3) rm -f $tmpfile

      sax2

      sh /usr/local/FIXIT/xconf/tmp/fixitl;;

   255)

      rm -f $tmpfile

      sh /usr/local/FIXIT/bin/fixit;;

esac 

The case part of the script has the following options:

"0" is the list button which runs the "conf" script with the choice as its argument.

"1" is the cancel button which returns to the fixit script.

"2" is the help button which runs a textbox dialog depending on the selection made and returns to this script. Notice how one dialog script can include another.

"3" is the extra button which runs the configuration command and returns to this script.

"255" is the escape key which returns to the fixit script.

This is the dialog window:

This is the conf script which lists the configuration.

#!/bin/sh

#Show text and return to section list

#fixit dir

FIXITC="/usr/local/FIXIT/xconf"

rm -f $FIXITC/tmp/conflist.txt

if [ ${#XCONFG} -lt 1 ]

then

   if [ ! -e /etc/X11/xorg.conf ]

   then

      XCONFG="/etc/X11/XF86Config"

   else

      XCONFG="/etc/X11/xorg.conf"

   fi

fi

#list lines from key to End Section.

#If the key is Overview cat the whole file

if [ $1 != "\"Overview\"" ]

then

   LINEN=`awk -v PAT="$1" '{if($1 ~ "Section" && $2 ~ PAT) print NR}' $XCONFG`

   for LIN in $LINEN

   do

      awk -v LNO=$LIN '{if(NR > LNO){if($1 !~ "EndSection") print;else exit}}' $XCONFG >> $FIXITC/tmp/conflist.txt

   done

else

   cat $XCONFG > $FIXITC/tmp/conflist.txt

fi

#Show the config

sh $FIXITC/bin/textbox

#Return to the config list

sh $FIXITC/tmp/fixitl

This script finds all the lines matching the section selected or the whole file if it is "Overview", saves them in a temporary file and runs the text box script on this file. The textbox script could be included in this file like in a previous script. On return from the textbox this shell returns to the X configuration list.

Here is the textbox script:

#!/bin/sh

# $Id: textbox,v 1.6 2003/08/15 19:40:37 tom Exp $

: ${DIALOG=dialog}

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

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

TEXT="/usr/local/FIXIT/xconf/tmp/conflist.txt"

test -f $TEXT

cat $TEXT | expand >> $tempfile

$DIALOG --clear --title "CONFIG MESSAGE" --textbox "$tempfile" 22 77

case $? in

   0)

      :;;

   255)

      :;;

esac

The textbox looks like this for the monitor selection.

And like this for the "Overview" help.

The second part of this article deals with another complete item where the scripts are different and YaST is launched to change the configuration. I think that you will agree that dialog can make life really pleasant for the MS Windows newbie who gets stuck in the command line.


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

© 2014 Novell