Funciones en Bash
Autor: Alexander Arias
Fecha: 22/05/2022
#!/bin/bash echo "---------------------------------" echo "Funciones" echo "---------------------------------" echo "function to add two numbers" echo "---------------------------------" #function to add two numbers add(){ x=$1 y=$2 echo -e "Number entered by u are: $x and $y" echo "sum of $1 and $2 is `expr $x + $y` " } # main script echo "enter first number" read first echo "enter second number" read sec #calling function add $first $sec echo "---------------------------------" echo "Another Function to add two numbers" echo "---------------------------------" suma(){ a=$1 b=$2 c=$(($a+$b)) echo "la suma de $a + $b es $c" } suma 10 20 echo "---------------------------------" echo "Function to add two numbers and return value" echo "---------------------------------" suma1(){ a=$1 b=$2 c=$(($a+$b)) #echo "la suma de $a + $b es $c" return $c } #Llamar la funcion suma1 10 20 #El valor de la ejecución se guarda en $? #lo guardo en d d=$? echo $d echo "---------------------------------" echo "end of the script"
El eresultado de Ejecución es:
bash main.sh --------------------------------- Funciones --------------------------------- function to add two numbers --------------------------------- enter first number 5 enter second number 6 Number entered by u are: 5 and 6 sum of 5 and 6 is 11 --------------------------------- Another Function to add two numbers --------------------------------- la suma de 10 + 20 es 30 --------------------------------- Function to add two numbers and return value --------------------------------- 30 --------------------------------- end of the script
No hay comentarios:
Publicar un comentario