1

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.

2
  • 2
    Remember to quote your variables! Commented Mar 31, 2022 at 20:00
  • 1
    Btw: Please paste your script at shellcheck.net and try to implement the recommendations made there. Commented Mar 31, 2022 at 21:06

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.