scripting menu.docx

Upload: vincent

Post on 04-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 scripting menu.docx

    1/21

    Click on our Sponsors to help Support SunWorld

    Shell programming and simple

    menus - part 2

    Getting more mileage out of your menu

    August 1997

    AbstractThis month we build on the basic menu system explained in last month's column, showing youhow to execute different menu files through one "menu driver." We take you through the code,step by step and, in the process, pass on a few tips on using the shell as a programming language.(3,200 words)

    nlast month's column we started a simple menu system,as useful utility thatillustrated several shell programming concepts. Now let's take that a step further.

    In the next example, the menu system is broken into two parts: the shell script thatexecutes the menu, and a separate "data" file that contains the menu prompts and

    commands to be executed.

    Listing 2 looks very similar to the first menu in last month's Listing 1 (see sidebar)except that it is missing all of the menu prompts and commands to execute! Whereare they?

    Listing 3 is an example of a menu file to be processed by shellmenu. This file contains themissing information. You have to study both listings to see how these work together. Bothlistings include line numbers to simplify the explanations.

    If you compare last months Listing 1 to Listing 2 and Listing 3 from this month, you will see that

    these two listings are almost identical, but the prompts and function definitions have been splitout of Listing 2 and placed in Listing 3.

    The key points to look at in Listing 2 are "TEST THE COMMAND LINE" at lines 68 through80, "TEST THE MENU FILE" at lines 81 through 100 and "READ THE MENU FILE" at lines132 through 138.

    Be sure that shellmenuhas been given execute privileges with the command:

    Mail this

    article to

    a friend

    http://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.htmlhttp://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.htmlhttp://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.html#sidebar1http://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.html#sidebar1http://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.html#sidebar1http://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/swol-mailfriend.cgi?/swol-08-1997/swol-08-unix101.html+Shell_programming_and_simple_menus_-_part_2+Mo_Budlong+tcolhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/search.html/http://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.html#sidebar1http://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.html
  • 8/14/2019 scripting menu.docx

    2/21

    chmod a+x shellmenu

    To start this menu using the file named mf.mnushown in Listing 3 as the menu, type the

    shellmenucommand followed by the name of the file to use as the menu.

    shellmenumf.mnu

    This causes the value mf.mnuto be assigned to a variable, $1, inside the shell script. Thevariables $0, $1, $2, $3 and so on exist in all shell scripts and contain any values that were listedon the command line that was used to start the script. $0 contains the name of the shell script

    itself, shellmenu. $1 contains the first word on the command line after shellmenu, in this case

    mf.mnu. The $2 variable would contain the next word or argument on the command line after

    mf.mnuif there were any. The shell also creates a variable named $#. This variable contains thenumber of arguments that were on the command line after the command (in this case,

    shellmenu).

    For the command: shellmenumf.mnuthe $# variable should contain the value 1 for the 1argument on the command line.

    In "TEST THE COMMAND LINE" at line 75 through 79, the value of $# is tested, and if it isnot 1 then a usage message is displayed and the script exits. The syntax for the test is

    if [ ! $# = 1 ]then

    usageexit

    fi

    Carefully copy the spacing used in the command there are spaces around the left bracket and theright bracket. The "!" means NOT in this test. The test reads "if the number of arguments is not 1

    then execute a usage()function and exit the script."

    The usage()function is defined at lines 10 through 20 and attempts to describe the correct usage

    for the shellmenuscript.

    In "TEST THE MENU FILE", at line 87, a new variable, $MENU, is created and is assigned thevalue of $1. Another test is done to determine if the file exists and is readable. The test:

    if [ -r $MENU ]

    uses the name of the file in $MENU, mf.mnu,and then tests two conditions. The -r tests that thefile exists and that it is readable. If so it returns "true" logically. We want the opposite test, so aNOT (!) is added in:

    if [ ! -r $MENU ]

    Again notice spaces around the left and right brackets.

  • 8/14/2019 scripting menu.docx

    3/21

    This tests if the file mf.mnuexists and is readable, provides a message for the user, and displaysthe usage information and exits.

    Finally the trick as to how the this menu system works appears in "READ THE MENU FILE" atline 87. Note the period in the command:

    . $MENU

    The period is a special character in a shell script that basically says read the following named file(or the file named in the variable) on this line as if it were part of this current script. It acts verymuch like a "include" or "copy" directive. As the file is included, the commands in it areexecuted.

    Advertisements

    Listing 2: Sample shell script menu driver

    1. #!/bin/sh2. # shellmenu3. # Simple shell script menu driver under UNIX4. # Main logic starts at MAIN LOGIC5.6. #---------------------------------------7. # FUNCTION DEFINITIONS8. #---------------------------------------

    9.10. # A function for usage info11. usage () {12. echo "Usage: shellmenu menu_file"13. echo " A menu file must contain 12 variables"14. echo " amenu through l menu"15. echo " defining the prompts for a menu"16. echo " and the definitions for 12"17. echo " functions apick() through lpick()"18. echo " that define the commands to be"

  • 8/14/2019 scripting menu.docx

    4/21

    19. echo " executed for each of the 12 picks"20. }21. # Define a function for invalid menu picks22. # The function loads an error message into a variable23. badchoice ()24. {25. MSG="Invalid Selection ... Please Try Again" ;26. }27.28. #---------------------------------------29. # DISPLAY FUNCTION DEFINITION30. #---------------------------------------31. # This function displays the menu.32. # The routine clears the screen, echoes33. # the logo and menu prompts34. # and some additional messages.35. # Note that this definition does36. # not cause the function to37. # be executed yet, it just defines38. # it ready to be executed.

    39.40. displaymenu () {41. # clear the screen42. clear43. echo `date`44. echo45. echo "\t\t\t" $LOGO46. echo47. echo "\t\tPlease Select:"48. echo49. echo "\t\t\t" $amenu50. echo "\t\t\t" $bmenu51. echo "\t\t\t" $cmenu52. echo "\t\t\t" $dmenu53. echo "\t\t\t" $emenu54. echo "\t\t\t" $fmenu55. echo "\t\t\t" $gmenu56. echo "\t\t\t" $hmenu57. echo "\t\t\t" $imenu58. echo "\t\t\t" $jmenu59. echo "\t\t\t" $kmenu60. echo "\t\t\t" $lmenu61. echo "\t\t\tx. Exit"62. echo63. echo $MSG64. echo65. echo Select by pressing the letter and then ENTER ;

    66. }67.68. #---------------------------------------69. # TEST THE COMMAND LINE70. #---------------------------------------71. # There should only be one argument72. # on the command line, which73. # should be the name of a menu file74.75. if [ ! $# = 1 ]

  • 8/14/2019 scripting menu.docx

    5/21

    76. then77. usage78. exit79. fi80.81. #---------------------------------------82. # TEST THE MENU FILE83. #---------------------------------------84. # Assign the value of the first argument85. # on the command line,86.87. MENU=$188.89. # Test that the file exists and is readable.90. # Abort if it is not91.92. if [ ! -r $MENU ]93. then94. echo "Can't locate or read " $MENU95. echo "Press ENTER to acknowledge."

    96. read $answer97. usage98. exit99. fi100.101. #---------------------------------------102. # MAIN LOGIC103. #---------------------------------------104. # Clear out the error message105. MSG=106.107. #---------------------------------------108. # MAIN LOOP109. #---------------------------------------110. # We're here because we have a file111. # that can be read, so OK to112. # continue with the main loop of the menu.113.114. # Repeat the menu over and over115. # Steps are:116. # 1. Display the menu117. # 2. 'read' a line of input from the keyboard118. # 3. Clear the error message119. # 4. Check the answer for a or A or b or B, etc. and dispatch120. # to the appropriate program or function or exit121. # 5. If the entry was invalid call the122. # badchoice() function

    123. # to initialize MSG to an error message124. # 6. This error message is used when setting125. # up the menu for a menu pick that is126. # valid but has no command127. # associated with it.128.129. while true130. do131.132. #---------------------------------------

  • 8/14/2019 scripting menu.docx

    6/21

    133. # READ THE MENU FILE134. # define the menu by reading in the file135. # named on the command line136. #---------------------------------------137. . $MENU138.139. #---------------------------------------140. # DISPLAY THE MENU141. # and read the user pick142. #---------------------------------------143. displaymenu144. read answer145. MSG=146. # dispatch to the users selection147. case $answer in148. a|A) apick;;149. b|B) bpick;;150. c|C) cpick;;151. d|D) dpick;;152. e|E) epick;;

    153. f|F) fpick;;154. g|G) gpick;;155. h|H) hpick;;156. i|I) ipick;;157. j|J) jpick;;158. k|K) kpick;;159. l|L) lpick;;160. x|X) break;;161. *) badchoice;;162. esac163. done164.

    Listing 3: mf.mnu-- a file of menu prompts and picks

    1. # mf.mnu a simple script menu for shellmenu2. # This file is read directly into shellmenu3. # using the dot (.) prefix as in4. # shellmenu this_file5. #6. # causing the contents7. # of this file to be read as part of the shell script8.9. # The logo for this menu will be displayed10. # at the top of the screen11. LOGO="More Functions"12.13. # Here are the list of menu prompts and the14. # commands that go with them

    15. # In the first list, enter the menu prompt16. # as it should appear17. # on the menu for each of the letters A - L18. # In the second list, replace badchoice19. # with any commands to run20. # when that letter is pressed.21.22. #---------------------------------------23. # MENU PROMPTS

  • 8/14/2019 scripting menu.docx

    7/21

    24. #---------------------------------------25. amenu="a. Job Scheduling" ;26. bmenu="b. Set Standard Defaults " ;27. cmenu="c. Display Directory Listing " ;28. dmenu="d Other Functions" ;29. emenu=" " ;30. fmenu=" " ;31. gmenu=" " ;32. hmenu=" " ;33. imenu=" " ;34. jmenu=" " ;35. kmenu=" " ;36. lmenu=" " ;37.38. #---------------------------------------39. # MENU ACTIONS AND COMMANDS40. #---------------------------------------41. apick () { shmnu ; }42. bpick () { defmnt ; }43. cpick ()

    44. {45. ls -l| more ; echo Press Enter ; read DUMMY ;46. }47. dpick () { shellmenu sf.mnu ; }48. epick () { badchoice ; }49. fpick () { badchoice ; }50. gpick () { badchoice ; }51. hpick () { badchoice ; }52. ipick () { badchoice ; }53. jpick () { badchoice ; }54. kpick () { badchoice ; }55. lpick () { badchoice ; }56.

    Putting it all togetherListing 4 is a logical representation of combination of shellmenuincluding the . $MENUcommand. Once you look at the combination, there is little difference between the originalListing 1 last month and Listing 4

    Listing 4: The logical combination of shellmenuand mf.mnu

    1. #!/bin/sh2. # shellmenu3. # Simple shell script menu driver under UNIX4. # Main logic starts at MAIN LOGIC

    5.6. #---------------------------------------7. # FUNCTION DEFINITIONS8. #---------------------------------------9.10. # A function for usage info11. usage () {12. echo "Usage: shellmenu menu_file"13. echo " A menu file must contain 12 variables"14. echo " amenu through l menu"

  • 8/14/2019 scripting menu.docx

    8/21

    15. echo " defining the prompts for a menu"16. echo " and the definitions for 12"17. echo " functions apick() through lpick()"18. echo " that define the commands to be"19. echo " executed for each of the 12 picks"20. }21. # Define a function for invalid menu picks22. # The function loads an error message into a variable23. badchoice ()24. {25. MSG="Invalid Selection ... Please Try Again" ;26. }27.28. #---------------------------------------29. # DISPLAY FUNCTION DEFINITION30. #---------------------------------------31. # This function displays the menu.32. # The routine clears the screen, echoes33. # the logo and menu prompts34. # and some additional messages.

    35. # Note that this definition does36. # not cause the function to37. # be executed yet. It just defines38. # it as ready to be executed.39.40. displaymenu () {41. # clear the screen42. clear43. echo `date`44. echo45. echo "\t\t\t" $LOGO46. echo47. echo "\t\tPlease Select:"48. echo49. echo "\t\t\t" $amenu50. echo "\t\t\t" $bmenu51. echo "\t\t\t" $cmenu52. echo "\t\t\t" $dmenu53. echo "\t\t\t" $emenu54. echo "\t\t\t" $fmenu55. echo "\t\t\t" $gmenu56. echo "\t\t\t" $hmenu57. echo "\t\t\t" $imenu58. echo "\t\t\t" $jmenu59. echo "\t\t\t" $kmenu60. echo "\t\t\t" $lmenu61. echo "\t\t\tx. Exit"

    62. echo63. echo $MSG64. echo65. echo Select by pressing the letter and then ENTER ;66. }67.68. #---------------------------------------69. # TEST THE COMMAND LINE70. #---------------------------------------71. # There should be only one argument

  • 8/14/2019 scripting menu.docx

    9/21

    72. # on the command line, which73. # should be the name of a menu file74.75. if [ ! $# = 1 ]76. then77. usage78. exit79. fi80.81. #---------------------------------------82. # TEST THE MENU FILE83. #---------------------------------------84. # Assign the value of the first argument85. # on the command line,86.87. MENU=$188.89. # Test that the file exists and is readable.90. # Abort if it is not91.

    92. if [ ! -r $MENU ]93. then94. echo "Can't locate or read " $MENU95. echo "Press ENTER to acknowledge."96. read $answer97. usage98. exit99. fi100.101. #---------------------------------------102. # MAIN LOGIC103. #---------------------------------------104. # Clear out the error message105. MSG=106.107. #---------------------------------------108. # MAIN LOOP109. #---------------------------------------110. # We're here because we have a file111. # that can be read, so it's OK to112. # continue with the main loop of the menu.113.114. # Repeat the menu over and over115. # Steps are:116. # 1. Display the menu117. # 2. 'read' a line of input from the keyboard118. # 3. Clear the error message

    119. # 4. Check the answer for a or A or b or B etc. and dispatch120. # to the appropriate program or function or exit121. # 5. If the entry was invalid call the122. # badchoice() function123. # to initialize MSG to an error message124. # 6. This error message is used when setting125. # up the menu for a menu pick that is126. # valid but has no command127. # associated with it.128.

  • 8/14/2019 scripting menu.docx

    10/21

    129. while true130. do131.132. #---------------------------------------133. # READ THE MENU FILE134. # define the menu by reading in the file135. # named on the command line136. #---------------------------------------137. . $MENU138. # >>139. # mf.mnu a simple script menu for shellmenu140. # This file is read directly into shellmenu141. # using the dot (.) prefix as in142. # shellmenu this_file143. #144. # causing the contents145. # of this file to be read as part of the shell script146.147. # The logo for this menu will be displayed148. # at the top of the screen

    149. LOGO="More Functions"150.151. # Here are the list of menu prompts and the152. # commands that go with them153. # In the first list, enter the menu prompt154. # as it should appear155. # on the menu for each of the letters A - L156. # In the second list, replace badchoice157. # with any commands to run158. # when that letter is pressed.159.160. #---------------------------------------161. # MENU PROMPTS162. #---------------------------------------163. amenu="a. Job Scheduling" ;164. bmenu="b. Set Standard Defaults " ;165. cmenu="c. Display Directory Listing " ;166. dmenu="d Other Functions" ;167. emenu=" " ;168. fmenu=" " ;169. gmenu=" " ;170. hmenu=" " ;171. imenu=" " ;172. jmenu=" " ;173. kmenu=" " ;174. lmenu=" " ;175.

    176. #---------------------------------------177. # MENU ACTIONS AND COMMANDS178. #---------------------------------------179. apick () { shmnu ; }180. bpick () { defmnt ; }181. cpick ()182. {183. ls -l| more ; echo Press Enter ; read DUMMY ;184. }185. dpick () { shellmenu sf.mnu ; }

  • 8/14/2019 scripting menu.docx

    11/21

    186. epick () { badchoice ; }187. fpick () { badchoice ; }188. gpick () { badchoice ; }189. hpick () { badchoice ; }190. ipick () { badchoice ; }191. jpick () { badchoice ; }192. kpick () { badchoice ; }193. lpick () { badchoice ; }194.195. # >>196.197. #---------------------------------------198. # DISPLAY THE MENU199. # and read the user pick200. #---------------------------------------201. displaymenu202. read answer203. MSG=204. # dispatch to the users selection205. case $answer in

    206. a|A) apick;;207. b|B) bpick;;208. c|C) cpick;;209. d|D) dpick;;210. e|E) epick;;211. f|F) fpick;;212. g|G) gpick;;213. h|H) hpick;;214. i|I) ipick;;215. j|J) jpick;;216. k|K) kpick;;217. l|L) lpick;;218. x|X) break;;219. *) badchoice;;220. esac221. done222.

    The advantage of this second method over the menu version created in last month's issue is that

    you create only one menu driver program, shellmenu. The menus themselves can be separatedout in individual files.

    In listing 4, pick "d" executes the command shellmenusf.mnu. The script executes the same

    shellmenuprogram, but this time it is processing a different menu file.

    Listing 5 is an example of what sf.mnumight look like. Listing 5 is stripped of all commentsand contains only the menu prompts and commands. The shellmenuscript takes care of thelogic of displaying the menu and dispatching to the various menu actions.

    Listing 5: sf.mnuis run by selecting "d" from mf.mnu

    LOGO="Some More Functions"amenu="a. Edit This Menu" ;

  • 8/14/2019 scripting menu.docx

    12/21

    bmenu="b. A Directory Listing Wide" ;cmenu="c. Directory Listing Long" ;dmenu=" " ;emenu=" " ;fmenu=" " ;gmenu=" " ;hmenu=" " ;imenu=" " ;jmenu=" " ;kmenu=" " ;lmenu=" " ;apick () { vi sf.mnu ; }bpick (){ls -C|more ;echo Press ENTER ;read DUMMY ;}cpick () {ls -l| more ;

    echo Press Enter ;read DUMMY ;}dpick () { badchoice ; }epick () { badchoice ; }fpick () { badchoice ; }gpick () { badchoice ; }hpick () { badchoice ; }ipick () { badchoice ; }jpick () { badchoice ; }kpick () { badchoice ; }lpick () { badchoice ; }

    This should give you some simple examples of menus as well as useful tips on using the shell asa programming language.

    Go

    Mo Budlong is president of King Computer Services, Inc. and has been involved in Unixdevelopment on Sun and other platforms for over 15 years. King Computer Services, Inc.

    specializes in UNIX and Client/Server consulting and training and currently publishes theCOBOL Just In Time Course, a crash COBOL course to train staff for the Year 2000 problem.Reach Mo [email protected].

    What did you think of this article?

    -Very worth reading -Too long -Too technical

    http://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+authorhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+authorhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+authorhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+author
  • 8/14/2019 scripting menu.docx

    13/21

    -Worth reading

    -Not worth reading

    -Just right

    -Too short

    -Just right

    -Not technical enough

    Comments:

    Name:

    Email:

    Company

    Name:

    Send data

    If you have technical problems with this magazine, [email protected]

    URL: http://www.sunworld.com/swol-08-1997/swol-08-unix101.html

    Last modified:

    Listing 1: Sample shell script menu

    1. #!/bin/sh2. # sample.mnu3. # A simple script menu under Unix4. # Main logic starts at MAIN LOGIC5. # The logo will be displayed at the top of the screen6. LOGO="Sample Menu"7.8. #------------------------------------------------------

    9. # MENU PROMPTS10. #------------------------------------------------------11. # A list of menu prompts to be displayed for the user.12. # The list can be modified.13. # In this first list, enter the menu prompt as it should appear14. # on the screen for each of the letters A - L. In this example15. # menu pick variables emenu through lmenu are blank as there16. # are no menu selections for keys E through L.17.18. amenu="a. Job Scheduling" ;

    http://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmasterhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmasterhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmasterhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmaster
  • 8/14/2019 scripting menu.docx

    14/21

    19. bmenu="b. Set Standard Defaults " ;20. cmenu="c. Display Directory Listing " ;21. dmenu="d Payroll Menu " ;22. emenu=" " ;23. fmenu=" " ;24. gmenu=" " ;25. hmenu=" " ;26. imenu=" " ;27. jmenu=" " ;28. kmenu=" " ;29. lmenu=" " ;30.31. #------------------------------------------------------32. # MENU FUNCTION DEFINITIONS33. #------------------------------------------------------34.35. # Define a function for invalid menu picks36. # The function loads an error message into a variable37. badchoice () { MSG="Invalid Selection ... Please Try Again" ; }38.

    39. # For each prompt displayed above, there is a list of40. # commands to execute in response to the user picking the41. # associated letter.42. # They are defined as functions43. # apick () through lpick () where44. # apick () corresponds to the menu45. # prompt amenu which is selected46. # selected by pressing a or A.47. # bpick () corresponds to the menu48. # prompt bmenu which is selected by49. # pressing b or B and so on.50. # Any menu item that is not51. # assigned a set of commands, is52. # assigned53. # the function badchoice () as a default for that pick.54. # If the user55. # selects a menu key that is assigned56. # to badchoice (). This function57. # causes an error message to be58. # displayed on the screen.59. # To add items to this second60. # list, replace badchoice ()61. # with the commands to run when62. # that letter is pressed.63. # The following steps simply define64. # the functions, but do not cause65. # any shell program steps to be executed.

    66.67. apick () { sched ; }68. bpick () { defmnt ; }69. cpick () { ls -l| more ; echo Press Enter ; read DUMMY ; }70. dpick () { payroll.mnu ; }71. epick () { badchoice ; }72. fpick () { badchoice ; }73. gpick () { badchoice ; }74. hpick () { badchoice ; }75. ipick () { badchoice ; }

  • 8/14/2019 scripting menu.docx

    15/21

    76. jpick () { badchoice ; }77. kpick () { badchoice ; }78. lpick () { badchoice ; }79.80. #------------------------------------------------------81. # DISPLAY FUNCTION DEFINITION82. #------------------------------------------------------83. # This function displays the menu.84. # The routine clears the screen, echoes85. # the logo and menu prompts86. # and some additional messages.87. # Note that this definition does88. # not cause the function to89. # be executed yet, it just defines90. # it ready to be executed.91.92. themenu () {93. # clear the screen94. clear95. echo `date`

    96. echo97. echo "\t\t\t" $LOGO98. echo99. echo "\t\tPlease Select:"100. echo101. echo "\t\t\t" $amenu102. echo "\t\t\t" $bmenu103. echo "\t\t\t" $cmenu104. echo "\t\t\t" $dmenu105. echo "\t\t\t" $emenu106. echo "\t\t\t" $fmenu107. echo "\t\t\t" $gmenu108. echo "\t\t\t" $hmenu109. echo "\t\t\t" $imenu110. echo "\t\t\t" $jmenu111. echo "\t\t\t" $kmenu112. echo "\t\t\t" $lmenu113. echo "\t\t\tx. Exit"114. echo115. echo $MSG116. echo117. echo Select by pressing the letter and then ENTER ;118. }119.120. #------------------------------------------------------121. # MAIN LOGIC122. #------------------------------------------------------

    123. # Every thing up to this point has been to define124. # variables or functions.125. # The program actually starts running here.126.127. # Clear out the error message variable128. MSG=129.130. # Repeat the menu over and over131. # Steps are:132. # 1. Display the menu

  • 8/14/2019 scripting menu.docx

    16/21

    133. # 2. 'read' a line of input from the key board134. # 3. Clear the error message135. # 4. Check the answer for a or A or b or B etc. and dispatch136. # to the appropriate program or function or exit137. # 5. If the entry was invalid call the badchoice () function138. # to initialize MSG to an error message139. # 6. This error message is used when setting up the menu140. # for a menu pick that is valid but has no command141. # associated with it.142.143. while true144. do145. # 1. display the menu146. themenu147.148. # 2. read a line of input from the keyboard149. read answer150.151. # 3. Clear any error message152. MSG=

    153.154. # 4. Execute one of the defined functions based on the155. # letter entered by the user.156.157. # 5. If the choice was E through L, the pre-defined158. # function for that pick will execute badchoice ()159. # which loads an error message into MSG160.161. case $answer in162. a|A) apick;;163. b|B) bpick;;164. c|C) cpick;;165. d|D) dpick;;166. e|E) epick;;167. f|F) fpick;;168. g|G) gpick;;169. h|H) hpick;;170. i|I) ipick;;171. j|J) jpick;;172. k|K) kpick;;173. l|L) lpick;;174.175. # If the user selects =91x=92 to exit then break out176. # of this loop177. x|X) break;;178.179. # 6. If the entry was invalid call the badchoice function

    180. # to initialize MSG to an error message181. *) badchoice;;182.183. esac184.185. # Do it again until the user enters =91x=92.186. done187.

  • 8/14/2019 scripting menu.docx

    17/21

    Go

    Click on our Sponsors to help Support SunWorld

    Resources

    Last month's article for reviewhttp://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.html

    A tutorial on how to write shell scriptshttp://www.ocean.odu.edu/ug/shell_help.html

    Examples of Bourne shell (sh) scripts and programminghttp://www.ict.pwr.wroc.pl/doc/Unixhelp/scrpt_scrpt2.x.html

    Discussions of various Bourne shell (sh) issueshttp://garfield.ir.ucf.edu/manual/aix/bourne.html

    Discussion of the Korn Shell (ksh) and programming tipshttp://www.ict.pwr.wroc.pl/doc/Unixhelp/shell_oview2.4.html

    On a lighter note, a collection of "Stupid Shell Tricks" should elicit the odd chucklehttp://www.cs.virginia.edu/~bah6f/funnies/unixcmds.html

    Full listing of other Unix 101 columns in SunWorldhttp://www.sun.com/sunworldonline/common/swol-backissues-columns.html#unix101

    What did you think of this article?

    -Very worth reading

    -Worth reading

    -Not worth reading

    -Too long

    -Just right

    -Too short

    -Too technical

    -Just right

    -Not technical enough

    Comments:

    Name:

    Email:

    http://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.htmlhttp://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.htmlhttp://www.ocean.odu.edu/ug/shell_help.htmlhttp://www.ocean.odu.edu/ug/shell_help.htmlhttp://www.ict.pwr.wroc.pl/doc/Unixhelp/scrpt_scrpt2.x.htmlhttp://www.ict.pwr.wroc.pl/doc/Unixhelp/scrpt_scrpt2.x.htmlhttp://garfield.ir.ucf.edu/manual/aix/bourne.htmlhttp://garfield.ir.ucf.edu/manual/aix/bourne.htmlhttp://www.ict.pwr.wroc.pl/doc/Unixhelp/shell_oview2.4.htmlhttp://www.ict.pwr.wroc.pl/doc/Unixhelp/shell_oview2.4.htmlhttp://www.cs.virginia.edu/~bah6f/funnies/unixcmds.htmlhttp://www.cs.virginia.edu/~bah6f/funnies/unixcmds.htmlhttp://www.sun.com/sunworldonline/common/swol-backissues-columns.html#unix101http://www.sun.com/sunworldonline/common/swol-backissues-columns.html#unix101http://www.sun.com/sunworldonline/common/swol-backissues-columns.html#unix101http://www.cs.virginia.edu/~bah6f/funnies/unixcmds.htmlhttp://www.ict.pwr.wroc.pl/doc/Unixhelp/shell_oview2.4.htmlhttp://garfield.ir.ucf.edu/manual/aix/bourne.htmlhttp://www.ict.pwr.wroc.pl/doc/Unixhelp/scrpt_scrpt2.x.htmlhttp://www.ocean.odu.edu/ug/shell_help.htmlhttp://www.sun.com/sunworldonline/swol-07-1997/swol-07-unix101.html
  • 8/14/2019 scripting menu.docx

    18/21

    Company

    Name:

    Send data

    If you have technical problems with this magazine, [email protected]

    URL: http://www.sunworld.com/swol-08-1997/swol-08-unix101.html

    Last modified:

    Listing 1: Sample shell script menu

    1. #!/bin/sh2. # sample.mnu3. # A simple script menu under Unix4. # Main logic starts at MAIN LOGIC5. # The logo will be displayed at the top of the screen6. LOGO="Sample Menu"7.8. #------------------------------------------------------9. # MENU PROMPTS10. #------------------------------------------------------11. # A list of menu prompts to be displayed for the user.12. # The list can be modified.13. # In this first list, enter the menu prompt as it should appear14. # on the screen for each of the letters A - L. In this example15. # menu pick variables emenu through lmenu are blank as there16. # are no menu selections for keys E through L.17.18. amenu="a. Job Scheduling" ;19. bmenu="b. Set Standard Defaults " ;20. cmenu="c. Display Directory Listing " ;21. dmenu="d Payroll Menu " ;22. emenu=" " ;23. fmenu=" " ;

    24. gmenu=" " ;25. hmenu=" " ;26. imenu=" " ;27. jmenu=" " ;28. kmenu=" " ;29. lmenu=" " ;30.31. #------------------------------------------------------32. # MENU FUNCTION DEFINITIONS33. #------------------------------------------------------

    http://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmasterhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmasterhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmasterhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-copyright97.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-adgallery.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-database.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+feedbackhttp://sunsite.uakom.sk/search.htmlhttp://sunsite.uakom.sk/sunworldonline/common/swol-subscribe.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/index.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+webmaster
  • 8/14/2019 scripting menu.docx

    19/21

    34.35. # Define a function for invalid menu picks36. # The function loads an error message into a variable37. badchoice () { MSG="Invalid Selection ... Please Try Again" ; }38.39. # For each prompt displayed above, there is a list of40. # commands to execute in response to the user picking the41. # associated letter.42. # They are defined as functions43. # apick () through lpick () where44. # apick () corresponds to the menu45. # prompt amenu which is selected46. # selected by pressing a or A.47. # bpick () corresponds to the menu48. # prompt bmenu which is selected by49. # pressing b or B and so on.50. # Any menu item that is not51. # assigned a set of commands, is52. # assigned53. # the function badchoice () as a default for that pick.

    54. # If the user55. # selects a menu key that is assigned56. # to badchoice (). This function57. # causes an error message to be58. # displayed on the screen.59. # To add items to this second60. # list, replace badchoice ()61. # with the commands to run when62. # that letter is pressed.63. # The following steps simply define64. # the functions, but do not cause65. # any shell program steps to be executed.66.67. apick () { sched ; }68. bpick () { defmnt ; }69. cpick () { ls -l| more ; echo Press Enter ; read DUMMY ; }70. dpick () { payroll.mnu ; }71. epick () { badchoice ; }72. fpick () { badchoice ; }73. gpick () { badchoice ; }74. hpick () { badchoice ; }75. ipick () { badchoice ; }76. jpick () { badchoice ; }77. kpick () { badchoice ; }78. lpick () { badchoice ; }79.80. #------------------------------------------------------

    81. # DISPLAY FUNCTION DEFINITION82. #------------------------------------------------------83. # This function displays the menu.84. # The routine clears the screen, echoes85. # the logo and menu prompts86. # and some additional messages.87. # Note that this definition does88. # not cause the function to89. # be executed yet, it just defines90. # it ready to be executed.

  • 8/14/2019 scripting menu.docx

    20/21

    91.92. themenu () {93. # clear the screen94. clear95. echo `date`96. echo97. echo "\t\t\t" $LOGO98. echo99. echo "\t\tPlease Select:"100. echo101. echo "\t\t\t" $amenu102. echo "\t\t\t" $bmenu103. echo "\t\t\t" $cmenu104. echo "\t\t\t" $dmenu105. echo "\t\t\t" $emenu106. echo "\t\t\t" $fmenu107. echo "\t\t\t" $gmenu108. echo "\t\t\t" $hmenu109. echo "\t\t\t" $imenu110. echo "\t\t\t" $jmenu

    111. echo "\t\t\t" $kmenu112. echo "\t\t\t" $lmenu113. echo "\t\t\tx. Exit"114. echo115. echo $MSG116. echo117. echo Select by pressing the letter and then ENTER ;118. }119.120. #------------------------------------------------------121. # MAIN LOGIC122. #------------------------------------------------------123. # Every thing up to this point has been to define124. # variables or functions.125. # The program actually starts running here.126.127. # Clear out the error message variable128. MSG=129.130. # Repeat the menu over and over131. # Steps are:132. # 1. Display the menu133. # 2. 'read' a line of input from the key board134. # 3. Clear the error message135. # 4. Check the answer for a or A or b or B etc. and dispatch136. # to the appropriate program or function or exit137. # 5. If the entry was invalid call the badchoice () function

    138. # to initialize MSG to an error message139. # 6. This error message is used when setting up the menu140. # for a menu pick that is valid but has no command141. # associated with it.142.143. while true144. do145. # 1. display the menu146. themenu147.

  • 8/14/2019 scripting menu.docx

    21/21

    148. # 2. read a line of input from the keyboard149. read answer150.151. # 3. Clear any error message152. MSG=153.154. # 4. Execute one of the defined functions based on the155. # letter entered by the user.156.157. # 5. If the choice was E through L, the pre-defined158. # function for that pick will execute badchoice ()159. # which loads an error message into MSG160.161. case $answer in162. a|A) apick;;163. b|B) bpick;;164. c|C) cpick;;165. d|D) dpick;;166. e|E) epick;;167. f|F) fpick;;

    168. g|G) gpick;;169. h|H) hpick;;170. i|I) ipick;;171. j|J) jpick;;172. k|K) kpick;;173. l|L) lpick;;174.175. # If the user selects =91x=92 to exit then break out176. # of this loop177. x|X) break;;178.179. # 6. If the entry was invalid call the badchoice function180. # to initialize MSG to an error message181. *) badchoice;;182.183. esac184.185. # Do it again until the user enters =91x=92.186. done187.

    About the authorMo Budlong is president of King Computer Services, Inc. and has been involved in Unixdevelopment on Sun and other platforms for over 15 years. King Computer Services, Inc.specializes in UNIX and Client/Server consulting and training and currently publishes the

    COBOL Just In Time Course, a crash COBOL course to train staff for the Year 2000 problem.Reach Mo [email protected].

    http://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+authorhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+authorhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+authorhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/swol-08-1997/swol-08-unix101.htmlhttp://sunsite.uakom.sk/sunworldonline/cgi-bin/[email protected]+/swol-08-1997/swol-08-unix101.html+author