BASH

Alles Rund um das Bash Scripten

Script Logik

Alle wichtigen Bash Kondizionen und ihre Anwendung

Script Logik

If, Elif, Else

#!/bin/bash
read -p "Enter your Age: " AGE
if [ $AGE -ge 80 ]
then
    echo "You are very old"
elif [ $AGE -ge 40 ]
then
    echo "You are old"
elif [ $marks -le 18 ]
then
    echo "You are very young"
elif [ $marks -le 30 ]
then
    echo "You are young"
else
    echo "You are the Rest"
fi
Script Logik

Case

#!/bin/bash
read -p "systemctl <your input> nginx" systemctl_cmd
case "$systemctl_cmd" in
        start)
            systemctl start nginx ;;
        stop)
            systemctl stop nginx ;;
        restart)
            systemctl restart nginx ;;
        *)
            echo "Your input was not vaild "
esac
Script Logik

Funktion

#!/bin/bash

#Function <-- Description
function delete_file {
	rm $1
}

#Script to call function
delete_file /root/useless_file
Script Logik

While

Inkrementelle Schlaufe

#!/bin/bash

i=0
while [ $i -lt 3 ]
do
	echo $i
    ((i++))
done

Unendliche Schlaufe

#!/bin/bash
while true;
do
	echo "Spam Message"
done

Skript Syntax

Bash syntaxe
https://acloudguru.com/blog/engineering/conditions-in-bash-scripting-if-statements

Skript Syntax

Dateiexistenz

[ -f Filepath ]


Dies testet ob die Datei existiert.

Skript Syntax

Kleiner/Grösser

[ $x -le 3 ]

Dies testet ob die x kleiner gleich (lower equal - le) ist.

[ $x -ge 3 ]

Dies testet ob die x grösser gleich (greather equal - ge) ist.

Skript Syntax

Datei-lesbar

[ -r Filepath ]

Dies testet ob die Datei lesbar ist.

Skript Syntax

Verneinung

Alle möglichen Syntaxe können in BASH mit einem "!" verneint werden.