Shell-Get User Confirmation

Preface

During the scripting you’ll see that in some cases its need to have user confirmation, but you’d like to be sure that someone has really confirmed your request.

Even I’m aware approach described in this chapter can be easily bypassed with methods like using “expect” language or an already existing library providing same approach in another language.I assume that this is still more as nothing.

Function that will ask User for confirmation

This function will ask user on CLI for confirmation with asking him to paste every time an specific string
return 0 - OK
return 1 - ERROR

Get_USR_Confirmation() {
#
# This function will ask user on CLI for confirmation with asking him to paste every time an specific string
# return 0  - OK
# return 1  - ERROR
#
  random_number="" ;								# Variable used for storing random number
  user_variable="" ;								# Variable to store string pasted by user
  return_code="" ;								# Return value

  random_number=$RANDOM ;							# Get Random number

  echo "To Confirm - type this number ( and press enter) $random_number" ;	# Ask User to type the random number
  read user_variable ;								# Read what has user pasted

  if [[ ${random_number} -eq ${user_variable} ]] ; then				# Check if user has pasted correct number
    echo "OK - Its correct input from user" ;					# If he has pasted correct number print passage
    return_code=0 ;								# Set return code to 0
  else
    echo "ERROR - Its not correct input from user" ;				# If the answer was not correct print error message
    return_code=1 ;								# Set return code to 1
  fi
										# Handover the return code
  return $return_code
}
export -f Get_USR_Confirmation ;						# Make available this function in script/shell (session)

Example

$ {                                                                              # BEGIN Definition of function in shell
Get_USR_Confirmation() {
#
# This function will ask user on CLI for confirmation with asking him to paste every time an specific string
# return 0  - OK
# return 1  - ERROR
#
   random_number="" ;                                                            # Variable used for storing random number
   user_variable="" ;                                                            # Variable to store string pasted by user
   return_code="" ;                                                              # Return value

   random_number=$RANDOM ;                                                       # Get Random number

   echo "To Confirm - type this number ( and press enter) $random_number" ;      # Ask User to type the random number
   read user_variable ;                                                          # Read what has user pasted

   if [[ ${random_number} -eq ${user_variable} ]] ; then                         # Check if user has pasted correct number
     echo "OK - Its correct input from user" ;                                   # If he has pasted correct number print passage
     return_code=0 ;                                                             # Set return code to 0
   else
     echo "ERROR - Its not correct input from user" ;                            # If the answer was not correct print error message
     return_code=1 ;                                                             # Set return code to 1
   fi
                                                                                 # Handover the return code
   return $return_code> }
export -f Get_USR_Confirmation ;                                                 # Make available this function in script/shell (session)
}                                                                                # END Definition of function in shell

#################################################################################

$ Get_USR_Confirmation ; echo $?                                                 # Calling of the function, and printing exit code
To Confirm - type this number ( and press enter) 9642                            # Function request to put the unmber to get confirmation
9642                                                                             # User pasted input to confirm as its requested
OK - Its correct input from user                                                 # Message text to confirm the correct input
0                                                                                # Return code of fucntion

#################################################################################

$ Get_USR_Confirmation ; echo $?                                                 # Calling of the function, and printing exit code
To Confirm - type this number ( and press enter) 23924                           # Function request to put the unmber to get confirmation
999999                                                                           # User pasted input to confirm as its requested
ERROR - Its not correct input from user                                          # Message text to prevent error output
1                                                                                # Return code of function
Navigation
Print/export
QR Code
QR Code wiki:programming_languages:shell:shell-get_user_confirmation (generated for current page)