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.
# 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
#!/bin/sh
myFilename="$1"
# or
myFilename="/tmp/Test/myFile.txt"
# Get only the path
path_only=$(dirname "$myFilename")
# /tmp/Test
# if it is a local file in the path, then . will be prompted e.g. myFilename="myFile.txt"
# Filename only
filename_only=$(basename "$myFilename")
# myFile.txt
# get only filename without extention
filename_without_ext="${filename_only%.*}"
# myFile
# get only the extension
filename_extention_only="${myFilename##*.}"
# txt
# put them together again
myFilename_together="${path_only}/${filename_without_ext}.${filename_extention_only}"
# /tmp/Test/myFile.txt