# Bash Scripting ## Ein Script erstellen Eine Bash-Datei muss die Endung .sh haben Aufbau der Bash: ```bash #! /bin/bash echo Hello World! ``` Die Zeile `#! /bin/bash` sagt dem System, welche Shell sie verwenden werden, in diesem Fall die Bash-Shell. ## Script ausführbar machen Damit jedes Skript ausgeführt werden kann muss es ausführbar gemacht werden. ```bash chmod +x helloworld.sh ``` ## Script ausführen ```bash ./helloworld.sh ``` ## Programmbeispiele
CodeKommentar
`$(date +%A)`ruft die Systemvariable ab, die den aktuellen Wochentag speichert
[![Screenshot 2023-04-17 130424.jpg](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/scaled-1680-/screenshot-2023-04-17-130424.jpg)](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/screenshot-2023-04-17-130424.jpg) [![Screenshot 2023-04-17 130541.jpg](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/scaled-1680-/screenshot-2023-04-17-130541.jpg)](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/screenshot-2023-04-17-130541.jpg) [![Screenshot 2023-04-17 130604.jpg](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/scaled-1680-/screenshot-2023-04-17-130604.jpg)](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/screenshot-2023-04-17-130604.jpg) [![Screenshot 2023-04-17 130629.jpg](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/scaled-1680-/screenshot-2023-04-17-130629.jpg)](https://wiki.hhml.selfhost.co/uploads/images/gallery/2023-04/screenshot-2023-04-17-130629.jpg) ### Variable übergeben ```bash #! /bin/bash echo Hello $1 ``` `./hello.sh Hermann` gibt Hallo Hermann auf dem Bildschirm aus ```bash #! /bin/bash firstname=$1 surname=$2 echo Hello $firstname $surname ```

Bei Variablen wird zwischen Name und '=' keine Leerzeichen gelassen!

### Klammern ```bash #! /bin/bash firstnumber=$1 secondnumber=$2 echo The sum is $(($firstnumber+$secondnumber)) ``` ### Input ```bash #! /bin/bash echo -n "Hello, what is your name? " #-n bewirkt, dass die Eingabe nicht in einer neuen Zeile stattfindet read firstname echo -n "Thank you, and what is your surname? " read surname clear echo Hello $firstname $surname, how are you today? ``` ### If, then und Else ```bash #! /bin/bash echo -n "Hello, what is your Name " read firstname echo -n "And what is your surname? " read surname clear if [ "$firstname" == "Hermann" ] && [ "$surname" == "Pelzer" ] then echo "Awesome name," $firstname $surname else echo Hello $firstname $surname, how are you today? fi ``` ### While-Schleife ```bash #! /bin/bash count=0 while [ $count -lt 100 ];do echo $count let count=count+1 done ``` ### For-Schleife ```bash #! /bin/bash for count in {0..100}; do echo $count let count=count+1 done ``` ### Choice ```bash #! /bin/bash auswahl = 4 echo "1. Tails" echo "2. Is" echo "3. Awesome" echo -n "Please choose an option (1, 2, or 3) " while [ $choice -eq 4 ] ; do read choice if [ $choice -eq 1 ] ; then echo "You have chosen: Tails" else if [ $choice -eq 2 ] ; then echo "You have chosen: Is" else if [ $choice -eq 3 ] ; then echo "You have chosen: Awesome" else echo "Pleas make a choise between 1 to 3" echo "1. Tails" echo "2. Is" echo "3. Awesome" echo -n "Please choose an option (1, 2, or 3) " choice=4 fi fi fi done ```