Um valor return
não é necessário em uma função. Normalmente, um return
seria usado em um script para um valor de saída a ser retornado. Os valores de saída normalmente são como 1
ou 0
, em que muitos scripts podem usá-lo para um 0
como bem-sucedido e um 1
como não bem-sucedido.
#!/bin/bash
#The following function returns a value of 0 or 1
function if_running(){
ps -ef | grep -w "$1" | grep -v grep > /dev/null
if [[ $? == 0 ]]; then
return 0
else
return 1
fi
}
#Read in name of a running process
read -p "Enter a name of a process: "
#Send REPLY to function
if_running $REPLY
#Check return value and echo appropriately
if [[ $? == 0 ]]; then
echo "Return value is $?"
echo "$REPLY is running..."
else
echo "Return value is $?"
echo "$REPLY is not running..."
fi
Exemplos:
~$ ./ps_test.bsh
Enter a name of a process: ls
Return value is 1
ls is not running...
~$ ./ps_test.bsh
Enter a name of a process: bash
Return value is 0
bash is running...
E essa resposta que escrevi há pouco não tem valores de retorno, mas ainda fornece um link de saída
#!/bin/bash
function area(){
circ=$(echo "3.14 * $1^2" | bc)
}
#Read in radius
read -p "Enter a radius: "
#Send REPLY to function
area $REPLY
#Print output
echo "Area of a circle is $circ"
Exemplo:
terrance@terrance-ubuntu:~$ ./circ.bsh
Enter a radius: 6
Area of a circle is 113.04
Espero que isso ajude!