Nov 26, 2024
for VARIABLE in LISTfor (( ; ; ))while EXPRuntil EXPRfor VARIABLE in LISTfor X in 1 2 3
do
... body of the loop ...
done
for COLOUR in red green blue
do
... body of the loop ...
done
$@.*.c.for SOURCEFILE in *.c
do
echo "=== Compiling $SOURCEFILE ==="
BINARY="$(echo $SOURCEFILE | cut -d. -f1)"
gcc SOURCEFILE -o $BINARY
done
for Loop: for (( ; ; ))(( )) and do/done for body.for (( i=0; i<=10; i++ ))
do
echo $i
done
while EXPRtest command [[ ]].while [[ "$A" == "$B" ]]
do
... body of the loop ...
done
until EXPRuntil [[ "$X" -gt "$Y" ]]
do
... body of the loop ...
done
tput command uses terminal codes for actions like setting text color.tput setaf _n_ and tput setab _n_ set foreground and background colors.#!/usr/bin/bash
for ((C=0; C<16; C++))
do
tput setaf $C
echo "This is colour $C"
done
tput sgr0
#!/usr/bin/bash
MAX=100
MAX_TRIES=7
GAMES=0
WINS=0
clear
tput setaf 5
while [[ "$PLAY" == "Y" || "$PLAY" == "y" || "$PLAY" == "YES" ||
"$PLAY" == "Yes" || "$PLAY" == "yes" ]]
do
SECRET=$(( RANDOM % MAX + 1 ))
GUESS=0
TRIES=0
((GAMES++))
until [[ $GUESS -eq $SECRET || $TRIES -ge $MAX_TRIES ]]
do
tput setaf 15
read -p "Enter your guess (#$((++TRIES))): " GUESS
if [[ $GUESS -gt $SECRET ]]
then
tput setaf 1
echo "Too high!"
elif [[ $GUESS -lt $SECRET ]]
then
tput setaf 1
echo "Too low!"
else
tput setaf 10
echo "You got it in $((TRIES)) tries!"
((WINS++))
fi
echo
done
if [[ ! $GUESS -eq $SECRET ]]
then
tput setaf 11
echo "You lose after $TRIES attempts. The number was $SECRET."
fi
tput setaf 15
read -p "Do you want to play again (Y/N)? " PLAY
echo
done
tput setaf 5
echo "You executed $GAMES missions and succeeded $WINS times ($((WINS*100/GAMES))% success)."
echo
tput sgr0