Bash: Difference between revisions
Jump to navigation
Jump to search
>Homaar No edit summary |
No edit summary |
||
Line 24: | Line 24: | ||
*http://linuxconfig.org/Bash_scripting_Tutorial | *http://linuxconfig.org/Bash_scripting_Tutorial | ||
*http://www.rhrk.uni-kl.de/fileadmin/rhrk/dienste/support/publikationen/kursunterlagen/UNIX_II_bash_1.pdf | *http://www.rhrk.uni-kl.de/fileadmin/rhrk/dienste/support/publikationen/kursunterlagen/UNIX_II_bash_1.pdf | ||
==for loop over list of strings== | |||
<pre> | |||
strings=( | |||
string1 | |||
string2 | |||
"string with spaces" | |||
stringN | |||
) | |||
for i in "${strings[@]}"; do | |||
echo "$i" | |||
done | |||
</pre> | |||
==Bedingungen== | ==Bedingungen== |
Latest revision as of 11:57, 5 May 2025
Bash Files
FILES /bin/bash The bash executable /etc/profile The systemwide initialization file, executed for login shells /etc/bash.bashrc The systemwide per-interactive-shell startup file /etc/bash.bash.logout The systemwide login shell cleanup file, executed when a login shell exits ~/.bash_profile The personal initialization file, executed for login shells ~/.bashrc The individual per-interactive-shell startup file ~/.bash_logout The individual login shell cleanup file, executed when a login shell exits ~/.inputrc Individual readline initialization file
Links
- http://linuxconfig.org/Bash_scripting_Tutorial
- http://www.rhrk.uni-kl.de/fileadmin/rhrk/dienste/support/publikationen/kursunterlagen/UNIX_II_bash_1.pdf
for loop over list of strings
strings=( string1 string2 "string with spaces" stringN ) for i in "${strings[@]}"; do echo "$i" done
Bedingungen
Bedingungen für test Ausdruck Beispiel Erklärung -d verzeichnis [ -d /tmp ] Ist wahr, wenn die Datei existiert und ein Verzeichnis ist. -f datei [ -f txt.txt ] Ist wahr, wenn die Datei existiert und eine normale Datei ist. -w datei [ -w text.txt ] Ist wahr, wenn die Datei existiert und den Schreibzugriff erlaubt. -x datei [ -x script.sh ] Ist wahr, wenn die Datei existiert und die Ausführung erlaubt. -n string [ -n "$name" ] Ist wahr, wenn die übergebene Zeichenkette nicht leer ist. str1 = str2 [ "$1" = "Hallo" ] Ist wahr, wenn beide Zeichenketten identisch sind. z1 -eq z2 [ 1 -eq $summe ] Ist wahr, wenn beide Zahlen gleich groß sind (in Bedingungen wird zwischen Zahlen und Zeichenketten unterschieden). z1 -lt z2 [ 17 -lt $zahl ] Ist wahr, wenn die erste Zahl kleiner als die zweite Zahl ist (lt = lower then). z1 -gt z2 [ 28 -gt $tag ] Ist wahr, wenn die erste Zahl größer als die zweite Zahl ist. z1 -ne z2 [ $zahl -ne 7 ] Ist wahr, wenn beide Zahlen ungleich sind. ! ausdruck [ ! 1 -eq $zahl ] Ist wahr, wenn der Ausdruck falsch ist (also eine Negierung).
Strings
= equal != not equal < less then > greater then -n s1 string s1 is not empty -z s1 string s1 is empty
Scriptfetzen
#Auth Log durchsuchen: cat /var/log/auth.log | grep "Invalid user" | cut -d ':' -f 1,2,4 | sort | uniq
genpasswd() { local l=$1 [ "$l" == "" ] && l=16 tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs }
Easteregg
47 # ----------------- 48 # Easter Egg alert! 49 # ----------------- 50 # Chet Ramey seems to have snuck a bunch of undocumented C-style 51 #+ constructs into Bash (actually adapted from ksh, pretty much). 52 # In the Bash docs, Ramey calls (( ... )) shell arithmetic, 53 #+ but it goes far beyond that. 54 # Sorry, Chet, the secret is out.