In: Computer Science
In bash
Write a script which receives a list of parameters. If no parameters are received, output an error message. Otherwise, iterate through the list and, using a case statement, test each item in the list to see if it starts with a capital letter, lower case letter, digit or other. Count each type and output at the end the total for each category (capital, lower case, digit, other).
CODE:
# display error if no parameter passed and exit
if [ $# -eq 0 ]
then
echo "Error. No parameter received."
exit;
fi
digit=0
lowercase=0
uppercase=0
anyother=0
# iterate over parameters provided on command line
for item in "$@"
do
# extract first character and use it in case statement
case ${item:0:1} in
# matches if first letter is digit
[0-9])
# increment digit count
digit=$((digit + 1))
;;
# matches if firdt letter is uppercase
[[:upper:]])
# increment uppercase count
uppercase=$((uppercase + 1))
;;
# matches if first letter is lowercase
[[:lower:]])
# increment lowercase count
lowercase=$((lowercase + 1))
;;
# matches if starts with any other letter
*)
# increment any other count
anyother=$((anyother + 1))
;;
esac
done
echo "Digits: $digit"
echo "Lowercase: $lowercase"
echo "Uppercase: $uppercase"
echo "Other: $anyother"
OUTPUT: