Both crabboy's and grail's code convert the contents of
yno to lowercase before it is tested. This makes the glob patterns in the case statement easier.
crabboy's technique pipes the content through the external
tr command, while grail's uses bash's built-in
parameter substitution (case manipulation was included in version4).
I personally wouldn't bother worrying about matching multiple patterns, and just simply test the first character of the string:
Code:
case ${yno,,} in
y*) echo "$PWD" ;;
n*) echo "$exit" ; exit 1 ;;
*) echo "“$exit" ;;
esac
$PWD (capitalized) is a shell built-in variable, by the way.
$pwd is not, but
pwd is a shell command that prints the same info.
Another option is simply to tell read to only accept a single character of input, with the
-n option. You can also use the
-p option to supply the prompt, instead of echoing it separately.
Code:
read -p 'May I see some of your files? [y/n]: ' -n 1 yno
echo
case ${yno,,} in
y) echo "$PWD" ;;
n) echo "$exit" ; exit 1 ;;
*) echo "$exit" ;;
esac
PS:
$(..) is highly recommended over `..`