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.
Empty variable detection
https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/
Exit script
- https://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs
- https://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error/2871034
if - then - elif - fi
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
Regular expressions & grep
- 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.
Regular expressions & scripts
Om regex te gebruiken in vergelijkingen, gebruik =~
. Zie https://www.networkworld.com/article/2693361/operating-systems/unix-tip-using-bash-s-regular-expressions.html voor details.
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.
→ Ik heb prima ervaringen met source
. Wel belangrijk dat de working directory ok is, of dat je absolute padnamen gebruikt.
Spaces in arguments
Gebruik dubbele aanhalingstekens. Bv.
# Change working dir. Use "" because of spaces ################################################################ # cd "$script_folder"
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