33 / 55 · 19 Linux & Command Line · Bash Scripting for QA← prev⊞ allnext →☰ Read as one page
5.3Control Flow
Conditionals
# If/else
if [ "$STATUS" -eq 200 ]; then
echo "PASS: Service is healthy"
elif [ "$STATUS" -eq 503 ]; then
echo "WARNING: Service unavailable"
else
echo "FAIL: Unexpected status $STATUS"
fi
# String comparison
if [ "$ENVIRONMENT" = "production" ]; then
echo "Running production smoke tests"
fi
# File checks
if [ -f "test-config.json" ]; then
echo "Config file exists"
fi
if [ -d "test-results" ]; then
echo "Results directory exists"
fi
# Logical operators
if [ "$STATUS" -eq 200 ] && [ "$RESPONSE_TIME" -lt 5 ]; then
echo "Fast and healthy"
fi
Common Test Operators
| Operator | Meaning | Example |
|---|---|---|
-eq |
Equal (numeric) | [ "$a" -eq 200 ] |
-ne |
Not equal (numeric) | [ "$a" -ne 0 ] |
-lt |
Less than | [ "$a" -lt 100 ] |
-gt |
Greater than | [ "$a" -gt 0 ] |
= |
Equal (string) | [ "$a" = "hello" ] |
!= |
Not equal (string) | [ "$a" != "" ] |
-f |
File exists | [ -f "config.json" ] |
-d |
Directory exists | [ -d "results/" ] |
-z |
String is empty | [ -z "$VAR" ] |
-n |
String is not empty | [ -n "$VAR" ] |
Loops
# For loop with a list
for browser in chromium firefox webkit; do
echo "Running tests in $browser"
npx playwright test --project="$browser"
done
# For loop with a range
for i in $(seq 1 5); do
echo "Test run $i of 5"
done
# While loop
ATTEMPTS=0
MAX_ATTEMPTS=10
while [ "$ATTEMPTS" -lt "$MAX_ATTEMPTS" ]; do
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/health")
if [ "$STATUS" -eq 200 ]; then
echo "Service is up after $ATTEMPTS attempts"
break
fi
ATTEMPTS=$((ATTEMPTS + 1))
sleep 5
done