2024-09-24 22:39:42 +00:00
|
|
|
#!/usr/bin/env bellos
|
2024-10-21 07:14:41 +00:00
|
|
|
# File: control_structures_with_seq.bellos
|
2024-09-24 22:39:42 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
# 1. Simple echo and variable assignment
|
2024-10-16 02:47:29 +00:00
|
|
|
echo "Demonstrating if-else statements:"
|
2024-10-03 04:14:47 +00:00
|
|
|
x=10
|
2024-10-21 07:14:41 +00:00
|
|
|
|
|
|
|
# 2. If-else statement
|
|
|
|
if [ $x -gt 5 ]
|
|
|
|
then
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "x is greater than 5"
|
2024-10-21 07:14:41 +00:00
|
|
|
else
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "x is not greater than 5"
|
2024-10-21 07:14:41 +00:00
|
|
|
fi
|
2024-09-24 22:39:42 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
# 3. Nested if-else
|
2024-10-16 02:47:29 +00:00
|
|
|
echo "Demonstrating nested if-else:"
|
2024-10-03 04:14:47 +00:00
|
|
|
y=20
|
2024-10-21 07:14:41 +00:00
|
|
|
if [ $x -gt 5 ]
|
|
|
|
then
|
|
|
|
if [ $y -gt 15 ]
|
|
|
|
then
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "x is greater than 5 and y is greater than 15"
|
2024-10-21 07:14:41 +00:00
|
|
|
else
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "x is greater than 5 but y is not greater than 15"
|
2024-10-21 07:14:41 +00:00
|
|
|
fi
|
|
|
|
else
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "x is not greater than 5"
|
2024-10-21 07:14:41 +00:00
|
|
|
fi
|
2024-10-03 04:14:47 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
# 4. While loop
|
2024-10-16 02:47:29 +00:00
|
|
|
echo "Demonstrating while loop:"
|
2024-10-03 04:14:47 +00:00
|
|
|
counter=0
|
2024-10-21 07:14:41 +00:00
|
|
|
while [ $counter -lt 5 ]
|
|
|
|
do
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "Counter: $counter"
|
|
|
|
counter=$((counter + 1))
|
2024-10-21 07:14:41 +00:00
|
|
|
done
|
2024-10-03 04:14:47 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
# 5. For loop
|
2024-10-16 02:47:29 +00:00
|
|
|
echo "Demonstrating for loop:"
|
2024-10-21 07:14:41 +00:00
|
|
|
for i in 1 2 3 4 5
|
|
|
|
do
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "Iteration: $i"
|
2024-10-21 07:14:41 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# 6. For loop with seq command
|
|
|
|
echo "Demonstrating for loop with seq command:"
|
|
|
|
for i in $(seq 1 5)
|
|
|
|
do
|
|
|
|
echo "Number from seq: $i"
|
|
|
|
done
|
|
|
|
|
|
|
|
# 7. Using seq with different arguments
|
|
|
|
echo "Demonstrating seq with different arguments:"
|
|
|
|
echo "seq 3 (implicit start at 1, increment by 1):"
|
|
|
|
for i in $(seq 3)
|
|
|
|
do
|
|
|
|
echo "Value: $i"
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "seq 2 5 (start at 2, increment by 1):"
|
|
|
|
for i in $(seq 2 5)
|
|
|
|
do
|
|
|
|
echo "Value: $i"
|
|
|
|
done
|
2024-09-24 22:39:42 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
echo "seq 0 2 10 (start at 0, increment by 2):"
|
|
|
|
for i in $(seq 0 2 10)
|
|
|
|
do
|
|
|
|
echo "Value: $i"
|
|
|
|
done
|
2024-10-03 04:14:47 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
# 8. Case statement
|
2024-10-16 02:47:29 +00:00
|
|
|
echo "Demonstrating case statement:"
|
2024-10-03 04:14:47 +00:00
|
|
|
fruit="apple"
|
2024-10-21 07:14:41 +00:00
|
|
|
case $fruit in
|
2024-10-03 04:14:47 +00:00
|
|
|
"apple")
|
|
|
|
echo "It's an apple"
|
|
|
|
;;
|
|
|
|
"banana")
|
|
|
|
echo "It's a banana"
|
|
|
|
;;
|
|
|
|
"orange")
|
|
|
|
echo "It's an orange"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown fruit"
|
|
|
|
;;
|
2024-10-21 07:14:41 +00:00
|
|
|
esac
|
2024-10-16 02:47:29 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
# 9. Using seq in arithmetic operations
|
|
|
|
echo "Using seq in arithmetic operations:"
|
|
|
|
sum=0
|
|
|
|
for i in $(seq 1 5)
|
|
|
|
do
|
|
|
|
sum=$((sum + i))
|
|
|
|
echo "Running sum: $sum"
|
|
|
|
done
|
|
|
|
echo "Final sum of numbers 1 to 5: $sum"
|
2024-10-16 02:47:29 +00:00
|
|
|
|
2024-10-21 07:14:41 +00:00
|
|
|
echo "Control structures and seq demonstration completed."
|