Bash programming

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

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

https://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs

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.

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

Stackoverflow:

  1. 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
  2. Call it with the source command (alias is .) like this: source /path/to/script
  3. 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