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

if - then - elif - fi

#!/bin/bash

mijnvar=$1

if [ -z "$mijnvar" ]; then
	echo "mijnvar is leeg"

elif [[ "$mijnvar" =~ [0-9] ]]; then   # Geen idee waarom je dubbele vierkante haakjes hebt
	echo "Mijnvar is een getal"

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

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.

→ 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"

Timestamp

Meestal zoek ik zoiets als

echo `date "+%Y%m%d-%H%M"`

of

echo $(date +'%Y%m%d-%H%M%S')

Voorbeeld uit een backup-routine:

#!/bin/bash

#########################################
# Variabelen
#########################################
#
db="dwh"
timestamp=$(date +'%Y%m%d-%H%M%S')
doel=$timestamp"-dwh.sql"

#########################################
# Dumpen
#########################################
#
mysqldump --routines $db > $doel
gzip $doel

Bronnen