Foil 08 - Utility subroutine to ask for user input (2/2)

 1 until [ "$input_value" != "" ]
 2   do
 3     echon "$prompt_string {${default}}: "  #Uses sub to print w/o NL
 4     read response_1
 5     if [ "$response_1" ]
 6       then
 7         input_value="$response_1"
 8       else
 9         input_value="$default_value"
10       fi
11     if [ "$input_value" = "" ]
12       then
13         echo '^G'       # Error message highlight and beep
14         echo A value must be supplied...
15       fi
16   done
17 
18 } # End of input function
19 
20 #---------------------------------------------------------------------
21 input "Enter the number (0-31) of the tape drive" "$tape_address"
22 tape_address="$input_value"                                        #08

This is the second half of a subroutine to ask the user a question and ask for a response. The question is output along with an optional default value used if the carriage return is pressed. The cursor is left on the same line and an input is read. If there is no default, it loops until the user supplies a value.

  • Line 1 loops until the user has supplied a response.
  • Line 3 uses another subroutine to output the prompt without a carriage return. This was done because some older systems used different syntax on the echo statement. The subroutine determines which syntax to use.
  • Line 4 reads the user's response and assigns it to a variable.
  • Lines 5-10 use the user's input or the default value.
  • Lines 11-15 prompt the user if there was no input and no default.
  • Lines 21-22 show how this subroutine could be used.

    Previous   Next   Index