Friday, February 3, 2012

shell script -- select OPTION in OPTIONLIST

it is a usefule statement for bash shell script.

select OPTION in OPTIONLIST
do
echo "You pick $OPTION of $REPLY "
break;
done

the value of your pick will be put into OPTION and option number to REPLY

ex:

#!/usr/bash

OLIST="a b c d e f g h i j k l m n o p q r s t u v w x y z"

select OPTION in $OLIST
do
echo "You pick $OPTION of $REPLY"
break;
done


output:
sh select_t.sh
1) a 4) d 7) g 10) j 13) m 16) p 19) s 22) v 25) y
2) b 5) e 8) h 11) k 14) n 17) q 20) t 23) w 26) z
3) c 6) f 9) i 12) l 15) o 18) r 21) u 24) x
#? 25
You pick y of 25


reference:
http://muffinresearch.co.uk/archives/2007/11/08/using-select-for-multiple-choices-in-shell-scripts/

1 comment: