In: Computer Science
Please find the requested script below. Also including the screenshot of sample output .
Please provide your feedback
Thanks and Happy learning!
#!/bin/bash
#Above line sets the environment for the script to bash
echo "************** Welcome to the animal sound finder **************"
echo
echo "Enter the name of your favorite animal : "
read animal
if [ ! -z "$animal" -a "$animal" != " " ]
then
#Check whether the file exists or not
if [ -f ./noises.txt ]
then
grepResult=`grep -i "$animal" ./noises.txt | head -1`
if [ ! -z "$grepResult" -a "$grepResult" != " " ]
then
animalSound=`echo $grepResult | cut -d: -f 2`
else
echo "Please enter the sound that $animal makes: "
read animalSound;
echo "$animal:$animalSound" >> ./noises.txt
fi
echo "$animal makes $animalSound sound."
else
echo "Please enter the sound that $animal makes: "
read animalSound;
echo "$animal:$animalSound" >> ./noises.txt
echo "$animal makes $animalSound sound."
fi
else
echo "Sorry, You have entered an invalid animal name!"
fi