Introduction à la programmation Bash
Alex Scriven
Data Scientist
Si vous avez utilisé des fonctions en R ou en Python, vous connaissez déjà ces avantages :
Voyons la syntaxe d'une fonction :
Une fonction Bash a la syntaxe suivante :
function_name () {
#function_code
return #something
}
Vous pouvez aussi créer une fonction ainsi :
function function_name {
#function_code
return #something
}
Principales différences :
function pour indiquer le début de la définitionAppeler une fonction Bash consiste simplement à écrire son nom :
function print_hello () { echo "Hello world!" }print_hello # ici, on appelle la fonction
Hello world!
Écrivons une fonction pour convertir Fahrenheit en Celsius, comme dans une leçon précédente, en utilisant une variable statique.
temp_f=30function convert_temp () {temp_c=$(echo "scale=2; ($temp_f - 32) * 5 / 9" | bc)echo $temp_c }convert_temp # appeler la fonction
-1.11
Introduction à la programmation Bash