#!/bin/csh -f # promptloop # ----------------------------------------------------------------------------- # Shell script to prompt the user for one of a set of values, insisting # that a valid value be entered and returning that value as stdout. # ----------------------------------------------------------------------------- # Usage: # - Intended to be called from another script, in a situation where the list # of valid values is known and can be enumerated, to force the user to select # one of the values. Ideal for choices like: # y, n # yes, no # retry, use default, skip # m, t, w, th, f, sa, su # mon, tue, wed, thu, fri, sat, sun # - Typically called via backtick operators, to pipe stdout to a variable. # For example: # set reply = `promptloop "Proceed (y/n)?" y n` # Assumptions: # Effects: # - Prompts the user until a valid value is entered. # - Writes value to stdout. # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ----------------------------------------------------------------------------- if ($#argv == 0 || "$1" == "-h" || "$1" == "--help") then echo Usage: set reply = \`$0:t prompt_string values...\` echo Example: set reply = \`$0:t \"Proceed \(y/n\)\?\" y n\` exit 1 endif while (1 == 1) # Write prompt to /dev/tty, not stdout, so that we can be called within # backticks as shown in Usage above, sending only the answer to stdout. echo -n "$1" > /dev/tty set answer=$< @ i = 2 while ($i <= $#argv) if ("$answer" == "$argv[$i]") then break; break # Break out of two nested loops endif @ i++ end end echo $answer