If Statements

KSH conditional execution with enhanced features

Basic if statement

KSH if statement with double brackets

#!/bin/ksh
if [[ condition ]]; then
    print "Condition is true"
fi
String comparison

String comparison in KSH

#!/bin/ksh
if [[ $string == "hello" ]]; then
    print "String matches"
fi
Pattern matching

Pattern matching with wildcards

#!/bin/ksh
if [[ $filename == *.txt ]]; then
    print "Text file detected"
fi
KSH if with double brackets and pattern matching

KSH if statement with pattern matching using double brackets

#!/bin/ksh
string="hello world"
if [[ $string == *"world"* ]]; then
    print "String contains 'world'"
fi
KSH if with regex matching

KSH if statement with regex pattern matching

#!/bin/ksh
email="user@example.com"
if [[ $email =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
    print "Valid email address"
else
    print "Invalid email address"
fi
KSH if with arithmetic evaluation

KSH if statement with arithmetic evaluation and logical operators

#!/bin/ksh
integer num=15
if (( num > 10 && num < 20 )); then
    print "Number is between 10 and 20"
fi
KSH if with file operations

KSH if statement with file existence and permission checks

#!/bin/ksh
filename="/tmp/test.txt"
if [[ -f $filename && -r $filename ]]; then
    print "File exists and is readable"
    wc -l $filename
fi
KSH if with string operations

KSH if statement with string length and case operations

#!/bin/ksh
text="Hello World"
if [[ ${#text} -gt 5 ]]; then
    print "String length is greater than 5"
fi

if [[ ${text,,} == "hello world" ]]; then
    print "Case-insensitive match"
fi
KSH if with command substitution

KSH if statement using command substitution

#!/bin/ksh
if [[ "$(whoami)" == "root" ]]; then
    print "Running as root"
else
    print "Running as regular user"
fi
KSH if with array operations

KSH if statement with array length and iteration

#!/bin/ksh
set -A colors red green blue
if [[ ${#colors[@]} -gt 0 ]]; then
    print "Array has ${#colors[@]} elements"
    for color in "${colors[@]}"; do
        print "Color: $color"
    done
fi
KSH if without brackets (command-based)

KSH if statement using command exit status (0 = success, non-zero = failure)

#!/bin/ksh
if command; then
    print "Command succeeded"
fi
KSH if with test command

KSH if statement using test command instead of square brackets

#!/bin/ksh
if test "$var" = "value"; then
    print "Variables are equal"
fi
KSH if with grep command

KSH if statement using grep command exit status

#!/bin/ksh
if grep -q "pattern" file.txt; then
    print "Pattern found in file"
else
    print "Pattern not found"
fi
KSH if with multiple conditions (AND)

KSH if statement with multiple conditions using AND operator

#!/bin/ksh
if [[ $age -ge 18 ]] && [[ $status = "active" ]]; then
    print "User is eligible"
fi
KSH if with multiple conditions (OR)

KSH if statement with multiple conditions using OR operator

#!/bin/ksh
if [[ $role = "admin" ]] || [[ $role = "moderator" ]]; then
    print "User has elevated privileges"
fi
KSH if with negation

KSH if statement with negation operator

#!/bin/ksh
if ! [[ -f "file.txt" ]]; then
    print "File does not exist"
fi