Shell Scripting

Inleiding

Een shell script heeft de .sh extensie.

De eerste regel is het absolute path naar de interpreter :

#!/bin/bash

Voor een systeem boot script is het aanbevolen de standaard shell te gebruiken :

#!/bin/sh

Een schell script heeft execute privileges nodig :

 chmod +x script.sh

Uitvoeren :

./script.sh

Het ECHO commando

echo "Hallo Wereld"

-n Nieuwe regel overslaan

-e Toestaan sequenties:

\0NNNthe character whose ASCII code is NNN (octal)
\\backslash
\aalert (bell)
\bbackspace
\csuppress trailing new line
\fform feed
\nnew line
\rcarriage return
\thorizontal tab
\vvertical tab
echo -n "Alles goed? [j/n]?"
echo -e "De Letter A : \0101"
echo -e "\aKolom 1 \tKolom 2 \tKolom 3"

Het READ commando

read naam
echo "Hallo $naam"

Invoer met timeout (10 seconden) :

read -t 10 -p "Naam : " naam

Invoer zonder af te beelden in Terminal :

read -s -p "Wachtwoord  : " wachtwoord

Variabelen

naam="David van der Tuijn"
leeftijd=27
echo "Mijn naam is $naam en ik ben $leeftijd jaar oud."

Variabelen als argument

echo "$0 is de naam van het script"
echo "$1 is het eerste argument"
echo "$2 is het tweede argument"
echo "$3 is het derde argument"
echo "Alle argumenten zijn $* of $@"

Constante variabelen

declare -r naam=davidvandertuijn

Integer:

declare -i leeftijd=27

Globale shell variabelen

export naam=davidvandertuijn
echo $naam

Relationele Operatoren

-eqis equal to5 == 6
-neis not equal to5 != 6
-ltis less than5 < 6
-leis less than or equal to5 <= 6
-gtis greater than5 > 6
-geis greater than or equal to5 <= 6

Logische operatoren

! expressieNOT
expressie1 -a expressie2 AND
expressie1 -o expressie2OR

Rekenkundige operatoren

1 + 2Optellen
2 - 1Aftrekken
10 - 5Delen
20 % 3Modulo
10 \* 3Vermenigvuldiging
som=`expr 2 + 3`
echo "de som van 2 plus 3 = $som"

Bestand test operatoren

-a fileTrue if file exists.
-b fileTrue if file exists and is a block special file.
-c fileTrue if file exists and is a character special file.
-d fileTrue if file exists and is a directory.
-e fileTrue if file exists.
-f fileTrue if file exists and is a regular file.
-g fileTrue if file exists and its set-group-id bit is set.
-h fileTrue if file exists and is a symbolic link.
-k fileTrue if file exists and its "sticky" bit is set.
-p fileTrue if file exists and is a named pipe (FIFO).
-r fileTrue if file exists and is readable.
-s fileTrue if file exists and has a size greater than zero.
-t fdTrue if file descriptor fd is open and refers to a terminal.
-u fileTrue if file exists and its set-user-id bit is set.
-w fileTrue if file exists and is writable.
-x fileTrue if file exists and is executable.
-O fileTrue if file exists and is owned by the effective user id.
-G fileTrue if file exists and is owned by the effective group id.
-L fileTrue if file exists and is a symbolic link.
-S fileTrue if file exists and is a socket.
-N fileTrue if file exists and has been modified since it was last read.
file1 -nt file2True if file1 is newer (according to modification date) than file2.
file1 -ot file2True if file1 is older than file2.
file1 -ef file2True if file1 and file2 have the same device and inode numbers.
BESTAND="$1"

if [ -f "$BESTAND" ]
then
  echo "File exists."
else
  echo "File does not exists!"
fi

If statement

getal=5
if [ $getal -lt 10 ]
then
  echo "$getal is kleiner dan 10"
fi

If-else statement

if [ $1 -gt 0 ]
then
  echo "$1 is positief"
else
  echo "$1 is negatief"
fi

If-else statement (genesteld)

if [ $1 -lt 10 ]; then
  echo "$1 is kleiner dan 10"
elif [ $1 -lt 20 ]; then
  echo "$1 is groter dan 10 en kleiner dan 20"
fi

For loop

for ((i=0; i<=5; i++))
do
  echo "Teller is $i"
done

While loop

i=0
while [ $i -le 10 ]
do
  echo "Teller is $i"
  i=`expr $i + 1`
done

Case statement

echo -n "Alles goed? [j/n]?"
read keuze
case $keuze in
   "j") echo "Antwoord is JA";;
   "n") echo "Antwoord is NEE";;
   *) echo "Antwoord is onbekend";;
esac

Functie

datum()
{
  echo `date +"%d %A %B %Y (%r)"`
}

echo "Datum : `datum`"

Met parameters :

leesplankje()
{
  echo "$1 is de eerste parameter"
  echo "$2 is de tweede parameter"
  echo "$3 is de derde parameter"
}

echo "Leesplankje : `leesplankje aap noot mies`"

Exit Status

Return waarde gelijk aan 0.script is succesvol uitgevoerd.
Return waarde ongelijk aan 0.script is niet succesvol uitgevoerd of een er is een fout opgetreden.
if [ $# -lt 1 ]; then
  exit 1
fi

De Exit Status wordt bewaard in $? :

echo $?

Meer informatie : Linux Shell Scripting Tutorial (LSST) v2.0