Write the program using an if command and then rewrite it using a case command. This program can be useful when reading yes/no responses from the terminal.
Write a program called isyes that returns an exit status of 0 if its argument is yes, and 1 otherwise. For purposes of this exercise, consider y, yes, Yes, YES, and Y all to be valid yes arguments:
$ isyes yes
$ echo $?
0
$ isyes no
$ echo $?
1
The first version with the if conditional is:
if [ -n "$( echo $1 | grep -E '(^[Yy][Ee]{0,1}[Ss]{0,1}$)')" ] ; then
exit 0
else
exit 1
fi
And the second version with the case statement is:
case "$1" in
[Yy][Ee]{0,1}[Ss]{0,1} ) exit 0 ;;
* ) exit 1 ;;
esac
You might also like to view...
Conditional execution means:
What will be an ideal response?
Compressed ________ images are used to produce small file sizes for web pages
Fill in the blank(s) with correct word
Another term for a search engine is a search ________
A) browser B) blog C) filter D) provider
In the equation: R = A + B * 2 – (C + 4) / A, the operands are:
a. +, *, -, / b. A, B, C c. R d. =