I need to make program in Linux that will find Pictures in folder. Like JPEG, PNG, BMP. And I need to make it robbust to invalid input. Like when I write LoL instead of png it will write error.Here is my code yet:
#!/bin/bash
echo "Input what you are searching for :"
read subor
echo "-----------------------"
if [ $subor != "png" ];
then
echo
echo "Nebol zadaný vstup pre PNG"
echo
else
Path=$ cd /home/zps/Pictures/
command=$ find -type f -exec file --mime-type {} \; | awk '{if ($NF == "image/png") print $0}' | wc -l
fi
if [ $subor != "jpeg" ];
then
echo
echo "Nebol zadaný vstup pre JPEG"
echo
else
Path=$ cd /home/zps/Pictures/
command=$ find -type f -exec file --mime-type {} \; | awk '{if ($NF == "image/jpeg") print $0}' | wc -l
fi
if [ $subor != "bmp" ];
then
echo
echo "Nebol zadaný vstup pre BMP"
echo
else
Path=$ cd /home/zps/Pictures/
command=$ find -type f -exec file --mime-type {} \; | awk '{if ($NF == "image/bmp") print $0}' | wc -l
fi
echo "-----------------------"
I tried something from internet but I just couldnt do it. I tried to make some code, and it works, I just cant figure it out how to make error when user input someting else that I want. I just need JPEG, PNG and BMP.
CodePudding user response:
Use a case
statement to match all the allowed types. Then you can use a *)
case to catch the invalid input.
case "$subor" in
png) ... ;;
jpeg) ... ;;
bmp) ... ;;
*) echo "Please enter png, jpeg, or bmp" ;;
esac