Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
#!/bin/bash # ---- if / elif / else ---- FILE="/etc/hosts" if [[ -f "$FILE" ]]; then echo "$FILE exists and is a regular file" elif [[ -d "$FILE" ]]; then echo "$FILE is a directory" else echo "$FILE does not exist" fi # String comparison USER_INPUT="yes" if [[ "$USER_INPUT" == "yes" ]]; then echo "User agreed" fi # Numeric comparison (-eq -ne -lt -le -gt -ge) COUNT=5 if (( COUNT > 3 )); then echo "Count is greater than 3" fi # ---- for loop ---- # Over a list for fruit in apple banana cherry; do echo "Fruit: $fruit" done # Over a range for i in {1..5}; do echo "Iteration $i" done # Over files for f in /etc/*.conf; do echo "Config file: $f" done # C-style for (( i=0; i<3; i++ )); do echo "Index $i" done # ---- while loop ---- counter=1 while (( counter <= 3 )); do echo "Counter: $counter" (( counter++ )) done # Read file line by line while IFS= read -r line; do echo "Line: $line" done < /etc/hosts # ---- case statement ---- DAY=$(date +%A) case "$DAY" in Monday|Tuesday|Wednesday|Thursday|Friday) echo "Weekday" ;; Saturday|Sunday) echo "Weekend" ;; *) echo "Unknown" ;; esac
Result
Open