Inhaltsverzeichnis

Linux console is programmed using bash. Siehe auch batch for windows.

# This is a single line comment
 
<< 'MULTILINE-COMMENT'
    This is
    a multiline
    comment
MULTILINE-COMMENT
name=Manuel // set variable
echo $name // use variable
 
echo $# // no of arguments passed
echo $1 $2 // print 1st and 2nd argument
echo -n TopSecret | md5sum
rename 's/old_pattern/new_pattern/' *.txt
command_name | while IFS= read -r line; do
    # Verarbeite jede Zeile hier
    echo "Zeile: $line"
done

Arrays

#!/bin/bash
 
# Declare an associative array called fruits
declare -A fruits
 
# Assign key-value pairs to the array
fruits[apple]="red"
fruits[banana]="yellow"
fruits[grape]="purple"
 
# Print specific element
echo "The color of an apple is ${fruits[apple]}."
 
# Iterate through the array
for key in "${!fruits[@]}"; do
  echo "$key: ${fruits[$key]}"
done

if else

password="lowsecret"
if [ $password = "topsecret" ] then
    echo "yes"
else
    echo "no"
fi
 
if [ $1 -eq $2 ] then
    ....
fi
ParameterDescription
-eqequals
-nenot equals
-ltlower than
-gtgreater than
-lelower equals
-gegreater equals

For While

for i in {1..5}; do
    echo $i
done
 
counter=0
while [ $counter -lt 5 ]; do
    echo $counter
    counter=$((counter + 1))
done

Functions

function greet() {
 
  echo $1
 
}
 
greet "Manuel"
 
// oder so
 
func_result="$(greet Manuel)"
echo $func_result

Files

if [ -d "verzeichnis" ]; then
    echo "Das Verzeichnis existiert."
fi
 
if [ -f "datei.txt" ]; then
    echo "Die Datei existiert."
fi

Example

#!/usr/bin/bash
 
if [ "${1}" = "" ]; then
	echo "Verwendung: ${0} <Verzeichnis alt> <Verzeichnis neu> <Ergebnis-Verzeichnis>"
	exit
fi
 
if [ ! -d "${1}" ]; then
	echo "Verzeichnis \"${1}\" existiert nicht"
	exit
fi
 
if [ "${2}" = "" ]; then
	echo "Verwendung: ${0} <Verzeichnis alt> <Verzeichnis neu> <Ergebnis-Verzeichnis>"
	exit
fi
 
if [ ! -d "${2}" ]; then
	echo "Verzeichnis \"${2}\" existiert nicht"
	exit
fi
 
if [ "${3}" = "" ]; then
	echo "Verwendung: ${0} <Verzeichnis alt> <Verzeichnis neu> <Ergebnis-Verzeichnis>"
	exit
fi
 
if [ ! -d "${3}" ]; then
	echo "Verzeichnis \"${3}\" existiert nicht"
	exit
fi
 
export LANG=de_DE@euro.ISO-8859-15
export LC_ALL=de_DE.ISO-8859-15
export LC_MESSAGES=de_DE.CP858
export LC_COLLATE=de_DE.CP858
export LC_CTYPE=de_DE.CP858
 
 
echo "File;Zeilen altes File;Zeilen neues File;Entfallene Zeilen;Entfallen Prozent;Hinzugekommene Zeilen;Hinzugekommen Prozent"
for FILE in `ls -l ${1}/ | sed -e "s/.* //1" | grep -a "\.csv" | sed -e "s/\.csv//1"`; do
	cat "${1}"/${FILE}.csv | grep -ai "Pfad;Identitaet;Rechte" |tr [:upper:] [:lower:] > "${3}/${FILE}_old_sorted.csv"
	cat "${1}"/${FILE}.csv | grep -vai "Pfad;Identitaet;Rechte" |tr [:upper:] [:lower:] | sort >> "${3}/${FILE}_old_sorted.csv"
 
	cat "${2}"/${FILE}.csv | grep -ai "Pfad;Identitaet;Rechte" |tr [:upper:] [:lower:] > "${3}/${FILE}_new_sorted.csv"
	cat "${2}"/${FILE}.csv | grep -vai "Pfad;Identitaet;Rechte" |tr [:upper:] [:lower:] | sort >> "${3}/${FILE}_new_sorted.csv"
 
 
	cat "${3}/${FILE}_old_sorted.csv" | grep -ai "Pfad;Identitaet;Rechte" > "${3}/${FILE}_entfallen.csv"
	diff "${3}/${FILE}_old_sorted.csv" "${3}/${FILE}_new_sorted.csv" | grep -a "^<" | sed -e "s/^< //1" >> "${3}/${FILE}_entfallen.csv" 
 
	cat "${3}/${FILE}_old_sorted.csv" | grep -ai "Pfad;Identitaet;Rechte" > "${3}/${FILE}_neu.csv"
	diff "${3}/${FILE}_old_sorted.csv" "${3}/${FILE}_new_sorted.csv" | grep -a "^>" | sed -e "s/^> //1" >> "${3}/${FILE}_neu.csv" 
 
	ANZ1=`cat "${1}"/${FILE}.csv | wc -l`
	ANZ2=`cat "${2}"/${FILE}.csv | wc -l`
	ANZ3=`cat "${3}/${FILE}_entfallen.csv" | wc -l`
	ANZ4=`cat "${3}/${FILE}_neu.csv"  | wc -l`
	PROZ1=`echo "($ANZ3*100)/$ANZ1" | bc`
	PROZ2=`echo "($ANZ4*100)/$ANZ2" | bc`
 
	echo "${FILE}.csv;${ANZ1};${ANZ2};${ANZ3};$PROZ1%;${ANZ4};$PROZ2%"
	#if [ $ANZ1 = $ANZ3 ]; then
	#	echo "Achtung ${FILE}.csv ==== Entfallen = Alt"		
	#elif [ $ANZ2 = $ANZ4 ]; then
	#	echo "Achtung ${FILE}.csv ==== Hinzugekommen = Neu"		
	#fi
 
	rm -f "${3}/${FILE}_old_sorted.csv" "${3}/${FILE}_new_sorted.csv"
done