NEW: Added if-usage.sh showing various use of the if statement

This commit is contained in:
xpk
2025-10-30 09:50:13 +08:00
parent 4abba069f9
commit c0e9f7b8c1
Executable
+31
View File
@@ -0,0 +1,31 @@
#!/bin/bash
if ls -d /tmp; then
echo "Command succeeded: file exists."
fi
if [ -f /etc/hosts ]; then
echo "/etc/hosts exists"
fi
num=100
if [ "$num" -gt 10 ]; then
echo "The number is greater than 10."
fi
floatnum=20.34
result=$(echo "$floatnum > 10.5" | bc)
if [ "$result" -eq 1 ]; then
echo "The number is greater than 10.5"
fi
word="hello"
if [ "$word" = "hello" ]; then
echo "You typed hello."
fi
sentense="I want to say hello to the world"
regex="say.*hello"
if [[ "$sentense" =~ $regex ]]; then
echo "Substring found."
fi