#!/bin/bash
my_string="hello"
if [ -n "$my_string" ]; then
echo "The string is not empty."
else
echo "The string is empty."
fi
#!/bin/bash
string1="hello"
string2="hello"
if [ "$string1" = "$string2" ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
#!/bin/bash
string1="hello"
string2="world"
if [ "$string1" != "$string2" ]; then
echo "The strings are not equal."
else
echo "The strings are equal."
fi
#!/bin/bash
file="/path/to/file"
if [ -e "$file" ]; then
echo "The file exists."
else
echo "The file does not exist."
fi
#!/bin/bash
file="/path/to/file"
if [ -f "$file" ]; then
echo "The file exists and is a regular file."
else
echo "The file does not exist or is not a regular file."
fi
#!/bin/bash
dir="/path/to/directory"
if [ -d "$dir" ]; then
echo "The directory exists."
else
echo "The directory does not exist."
fi
#!/bin/bash
file="/path/to/file"
if [ -r "$file" ]; then
echo "The file is readable."
else
echo "The file is not readable."
fi
#!/bin/bash
file="/path/to/file"
if [ -w "$file" ]; then
echo "The file is writable."
else
echo "The file is not writable."
fi
#!/bin/bash
file="/path/to/file"
if [ -x "$file" ]; then
echo "The file is executable."
else
echo "The file is not executable."
fi
#!/bin/bash
file="/path/to/file"
if [ -s "$file" ]; then
echo "The file is not empty."
else
echo "The file is empty."
fi
#!/bin/bash
num1=5
num2=5
if [ "$num1" -eq "$num2" ]; then
echo "The numbers are equal."
else
echo "The numbers are not equal."
fi
#!/bin/bash
num1=5
num2=10
if [ "$num1" -ne "$num2" ]; then
echo "The numbers are not equal."
else
echo "The numbers are equal."
fi
#!/bin/bash
num1=5
num2=10
if [ "$num1" -lt "$num2" ]; then
echo "$num1 is less than $num2."
else
echo "$num1 is not less than $num2."
fi
#!/bin/bash
num1=5
num2=10
if [ "$num1" -le "$num2" ]; then
echo "$num1 is less than or equal to $num2."
else
echo "$num1 is not less than or equal to $num2."
fi
#!/bin/bash
num1=10
num2=5
if [ "$num1" -gt "$num2" ]; then
echo "$num1 is greater than $num2."
else
echo "$num1 is not greater than $num2."
fi
#!/bin/bash
num1=10
num2=5
if [ "$num1" -ge "$num2" ]; then
echo "$num1 is greater than or equal to $num2."
else
echo "$num1 is not greater than or equal to $num2."
fi
#!/bin/bash
file="/path/to/file"
if [ ! -e "$file" ]; then
echo "The file does not exist."
else
echo "The file exists."
fi
#!/bin/bash
file1="/path/to/file1"
file2="/path/to/file2"
if [ -e "$file1" -a -e "$file2" ]; then
echo "Both files exist."
else
echo "Both files do not exist."
fi
#!/bin/bash
file1="/path/to/file1"
file2="/path/to/file2"
if [ -e "$file1" -o -e "$file2" ]; then
echo "At least one of the files exists."
else
echo "Neither of the files exists."
fi
These examples cover a range of common tests you might perform in shell scripts.