You may optionally enter a comparison operator (<, <=, >, >=, <> or =) at the beginning of each of your search values to specify how the comparison should be done.
#!/bin/sh
# The script is located in /tmp/abc/myscript.sh
export AppPath=$(dirname $0)
echo $0
# /tmp/abc/myscript.sh
echo $AppPath
# /tmp/abc
43
Bash Script
Sonstiges
Kopfzeile sh-Datei
#!/bin/sh
# Einfaches Beispiel ...
129
Bash Script
Variablen
Wert einer Variable zuweisen
# Aktuelles Verzeichnis in einer Variable setzen
mydir="Mein Verzeichnis lautet $PWD"
echo $mydir
163
Bash Script
Datum, Zeit
Aktuelles Datum/Uhrzeit
#!/bin/sh
myDate=$(date +"%Y-%m-%d")
echo $myDate
# 2014-01-14
echo $(date +"%Y-%m-%d_%a") # with short dayname
# 2017-01-05_Do (Do => German, localized short of Tuesday)
echo $(date +"%Y-%m-%d %H:%M:%S")
# 2014-01-14 22:15
# List of formatting options: https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
167
Bash Script
Prozeduren, Funktionen, Methoden
Deklaration einer Funktion/Prozedur
#!/bin/sh
do_something()
{
echo $1
echo $2
}
do_something "this is my first" " and second argument"
# this is my first and second argument
174
Bash Script
Strings
Strings miteinander verbinden
# http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger#Variablen-abgrenzen
#!/bin/bash
subString1="Hello"
subString2='World!'
echo "${subString1} ${subString2}"
Hello World!
echo "${subString1} ${subString2}. I'm in here ;-)"
# Hello World!. I'm in here ;-)
175
Bash Script
Filehandling
Verzeichnis rekursiv durchsuchen
# http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger#Variablen-Teil-2
#!/bin/bash
for file in /home/myUser/*/*.png ; do
echo "Diese Datei: $file"
fname=$(basename "$file")
echo "hat den Namen: $fname"
fdir=$(dirname "$file")
echo "und steht im Verzeichnis: $fdir"
# Diese Datei: /home/myUser/galaxy.png
# hat den Namen: galaxy.png
# und steht im Verzeichnis: /home/myUser
176
Bash Script
Filehandling
Prüfen ob Datei existiert
# http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger#If-Else-Anweisung
#!/bin/bash
filePath="/home/myUser/test.txt"
if [ -f ${filePath} ]
then
echo "There it is! ;-)"
else
echo "Whow, file is missing at this place :-("
fi
242
Bash Script
Fallunterscheidung
Wenn-Dann Abfrage
VAR1=1
VAR2=2
if [ "$VAR1" -eq "$VAR2" ]; then
echo "true";
else
echo "false";
fi
# false