🔀 What is switch case?

In shell scripting, switch case is written as case.
It is used to match a value against multiple patterns.


🔹 Syntax

case variable in
    pattern1)
        commands ;;
    pattern2)
        commands ;;
    *)
        default commands ;;
esac

Example 3: Check character type

#!/bin/bash

read ch

case $ch in
[a-z])
echo “Lowercase letter”
;;
[A-Z])
echo “Uppercase letter”
;;
[0-9])
echo “Digit”
;;
*)
echo “Special character”
;;
esac

✅ Example 4: File type check

!/bin/bash

read file

case $file in
*.sh)
echo “Shell script file”
;;
*.txt)
echo “Text file”
;;
*)
echo “Unknown file type”
;;
esac

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *