# touch teste.sh
# nano teste.sh
# chmod u+x teste.sh
# ./teste.sh
# ITERATIONS: FOR
#!/bin/bash
for n in 1 2 3 4 10 20
do
echo $((n * 2))
done
# ITERATIONS: WHILE
#!/bin/bash
n=1
while [ $n -le 100 ]
do
echo $(($n * 2))
n=$(($n+1))
done
# ITERATIONS: UNTIL
#!/bin/bash
if [ $# -ne 1 ]
then exit 1
fi
until who | cut -d' ' -f1 | grep "^$1$" > "/dev/null"
do
sleep 3
# programa para por 3 segundos
echo "user $1 offline!"
done
echo "user $1 online!"
exit 0
# o programa terminou sem problemas nenhuns se tiver um 0, se tiver um numero estamos a sinalizar um erro, o valor do exit é uma variavel para erro
# ITERATIONS: FUNCTIONS
#!/bin/bash
function mult2numbers() {
local minhaConstante = "const" # constante scoped
local readonly name="mult2numbers" # function scoped variable
# o $# é uma variavel onde está a quantidade de parametros passados para a funcão
# não é preciso definir os parametros na funcao
if [ $# -ne 2 ]
then echo "error in $name"
exit 1
fi
echo $1 '*' $2 '=' $(($1 * $2))
}
previous=0
for n in 1 2 3 4 5 6 7 8 9 10; do
mult2numbers $previous $n
previous=$n
done
– break: immediately leave a cycle (for, while, until)
– continue: go immediately to the next cycle iteration
– exit: immediately terminate a script
– clear: clear the screen
– export: create a persistent environment variable
– arithmetic operations: $(($var1 op