Lab 6.2 - Conditions and Loops
Objectives
- Use if/else
- Use case
- Use for and while
- Create conditional scripts
Exercise 1: Conditions
# Script with if
cat > check.sh << 'EOF'
#!/bin/bash
if [ -f "$1" ]; then
echo "File exists"
else
echo "File does not exist"
fi
EOF
chmod +x check.sh
./check.sh fichier.txt
Exercise 2: Loops
# For loop
for i in {1..5}; do
echo "Number: $i"
done
# While loop
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
Lab created on: December 2024 - Tested on Ubuntu 24.04