Construct Web Test

Web test description

Example Web Test

Web test description

print("Hello World")
If-else statement

If-else with alternative execution

#!/bin/bash
if [ condition ]; then
    echo "Condition is true"
else
    echo "Condition is false"
fi
If-elif-else statement

Multiple conditions with elif

#!/bin/bash
if [ $1 -eq 1 ]; then
    echo "First option"
elif [ $1 -eq 2 ]; then
    echo "Second option"
else
    echo "Other option"
fi
Check if file exists

Check for file existence

#!/bin/bash
if [ -f "filename.txt" ]; then
    echo "File exists"
else
    echo "File does not exist"
fi
Check if directory exists

Check for directory existence

#!/bin/bash
if [ -d "directory_name" ]; then
    echo "Directory exists"
else
    echo "Directory does not exist"
fi
Check if string is not empty

Check if variable has content

#!/bin/bash
if [ -n "$variable" ]; then
    echo "Variable is not empty"
else
    echo "Variable is empty"
fi
Check if string is empty

Check if variable is empty

#!/bin/bash
if [ -z "$variable" ]; then
    echo "Variable is empty"
else
    echo "Variable is not empty"
fi
Numeric comparison

Compare numeric values

#!/bin/bash
if [ $num -gt 10 ]; then
    echo "Number is greater than 10"
elif [ $num -lt 5 ]; then
    echo "Number is less than 5"
else
    echo "Number is between 5 and 10"
fi
If without brackets (command-based)

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

#!/bin/bash
if command; then
    echo "Command succeeded"
fi
If with grep command

If statement using grep command exit status

#!/bin/bash
if grep -q "pattern" file.txt; then
    echo "Pattern found in file"
else
    echo "Pattern not found"
fi
If with test command (alternative to [])

If statement using test command instead of square brackets

#!/bin/bash
if test "$var" = "value"; then
    echo "Variables are equal"
fi
If with arithmetic evaluation

If statement using arithmetic evaluation with double parentheses

#!/bin/bash
if (( $num > 10 )); then
    echo "Number is greater than 10"
fi
If with pattern matching

If statement using pattern matching with double brackets

#!/bin/bash
if [[ $string == *"pattern"* ]]; then
    echo "String contains pattern"
fi
If with regex matching

If statement using regex matching

#!/bin/bash
if [[ $string =~ ^[0-9]+$ ]]; then
    echo "String contains only digits"
fi
If with multiple conditions (AND)

If statement with multiple conditions using AND operator

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

If statement with multiple conditions using OR operator

#!/bin/bash
if [ $role = "admin" ] || [ $role = "moderator" ]; then
    echo "User has elevated privileges"
fi
If with negation

If statement with negation operator

#!/bin/bash
if ! [ -f "file.txt" ]; then
    echo "File does not exist"
fi
If with complex file checks

If statement with multiple file permission checks

#!/bin/bash
if [ -r "file.txt" ] && [ -w "file.txt" ] && [ -x "file.txt" ]; then
    echo "File is readable, writable, and executable"
fi
If with string length check

If statement checking string length using parameter expansion

#!/bin/bash
if [ ${#string} -gt 5 ]; then
    echo "String is longer than 5 characters"
fi
If with array element check

If statement checking array length

#!/bin/bash
array=("apple" "banana" "cherry")
if [ ${#array[@]} -gt 0 ]; then
    echo "Array has ${#array[@]} elements"
fi
If with command substitution

If statement using command substitution

#!/bin/bash
if [ "$(whoami)" = "root" ]; then
    echo "Running as root user"
fi
If with process check

If statement checking if a process is running

#!/bin/bash
if pgrep -f "python.*app.py" > /dev/null; then
    echo "Python application is running"
fi
If with network connectivity check

If statement checking network connectivity

#!/bin/bash
if ping -c 1 google.com > /dev/null 2>&1; then
    echo "Internet connection is available"
fi
If with user input validation

If statement with comprehensive input validation

#!/bin/bash
read -p "Enter your age: " age
if [[ $age =~ ^[0-9]+$ ]] && [ $age -ge 0 ] && [ $age -le 120 ]; then
    echo "Valid age entered: $age"
else
    echo "Invalid age entered"
fi
If with case-insensitive string comparison

If statement with case-insensitive string comparison using parameter expansion

#!/bin/bash
if [[ "${string,,}" == "yes" ]]; then
    echo "User said yes (case-insensitive)"
fi
If with file type checking

If statement with different file type checks

#!/bin/bash
if [ -b /dev/sda ]; then
    echo "It's a block device"
elif [ -c /dev/tty ]; then
    echo "It's a character device"
elif [ -p /tmp/fifo ]; then
    echo "It's a named pipe"
fi
If with time-based conditions

If statement with time-based conditions

#!/bin/bash
current_hour=$(date +%H)
if [ $current_hour -ge 9 ] && [ $current_hour -lt 17 ]; then
    echo "Business hours"
else
    echo "After hours"
fi
If with system resource check

If statement checking system disk usage

#!/bin/bash
disk_usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ $disk_usage -gt 80 ]; then
    echo "Warning: Disk usage is ${disk_usage}%"
fi