Bash Skripte 2018

Im Zuge der Vorbereitung auf die LPIC 1 Prüfung, enstand diese Github Repo. Es umfasst die Grundlagen rund um das Thema Bash Skripting, und soll in weiterer Zukunft als Sammlung von nützlichen Skripten für den täglichen gebrauch dienen.

Link zum Github Repo:
https://github.com/dg60/Bash_Scripts

Basics

Um ein skript ausführen zu können muss diese erst ausführbar gemacht werden:

$ chmod 755 script.sh

Deklarieren von Variablen

#!/bin/bash
#
# declare variables
#
var1=4
var33=300
var42=400

echo var1 = $var1
echo $var33 $var42

Ausgabe:

$ ./var.sh
var1 = 4
300 400

IF Abfrage

#!/bin/bash
# Basic if statement
if [ $1 -gt 100 ]
then
    echo Hey that\'s a large number.
else
    echo Hey that\'s a small number.
fi
#Read out the actual time
date

Ausgabe:

$ ./if.sh 1
"Hey that's a small number.
Sun Feb 25 11:25:19 CET 2018"

$ ./if.sh 101
"Hey that's a large number.
Sun Feb 25 11:25:34 CET 2018"

IF ELIF Abfrage

#!/bin/bash
# elif statements
if [ $1 -ge 18 ]
then
    echo You may go to the party.
elif [ $2 == 'yes' ]
then
    echo You may go to the party but be back before midnight.
else
    echo You may not go to the party.
fi

Ausgabe:

$ ./if_elif.sh 20
You may go to the party.

$ ./if_elif.sh 15 yes
You may go to the party but be back before midnight.

Case Abfrage

#!/bin/bash
# case example
case $1 in
    start)
        echo starting
        ;;
    stop)
        echo stoping
        ;;
    restart)
    echo restarting
        ;;
    *)
    echo don\'t know
    ;;
esac

Ausgabe:

$ ./case.sh
"don't know"

$ ./case.sh start
starting

$ ./case.sh stop
stoping

While loop

#!/bin/bash
# Basic while loop

counter=1

while [ $counter -le 10 ]
do
    echo $counter
    ((counter++))
done

echo All done

Ausgabe:

$ ./while_loop.sh
1
2
3
4
5
6
7
8
9
10
All done

Until loop

#!/bin/bash
# Basic until loop
counter=1

until [ $counter -gt 10 ]
do
    echo $counter
    ((counter++))
done

echo All done

Ausgabe:

$ ./until_loop.sh
1
2
3
4
5
6
7
8
9
10
All done

For loop

#!/bin/bash
# Basic for loop
names='Kylo Rey Luke'

for name in $names
do
    echo $name
done

echo All done

Ausgabe:

$ ./for_loop.sh
Kylo
Rey
Luke
All done

Functions

#!/bin/bash
# Passing arguments to a function


print_something () {
    echo I love $1
    return 1
}

print_something RedBull
print_something coffe

Ausgabe:

$ ./function_arguments.sh
I love RedBull
I love coffe

Verwendung von lokalen Variablen

#!/bin/bash
# local variables

var_change () {
    local var1='local 1'
    echo Inside function: var1 is $var1 : var2 is $var2
    var1='changed again'
    var2='2 changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

Ausgabe:

$ ./local_variables.sh
Before function call: var1 is global 1 : var2 is global 2
Inside function: var1 is local 1 : var2 is global 2
After function call: var1 is global 1 : var2 is 2 changed again

Rechenoperationen (Arithmetic)

#!/bin/bash
# Basic arithmetic using let
let a=5+4
echo $a # 9
let "a = 5 + 4"
echo $a # 9
let a++
echo $a # 10
let "a = 4 * 5"
echo $a # 20
let "a = $1 + 30"
echo $a # 30 + first command line argument

Ausgabe:

$ ./let.sh
9
9
10
20
30

Beispiele

Einbinden eines config Files

#config.sh
#!/usr/bin/env bash

#read out enviroment variables
user=${USER}
home=${HOME}
id=${UID}

#sourcing.sh
#!/usr/bin/env bash

. ./config.sh

echo "The name of the actual user was: $user with the id: $id "
echo "The Home directory of the user: $home"

echo "The log in history of the actual user:"
users=$(who | cut -d' ' -f1 | sort)
last | grep $users

Ausgabe:

$ ./sourcing.sh
The name of the actual user was: user with the id: 9999
The Home directory of the user: /home/user
The log in history of the actual user:
user   tty7         :0               Sun Feb 25 08:40    gone - no logout
user   tty7         :0               Wed Feb 14 19:00 - down  (7+13:05)
user   tty7         :0               Fri Feb  9 22:04 - down  (4+00:03)
user   tty7         :0               Wed Feb  7 19:06 - down  (2+00:57)
user   tty7         :0               Sat Feb  3 10:20 - down   (01:04)
user   tty7         :0               Fri Feb  2 20:47 - down   (02:18)
user   tty7         :0               Thu Feb  1 21:33 - crash  (23:12)
user   tty7         :0               Thu Feb  1 20:32 - down   (01:00)

Anlegen eines users

#!/usr/bin/env bash
# Add a user on a debian based System
# need root privilege


#check if the user has root privilege
if (( $EUID != 0 )); then
    echo "Please run the script as root or with sudo !"
    exit
fi

clear; 
echo -e "Please enter the name of the user:"
read username

clear; 
echo -e "Please enter the path of the home directory /home/username:"
read homedir

clear;
echo -e "Please enter a description for the user"
read description

# add the user
useradd -m -d ${homedir} -c ${description} ${username}

#check if the user was added
echo -e "User was added"
tail -1 /etc/passwd

#set the password for the user
echo -e "Please enter the password for ${username}"
passwd ${username}

Ausgabe:

$ sudo ./useradd.sh

Please enter the name of the user:
hugo

Please enter the path of the home directory /home/username:
/home/hugo

Please enter a description for the user:
test 

User was added
hugo:x:1003:1003:test:/home/hugo:
Please enter the password for hugo
Geben Sie ein neues UNIX-Passwort ein:
Geben Sie das neue UNIX-Passwort erneut ein:
passwd: Passwort erfolgreich geändert