Introductie tot Bash-scripting
Alex Scriven
Data Scientist
Als je functies in R of Python hebt gebruikt, ken je deze voordelen:
Laten we de functiesyntaxis ontleden:
Een Bash-functie heeft deze syntax:
function_name () {
#function_code
return #something
}
Je kunt ook zo een functie maken:
function function_name {
#function_code
return #something
}
Belangrijkste verschillen:
function om een functie te startenEen Bash-functie aanroepen is simpelweg de naam schrijven:
function print_hello () { echo "Hello world!" }print_hello # hier roepen we de functie aan
Hello world!
We schrijven een functie om Fahrenheit naar Celsius om te zetten, zoals eerder, met een statische variabele.
temp_f=30function convert_temp () {temp_c=$(echo "scale=2; ($temp_f - 32) * 5 / 9" | bc)echo $temp_c }convert_temp # functie aanroepen
-1.11
Introductie tot Bash-scripting