2

is there any way to check or test the input from a 'read' against a for 'loop'?

I don't want the case statement to execute there any of the input is incorrect. As of right now it will loop through, outputting the * ) branch multiple times while still executing the correct inputs.

Example:

echo "Select Your Options (Seperated by spaces): 1 2 3"
read SELECTION
     # Test SELECTION here? Then exit if fail? IE, if a 4 is entered.

for INPUT in $SELECTION; do
    case $INPUT in
                   1 ) echo "you selection option 1"
                       ;;
                   2 ) echo "you selection option 2"
                       ;;
                   3 ) echo "you selection option 3"
                       ;;
                   * ) echo "Check your selection. Anything outside the following will make this fail:  1 2 3 "
                       RETVAL=1
                       exit          
                       ;; 
    esac
done
  • 2
    Depending on your exact use case, you may be interested in the select bash construct; it takes care of presenting numeric options for you. – dhag Dec 17 '15 at 19:51
  • @dhag - a ksh construct. – mikeserv Dec 17 '15 at 20:15
  • @mikeserv bash borrowed (I assume) the select statement a very long time ago; it's present in bash 2.0 at least. – chepner Dec 17 '15 at 22:52
  • I'm not sure if a select construct will be good for this script. The script if looking for specific files in a directory and moving them to other locations. IE: If I select option 1) the script moves out all files in a directory that start with S_ . – Motorahead Dec 18 '15 at 20:01
1

Like this?

rpt=0
until [ 5 -le "$((rpt+=1))" ] && unset rpt
      printf '\nSelect your Options:(1|2|3): ' >&2
      read  SELECTION &&
      case $SELECTION in 
      (*[!123\ ]*|*[123][123]*|'') ! :
      esac
do    : "${rpt?too many loops!}"
      echo >&2 \
      "Check your selection. Anything outside the following will make this fail:  1 2 3 "
done

for INPUT in $SELECTION
do  case $INPUT  in
                 (1) echo "you selection option 1"
                       ;;
                 (2) echo "you selection option 2"
                       ;;
                 (3) echo "you selection option 3"
                       ;;
    esac
done
1
#!/usr/bin/env bash

echo "Select Your Options (Seperated by spaces): 1 2 3"
read selection

#exit(1) unless every component of $selection is one of 1, 2, or 3

set -f #to supress glob expansion
for input in $selection; do
  case "$input" in 1);; 2);; 3);; *) exit 1;; esac
done

for input in $selection; do
  case "$input" in
    1 ) echo "you selection option 1"
      ;;
    2 ) echo "you selection option 2"
      ;;
    3 ) echo "you selection option 3"
      ;;
  esac
done
  • I see what your doing this. They may cleaner option to use but this does work. I'll try it out. – Motorahead Dec 18 '15 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.