Case Statements

KSH case statement constructs

KSH case with pattern matching

KSH case statement with file type pattern matching

#!/bin/ksh
filename="document.pdf"
case $filename in
    *.txt|*.log)
        print "Text file"
        ;;
    *.pdf|*.doc)
        print "Document file"
        ;;
    *.jpg|*.png)
        print "Image file"
        ;;
    *)
        print "Unknown file type"
        ;;
esac
KSH case with regex patterns

KSH case statement with character class patterns

#!/bin/ksh
input="123abc"
case $input in
    [0-9]*)
        print "Starts with digit"
        ;;
    [a-zA-Z]*)
        print "Starts with letter"
        ;;
    *)
        print "Other pattern"
        ;;
esac
KSH case with menu system

KSH case statement for menu system

#!/bin/ksh
print "1. Option 1"
print "2. Option 2"
print "3. Exit"
print "Choose: "
read choice

case $choice in
    1)
        print "You chose option 1"
        ;;
    2)
        print "You chose option 2"
        ;;
    3)
        print "Exiting..."
        exit 0
        ;;
    *)
        print "Invalid choice"
        ;;
esac