Bash programming
Bash staat voor Born Again Shell. Het is de standaard-shell, terminal of command line op Linux, Apple OS X [1], en vermoedelijk op diverse andere Unix-varianten.
In tegenstelling tot de Windows-Command Line, is Bash een uitgebreide interface om een computer naar je hand te zetten, inclusief programmeren, variabelen, arrays en flow control.
Arrays
Bv.:
#1/bin/bash a[1]="Jantje" a[2]="Zag" a[3]="Eens" a[4]="Pruimen" a[5]="Hangen" for i in `seq 1 5`; do echo ${a[$i]} # Gebruik accolades om een array-variabele te evalueren. Anders krijg je [1] als output done
Output:
Jantje Zag Eens Pruimen Hangen
Empty variable detection
https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/
Exit script
Functions
Voorbeeld:
#!/bin/sh subroet () { echo "2" } echo "1" subroet echo "3"
- Je hebt geen GOSUB oid., maar met functies kun je dat redelijk opvangen
- De functie moet vóór de eerste aanroep gedefineerd worden. Anders foutmelding
- Wil je de functie-definities toch uit de weg hebben? Zet ze in een bibliotheek, en roep die aan het begin van het script aan.
Loops
Voorbeelden:
#!/bin/bash for i in `seq 1 10`; do echo $i done 1 2 3 4 5 6 7 8 9 10
Pass arguments
Voorbeeld van een script, genaamd argtest
#!/bin/bash name=$1 echo $name
Aanroep:
./argtest blub
Uitvoer:
blub
Een invoer-argument is niet verplicht. Dus het script hierboven kun je ook prima zonder argument aanroepen.
Test voor null-waardes:
################################################################################### # Input-variabelen ################################################################################### # # Dit script is een universele routine. Daarom werkt het met input-argumenten! # domeinnaam=$1 mapalias=$2 # Verificatie ####################################### # if [ -z "$domeinnaam" ]; then echo "Geen domeinnaam gegeven" exit fi if [ -z "$mapalias" ]; then echo "Geen mapalias gegeven" exit fi
Regular expressions
- Handig in combinatie met grep
- In subdirectories alle bestanden die eindigen op .txt naar bestand speakers.txt copieren:
ls -R | grep .txt > speakers.txt
Toon namen van alle bestanden die beginnen met een hoofdletter E:
ls -R | grep ^E
Toon namen van alle bestanden die beginnen met een kleine letter e:
ls -R | grep ^e
Deze twee zijn namelijk niet hetzelfde.
Wellicht heb je stream editing nodig om regels vervolgens aan te passen. Bv. sed.
Script aanroepen vanuit een ander script
- Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command
- Call it with the source command (alias is .) like this: source /path/to/script
- Use the bash command to execute it: /bin/bash /path/to/script.
The first and third methods execute the script as another process, so variables and functions in the other script will not be accessible. The second method executes the script in the first script's process, and pulls in variables and functions from the other script so they are usable from the calling script.
In second method, if you are using 'exit' in second script, it will exit the first script as well. Which will not happen in first and third methods.
Variabelen
Belangrijk dat er geen spaties staan rondom aanroepen van variabelen:
in_bestand="2017.09.24-19.12.32-intermediate-01-org.mp4" uit_bestand="2017.09.24-19.12.32-intermediate-01-uit.mp4" echo $in_bestand echo $uit_bestand
Plaats variabelen tussen dubbele aanhalingstekens, om te voorkomen dat een shell-aanroep in de war raakt door spaties in de waarde van de variabele:
in_bestand="2017.09.24-19.12.32-intermediate-01-org.mp4" uit_bestand="2017.09.24-19.12.32-intermediate-01-uit.mp4" avconv -i "$in_bestand" -vf transpose=3 -vsync 2 "$uit_bestand"
Bronnen
- http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_01.html
- http://floppix.ccai.com/scripts1.html (Bash scripts)
- http://www.talug.org/events/20030709/cmdline_history.html (Using Bash's History Effectively)
- http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
- http://stackoverflow.com/questions/8352851/how-to-call-shell-script-from-another-shell-script