Introduktion till Bash-skriptning
Alex Scriven
Data Scientist
Om du har använt funktioner i R eller Python känner du igen dessa fördelar:
Låt oss gå igenom funktionens syntax:
En Bash-funktion har följande syntax:
function_name () {
#function_code
return #something
}
Du kan också skapa en funktion så här:
function function_name {
#function_code
return #something
}
De viktigaste skillnaderna:
function för att inleda funktionsdefinitionenAtt anropa en Bash-funktion görs enkelt genom att skriva dess namn:
function print_hello () { echo "Hello world!" }print_hello # here we call the function
Hello world!
Vi skriver en funktion för att konvertera Fahrenheit till Celsius, precis som i en tidigare lektion, med hjälp av en statisk variabel.
temp_f=30function convert_temp () {temp_c=$(echo "scale=2; ($temp_f - 32) * 5 / 9" | bc)echo $temp_c }convert_temp # call the function
-1.11
Introduktion till Bash-skriptning