First Version
0
Anhang.tex
Executable file
BIN
BashingCookBook.pdf
Normal file
75
BashingCookBook.tex
Executable file
@ -0,0 +1,75 @@
|
||||
%******************************************************
|
||||
% Bashing, Scripts and Cook Book
|
||||
%
|
||||
% Author: Uwe Schimanski
|
||||
% Company: Seab@er Software AG
|
||||
% Mail: uws@seabaer-ag.de
|
||||
% Date: 28-Jun-2018
|
||||
% Compiling: 1. pdflatex BashingCookBook.tex
|
||||
% 2. makeindex index.ndx -s StyleInd.ist
|
||||
% 3. optional: biber BashingCookBook.tex
|
||||
% 4. pdflatex BashingCookBook.tex 2x
|
||||
%******************************************************
|
||||
|
||||
\documentclass[11pt,fleqn]{book}
|
||||
|
||||
\input{structure}
|
||||
\makeindex
|
||||
|
||||
%\DeclareCaptionFormat{listing}{\rule{\dimexpr\textwidth+17pt\relax}{0.4pt}\par\vskip1pt#1#2#3}
|
||||
%\captionsetup[lstlisting]{format=listing,singlelinecheck=false, margin=0pt, font={sf}, labelsep=space, labelfont=bf}
|
||||
|
||||
\begin{document}
|
||||
\begin{titlepage}
|
||||
\begin{center}
|
||||
{\Huge \ \\[12ex] Bashing\\[4ex] The Cook Book\\ \par } % Nach \par muss ein Leerzeichen sein.
|
||||
\vspace{2cm}
|
||||
{\large Uwe Schimanski\\[2ex] Seab@er Software\par }
|
||||
\vspace{1cm}
|
||||
Goch, den \today
|
||||
\end{center}
|
||||
\vspace{2cm}
|
||||
\begin{figure}[ht]
|
||||
\includegraphics[scale=0.5]{FirstPage1.png}
|
||||
\end{figure}
|
||||
\end{titlepage}
|
||||
|
||||
%----------------------------------------------------------------------------------------
|
||||
% COPYRIGHT PAGE
|
||||
%----------------------------------------------------------------------------------------
|
||||
|
||||
\newpage
|
||||
~\vfill
|
||||
\thispagestyle{empty}
|
||||
|
||||
\noindent Copyright \copyright\ 2018 Uwe Schimanski\\ % Copyright notice
|
||||
|
||||
\noindent \textsc{Published by Publisher}\\ % Publisher
|
||||
|
||||
\noindent \textsc{seabaer-ag.de}\\ % URL
|
||||
|
||||
\noindent Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License (the ``License''). You may not use this file except in compliance with the License. You may obtain a copy of the License at \url{http://creativecommons.org/licenses/by-nc/3.0}. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \textsc{``as is'' basis, without warranties or conditions of any kind}, either express or implied. See the License for the specific language governing permissions and limitations under the License. % License information
|
||||
|
||||
\noindent \textit{First printing, May 2018} % Printing/edition date
|
||||
|
||||
|
||||
\frontmatter
|
||||
{
|
||||
\hypersetup{linkcolor=black}
|
||||
\tableofcontents
|
||||
}
|
||||
\include{Vorwort}
|
||||
%\include{Inhaltsverzeichnis}
|
||||
|
||||
\mainmatter
|
||||
\input{Kapitel1/Index} % input mit verschachtelung
|
||||
\input{Kapitel2/Index}
|
||||
\input{Kapitel3/Index}
|
||||
\input{Kapitel4/Index}
|
||||
|
||||
\backmatter
|
||||
\include{Anhang}
|
||||
\printindex
|
||||
%\include{Index}
|
||||
|
||||
\end{document}
|
BIN
FirstPage.png
Executable file
After Width: | Height: | Size: 70 KiB |
BIN
FirstPage1.png
Executable file
After Width: | Height: | Size: 74 KiB |
269
Kapitel1/AusgabeShell.tex
Executable file
@ -0,0 +1,269 @@
|
||||
\section{Ein- und Ausgabe auf der Shell}
|
||||
\subsection{Echo}
|
||||
\begin{justify}
|
||||
M\"ochte man Text auf der Konsole ausgeben, so kann man das mit dem Befehl {\ttfamily echo} erledigen.
|
||||
Den Befehl {\ttfamily echo} kann man nicht nur in einem Script verwenden, sondern auch direkt in einer Shell,
|
||||
wenn man zum Beispiel den Inhalt einer Variable sich anzeigen lassen m\"ochte.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel echo]
|
||||
uws@tux>echo "Connected user: $USER"
|
||||
Connected user: uws
|
||||
\end{lstlisting}
|
||||
Gibt man hinter dem Befehl {\ttfamily echo} die Option {\ttfamily -n} an, so bleibt der Cursour hinter den auszugebenden
|
||||
Text stehen. Die Option {\ttfamily -e} erlaubt die {\ttfamily Backslash Escapes.}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Escape Sequence]
|
||||
uws@tux>cat eingabe.sh
|
||||
#!/bin/env bash
|
||||
#
|
||||
# Einlesen einer Benutzereingabe
|
||||
#
|
||||
echo -n -e "\tBitte Mysql Password eingeben: "
|
||||
read strPasswd
|
||||
\end{lstlisting}
|
||||
%-------------------------------------------------------------------------------
|
||||
% Section: Read
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{Read}
|
||||
Mit dem Befehl {\ttfamily read} kann man die Eingabe in einer Variable speichern. Ohne eine zus\"atzliche Option bei dem
|
||||
Befehl {\ttfamily read}, wird die Eingabe nach einem Return in der Konsole wiederholt. Soll die Eingabe nicht angezeigt werden,
|
||||
so gibt man hierzu die Option {\ttfamily -s} an. Diese Option unterdr\"uckt das Echo auf der Konsole.
|
||||
Die Option {\ttfamily -v<zahl>} liest die angegebene Anzahl an Zeichen ein.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel read]
|
||||
uws@tux>cat eingabe1.sh
|
||||
#!/bin/env bash
|
||||
#
|
||||
# Einlesen einer Benutzereingabe
|
||||
#
|
||||
echo -n "Bitte Mysql Password eingeben: "
|
||||
read -s -n10 strPasswd
|
||||
\end{lstlisting}
|
||||
|
||||
\subsection{Printf}
|
||||
In Bash Scripten und auch in der Konsole kann man den Befehl {\ttfamily printf} verwenden.
|
||||
Mit diesem Befehl ist es m\"oglich, Zeilenvorsch\"ube und auch Tabs zu verwenden. Einen Zeilenvorschub
|
||||
wird mit der Option {\ttfamily \textbackslash n} gemacht und mit {\ttfamily \textbackslash t} wird ein Tab erzeugt.
|
||||
%\begin{flushleft}
|
||||
In der nachfolgenden Tabelle werden die Konfigurationsdateien aufgelistet, die bei einem Login/Aufruf einer Shell in der Reihenfolge verarbeitet werden.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Printf]
|
||||
uws@tux>cat Ausgabe.sh
|
||||
#!/bin/env bash
|
||||
#
|
||||
# Ausgabe von Text
|
||||
#
|
||||
clear
|
||||
printf "\n\n\t\t *******************************************"
|
||||
printf "\n\n\t\t Dieses Programm wir ausgefuehrt"
|
||||
printf "\n\t\t mit freundlicher Untestuetzung von"
|
||||
printf "\n\t\t\ Seab@er Software AG"
|
||||
printf "\n\n\t\t ********************************************\n"
|
||||
\end{lstlisting}
|
||||
|
||||
{\ttfamily Printf} wurde von der Programmiersprache C entliehen. Der Aufbau ist {\ttfamily "\%Format" Daten}.
|
||||
In der nachfolgenden Tabelle sind die Formatierungen aufgelistet.\\[2ex]
|
||||
|
||||
\begin{table}[ht]
|
||||
|
||||
\begin{tabular}{p{1.5cm}p{14.5cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Format}} & \emph{\textbf{Beschreinung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\%5.2f & Flie"skomma mit f\"unf Stellen vor dem Komma und 2 Nachkomma Stellen\\
|
||||
\%.10s & Zeichenkette mit maximal 10 Characters\\
|
||||
\%X\textbackslash n & Hexadezimal mit Gro\ss buchstaben\\
|
||||
\%y\textbackslash n & Hexadezimal mit Kleinbuchstaben\\
|
||||
\%\#X\textbackslash n & Hexadezimal mit Gro\ss buchstaben und f\"uhrenden 0X\\
|
||||
\%i\textbackslash n & Ganzzahl\\
|
||||
\%s & Zeichenkette (string)\\
|
||||
\%d & Dezimal\\
|
||||
\end{tabular}
|
||||
\caption{Formate}
|
||||
\end{table}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Formatierung]
|
||||
uws@tux>cat format.sh
|
||||
#!/bin/env bash
|
||||
a=456.863
|
||||
b=387,162
|
||||
c="Ohne_Unterstrich_keine_Ausgabe"
|
||||
printf "Wert a: %5.2f\n" `echo $a | tr . ,`
|
||||
printf "Wert b= %5.2f\n" $b
|
||||
printf "Wert c: %.30s\n Wert d: %.30s" $c "So geht es ohne _"
|
||||
printf "%10.5s\n" "Linux-Magazin" # Ausgabe rechtsbuendig, Feldgroesse 10
|
||||
printf "%-10.5s\n" "Linux-Magazin" # Ausgabe linksbuendig, Feldgroesse 10
|
||||
printf "%.6s\n" "Linux-Magazin"
|
||||
|
||||
uws@tux>./format.sh
|
||||
Wert a: 456,863
|
||||
Wert b: 387,162
|
||||
Wert c: Ohne_Unterstrich_keine_Ausgabe
|
||||
Wert d: So geht es ohne _
|
||||
Linux
|
||||
Linux
|
||||
Linux-
|
||||
\end{lstlisting}
|
||||
\begin{justify}
|
||||
M\"ochte man den Text in Farbe ausgeben, so wird hierzu das Zeichen {\ttfamily ESC (\textbackslash 033)} verwendet. Nach dem {\ttfamily Escape} Zeichen wird die Farbe angegeben. Die Angabe bezieht sich dann nicht nur auf die Zeile, sondern auf alle Ausgaben von Texten. Deshalb sollte man nach dem zu f\"arbenden Text wieder die urspr\"ungliche Farbe eingestellt werden.
|
||||
\end{justify}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Farbige Ausgabe]
|
||||
uws@tux>cat ColorAusgabe.sh
|
||||
#!/bin/env bash
|
||||
#
|
||||
# Ausgabe von Text in Farbe
|
||||
#
|
||||
clear
|
||||
printf "\033[34m\n\n\t\t *******************************************"
|
||||
printf "\033[33m\n\n\t\t Dieses Programm wir ausgefuehrt"
|
||||
printf "\n\t\t mit freundlicher Untestuetzung von"
|
||||
printf "\n\t\t\ Seab@er Software AG"
|
||||
printf "\n\n\t\t ********************************************\n"
|
||||
printf "\033[0m" # ursprüngliche Farbe einstellen.
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{1cm}p{15cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Wert}} & \emph{\textbf{Farbe}} \\
|
||||
\hline
|
||||
\hline
|
||||
0m & Reset der Farbe\\
|
||||
01m & Fett\\
|
||||
04m & Unterstreichen\\
|
||||
05m & Blinkend\\
|
||||
07m & Vorder- und Hintergrunffarbe vertauscht\\
|
||||
22m & Normale Intensit\"at wiederherstellen\\
|
||||
30m & Vordergrund schwarz\\
|
||||
31m & Vordergrund rot\\
|
||||
32m & Vordergrund gr\"un\\
|
||||
33m & Vordergrund braun (gelb)\\
|
||||
34m & Vordergrund blau\\
|
||||
35m & Vordergrund magenta\\
|
||||
36m & Vordergrund cyan\\
|
||||
37m & Vordergrund wei\ss\\
|
||||
40m & Hintergrund schwarz\\
|
||||
41m & Hintergrund rot\\
|
||||
42m & Hintergrund gr\"un\\
|
||||
43m & Hintergrund braun\\
|
||||
44m & Hintergrund blau\\
|
||||
45m & Hintergrund magenta\\
|
||||
46m & Hintergrund cyan\\
|
||||
47m & Hintergrund wei\ss\\
|
||||
49m & Voreingestellter Hintergrund\\
|
||||
\end{tabular}
|
||||
\caption{Farbwerte}
|
||||
\end{table}
|
||||
\begin{justify}
|
||||
Wird bei {\ttfamily 33m} die Farbe braun ausgegeben und man m\"ochte aber die Farbe {\ttfamily Gelb} ausgeben, so kann kann man das mit der zus\"atzlichen Angabe von {\ttfamily 01m} machen.
|
||||
\end{justify}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Gelbe Ausgabe]
|
||||
uws@tux>printf "\033[01m\033[33mFarbe Gelb \033[33m und nun Braun."
|
||||
\end{lstlisting}
|
||||
Eine Farbausgabe des Textes kann auch mit der Sequence {\ttfamily \textbackslash e[1;32m} gemacht werden. Dieses funktionert auch mit {\ttfamily echo}. Hierzu wird noch die Option {\ttfamily -e} mit angegeben.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Echo]
|
||||
uws@tux>echo -e " \e[1;32m Dieser Text ist in Gr\"un. \e[1;0m"
|
||||
\end{lstlisting}
|
||||
%--------------------------------------------------------------------------------
|
||||
% Subsection: Null
|
||||
%--------------------------------------------------------------------------------
|
||||
\subsection{Null}
|
||||
Möchte man keine Ausgabe in der Shell haben, so kann man dieses nach {\ttfamily /dev/null} umlenken. Mit {\ttfamily 1} werden die normalen Ausgaben umgelenkt und mit {\ttfamily 2} die Fehlermeldungen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Null]
|
||||
uws@tux>cat DemoNull.sh
|
||||
#!/usr/bin/env bash
|
||||
# %1 = erster Ausgabe Kanal, hier /dev/null
|
||||
ping -w1 -c1 tux2 >/dev/null 2>%1
|
||||
|
||||
ssh ${USER}@${HOST} 'ls' 2> /dev/null
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
%--------------------------------------------------------------------------------
|
||||
% Subsection: Beispiele
|
||||
%--------------------------------------------------------------------------------
|
||||
\subsection{Beispiele}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Textbox]
|
||||
uws@tux>cat textbox.sh
|
||||
#!/bin/env bash
|
||||
box()
|
||||
{
|
||||
printf '*%.0s' $(seq 1 67) # Print * with length 67
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
content()
|
||||
{
|
||||
printf "*"
|
||||
if [ $# -eq 0 ]; then
|
||||
printf ' %.0s' $(seq 1 65)
|
||||
elif [ $# -eq 1 ]; then
|
||||
TEXTR="$1"
|
||||
printf ' %.0s' $(seq 1 17)
|
||||
printf "${TEXTR}"
|
||||
printf ' %.0s' $(seq 1 $[48-${#TEXTR}])
|
||||
elif [ $# -eq 2 ]; then
|
||||
TEXTL="$1"
|
||||
TEXTR="$2"
|
||||
printf ' %.0s' $(seq 1 3)
|
||||
printf "${TEXTL}"
|
||||
printf ' %.0s' $(seq 1 $[14-${#TEXTL}])
|
||||
printf "${TEXTR}"
|
||||
printf ' %.0s' $(seq 1 $[48-${#TEXTR}])
|
||||
fi
|
||||
printf "*\n"
|
||||
}
|
||||
|
||||
SCRIPTVERSION="13.01.07"
|
||||
|
||||
IFS=$'\012'
|
||||
|
||||
box
|
||||
content
|
||||
content "Script:" "$(basename $0)"
|
||||
content "Version:" "${SCRIPTVERSION}"
|
||||
content
|
||||
content "This script is an example."
|
||||
content
|
||||
box
|
||||
|
||||
uws@tux>./textbox.sh
|
||||
********************************************************************
|
||||
* *
|
||||
* Script: textbox.sh *
|
||||
* Version: 13.01.07 *
|
||||
* *
|
||||
* This script is an example. *
|
||||
* *
|
||||
********************************************************************
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Color mit Function]
|
||||
uws@tux>cat FuncColorText.sh
|
||||
#!/usr/bin/env bash
|
||||
|
||||
err() {
|
||||
RESET="\e[1;0m"
|
||||
FETT="\e[1;1m"
|
||||
ROT="$FETT\e[1;31m"
|
||||
local txt=$1; shift
|
||||
printf "${ROT}==>${RESET}${FETT} ${txt}${RESET}\n" "$@" >&2
|
||||
}
|
||||
|
||||
msg() {
|
||||
RESET="\e[1;0m"
|
||||
FETT="\e[1;1m"
|
||||
GRUEN="$FETT\e[1;32m"
|
||||
local txt=$1; shift
|
||||
printf "${GRUEN}==>${RESET}${FETT} ${txt}${RESET}\ n" "$@" >&2
|
||||
}
|
||||
|
||||
msg "File found in folder."
|
||||
err "No file found."
|
||||
\end{lstlisting}
|
||||
\end{justify}
|
12
Kapitel1/AusgabeUmleiten.tex
Executable file
@ -0,0 +1,12 @@
|
||||
\section{Ausgabe umleiten}
|
||||
M"ochte man die Ausgabe in einer Datei umleiten, so wird das mit einem {\ttfamily >} gemacht.
|
||||
Soll die Ausgabe an einer bestehenden Datei angehangen werden, so gibt man {\ttfamily >>}an,
|
||||
ansonsten wird die Datei neu erstellt und der Inhalt gel"oscht. Sollen auch die Error Meldungen
|
||||
in einer Datei geschrieben werden, so wird das mit {\ttfamily 2>} gemacht.\\
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Ausgabe umleiten, label=lst:bash]
|
||||
uws@tux>echo Hello World 1>&2> ~/data/MyLog.txt
|
||||
\end{lstlisting}
|
||||
|
||||
\subsection{Ein-/Ausgabekanal}
|
12
Kapitel1/BefehleMehrzeilig.tex
Executable file
@ -0,0 +1,12 @@
|
||||
\section{Befehle mehrzeilig}
|
||||
\begin{justify}
|
||||
F"ur eine bessere "Ubersicht in einem Script kann es erforderlich sein, den Befehl in mehrere Zeilen zu schreiben. Dieses kann man mit einem Backslash {\ttfamily \verb|\|} am Ende der Zeile machen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Mehrzeilig, label=lst:bash]
|
||||
uws@tux>cat example3.sh
|
||||
#!/bin/env bash
|
||||
ls \
|
||||
-lah \
|
||||
*.txt
|
||||
\end{lstlisting}
|
||||
\end{justify}
|
27
Kapitel1/BefehleVerketten.tex
Executable file
@ -0,0 +1,27 @@
|
||||
\section{Befehle verketten}
|
||||
\begin{flushleft}
|
||||
Befehle lassen sich mit {\ttfamily \&\& } in einer Zeile verketten. Diese Verkettung ist eine
|
||||
{\ttfamily und} Verbindung. Eine {\ttfamily oder} Verbindung wird mit zwei Pipe Zeichen
|
||||
{\ttfamily ||} gemacht. Anstelle von den Pipe Zeichen kann auch ein Semikolon genommen werden.\\[2ex]
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Verketten, label=lst:bash]
|
||||
uws@tux>cat example_command.sh
|
||||
#!/bin/env bash
|
||||
ask_fragen() {
|
||||
for (( i=1;i<=4;i++ )); do
|
||||
echo "$i: $1" && shift
|
||||
sleep 2
|
||||
done
|
||||
sekunden=10
|
||||
let timer=10
|
||||
while [ "$sekunden" -gt 0]; do
|
||||
printf "$timer" && let sekunden-- && let timer--
|
||||
sleep 1
|
||||
done
|
||||
printf "\n"
|
||||
}
|
||||
### Main ###
|
||||
ask_fragen "Was ist die Bash?" "Was ist KVM?" "Was ist LVM?" "???"
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
82
Kapitel1/DatumBashScripte.tex
Executable file
@ -0,0 +1,82 @@
|
||||
\section{Datum in Bash Scripte}
|
||||
In einem Bash Script kann man sich sein eigenes Datumsformat zusammenstellen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiele Datumsformat, label=lst:bash]
|
||||
uws@tux>export stamp=$(date +%a)
|
||||
uws@tux>echo $stamp
|
||||
Do
|
||||
|
||||
uws@tux>export stamp=`date +%Y_%m_%d`
|
||||
uws@tux>echo $stamp
|
||||
2010_06_17
|
||||
|
||||
uws@tux>printf "`date +%Y_%m_%d` - Time\n"
|
||||
2010_06_17 - Time
|
||||
|
||||
uws@tux># -d oder --date="yesterday"
|
||||
uws@tux># weitere Werte: next Friday, 2 days ago, 1 day, next day
|
||||
uws@tux>printf "$(date -d "yesterday" '+%Y.%m.%d %H:%M:%S')\n"
|
||||
2010.06.16 10:23:44
|
||||
uws@tux># Weitere Beispiele ohne Format Angabe
|
||||
uws@tux># https://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html
|
||||
uws@tux>printf "$(date -d "1 years ago")\n"
|
||||
uws@tux>printf "$(date -d "3 years")\n"
|
||||
uws@tux>printf "$(date -d "this Friday")\n"
|
||||
uws@tux>printf "$(date -d "second Fri")\n"
|
||||
uws@tux>printf "$(date -d "Second Friday")\n"
|
||||
uws@tux>printf "$(date -d "last Friday")\n"
|
||||
uws@tux>printf "$(date -d "5 months 10 days")\n"
|
||||
uws@tux>printf "$(date -d "-3 months 8 day ago")\n"
|
||||
|
||||
uws@tux>cat example.sh
|
||||
#!/bin/bash
|
||||
# Example for Date
|
||||
#
|
||||
DATUM=`date +%Y_%m_%d`
|
||||
stamp=$(date +%a)
|
||||
find . -type -f ( -name '*.jpg' ) -exec zip ${Datum}_jpg.zip {} \;
|
||||
mv example.txt example.txt.$stamp
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
\begin{flushleft}
|
||||
Eine Auflistung f"ur die Datumformatierung befindet sich in der nachfolgenden Tabelle.
|
||||
%
|
||||
% Hier kommt eine Tabelle, 2 Spalten
|
||||
%
|
||||
% \emph und \textbf => Kursiv und Fett
|
||||
% \ => demask
|
||||
% Umlaute => \"A = Ä
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{1.5cm}p{14.5cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Format}} & \emph{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\%H & Stunden (00 bis 23)\\
|
||||
\%I & Stunden (01 bis 12)\\
|
||||
\%M & Minuten (00 bis 59)\\
|
||||
\%S & Sekunden (00 bis 23)\\
|
||||
\%p & Vor- oder Nachmittag (AM oder PM)\\
|
||||
\%r & Zeitangabe, 12 Stunden (hh:mm:ss AM/PM)\\
|
||||
\%R & Zeitangabe, 24 Stunden (hh:mm:ss), entspricht damit \%H:\%M\\
|
||||
\%s & Sekunden seit dem 1. Januar 1970 (Unix Zeit)\\
|
||||
\%Z & Aktuelle Zeitzone (CEST, GMT, ...)\\
|
||||
\%a & Wochentag in Kurzform (Son, Mon, Die, ...)\\
|
||||
\%A & Wochentag in Langform\\
|
||||
\%b & Monat in Kurzform (Jan, Feb, ...)\\
|
||||
\%B & Monat in Langform\\
|
||||
\%d & Tag in zweistelliger Zahl\\
|
||||
\%e & Tag (einstellig mit Leerzeichen)\\
|
||||
\%D & Datum in der Form mm/dd/yy\\
|
||||
\%j & Zeigt, der wie vielte Tag im angegebenen Jahr ist\\
|
||||
\%u & Zeigt, welcher Wochentag es ist (1 bis 7)\\
|
||||
\%U & Zeigt, wie viele Wochen im angegebenen jahr es ist\\
|
||||
\%m & Monat, zweistellig\\
|
||||
\%y & Jahr, zweistellig\\
|
||||
\%Y & Jahr, vierstellig\\
|
||||
\%\% & Ausgabe des Prozentzeichens\\
|
||||
\%n & Zeilenende\\
|
||||
\%t & Tabulator\\
|
||||
\end{tabular}
|
||||
\caption{Formate}
|
||||
\end{table}
|
||||
\end{flushleft}
|
83
Kapitel1/Exec.tex
Executable file
@ -0,0 +1,83 @@
|
||||
\section{Exec}
|
||||
\begin{flushleft}
|
||||
Mit dem Befehl {\ttfamily exec} kann man Befehle ausf"uhren, ohne eine neue Shell / Process zu starten.\\
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Exec Example, label=lst:bash]
|
||||
uws@tux>bash
|
||||
uws@tux>pstree
|
||||
--gnome-terminal
|
||||
|-bash--bash--pstree
|
||||
|
||||
uws@tux>exec bash
|
||||
uws@tux>pstree
|
||||
--gnome-terminal
|
||||
|-bash--pstree
|
||||
\end{lstlisting}
|
||||
|
||||
\subsection{Ein-/Ausgabekanal}
|
||||
|
||||
In dem nachfolgenden Beispiel werden neue Eingabe- und Ausgabekan"ale definiert.\\
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoExec.sh, label=lst:bash]
|
||||
uws@tux>cat DemoExec.sh
|
||||
#!/bin/env bash
|
||||
#--------------------------------------------------------------------------
|
||||
#
|
||||
# Example to create File-Descriptors
|
||||
#
|
||||
#--------------------------------------------------------------------------
|
||||
#
|
||||
# Help
|
||||
printf "\nDemo create File-Descriptor.\n"
|
||||
printf "============================\n"
|
||||
printf "exec Ziffer>Ziel\t\tAusgabekanal anlegen\n"
|
||||
printf "exec Ziffer<Quele\t\tEingabekanal anlegen\n "
|
||||
printf "exec Ziffer<>Datei\tEin/Ausgabekanal anlegen\n"
|
||||
printf "Befehl >&Nr\t\t\t\t\tAusgabe auf Kanal Nr.\n"
|
||||
printf "exec Ziffer>&-\t\t\tAusgabkanal loeschen\n"
|
||||
printf "exec Ziffer<&-\t\t\tEingabekanal loeschen\n"
|
||||
printf "\nJeder angelegter Kanal muss separat geloescht werden.\n"
|
||||
printf "===================================================\n\n"
|
||||
|
||||
# Create test data
|
||||
printf "First string\nSecond string\nThird string\n " > data1.txt
|
||||
printf "one\ntwo\nthree\n" > data2.txt
|
||||
|
||||
# Create Ausgabekanal
|
||||
printf " Anlegen Ausgabekanal 3.\n"
|
||||
exec 3>data3.txt
|
||||
printf " Anlegen Eingabekanal 4.\n"
|
||||
exec 4<data1.txt
|
||||
printf " Anlegen Ein-/Ausgabekanal 5.\n"
|
||||
exec 5<>data5.txt
|
||||
|
||||
# Setzen Umleitung stdout und stderr
|
||||
exec >data6.txt 2>data.err
|
||||
echo "Das kommt in der Datei."
|
||||
|
||||
# Ausgabe in Kanal 3
|
||||
echo "Das kommt in den Kanal 3." >&3
|
||||
echo " Die einzelne Anweisung zur Umleitung ist nicht noetig."
|
||||
|
||||
# Erzeugen einer Fehlermeldung
|
||||
ls new.txt
|
||||
|
||||
# Auslesen Kanal 4 und schreiben auf Kanal 3-
|
||||
read A <&4
|
||||
echo $A >&3
|
||||
|
||||
# Benutze den Ein-/Ausgabekanal 5.
|
||||
tail -l <&5
|
||||
echo "Neue Zeile" >&5
|
||||
|
||||
# Schliessen der Kanaele
|
||||
exec 3>&-
|
||||
exec 4<&-
|
||||
exec 5>&-
|
||||
exec 5<&-
|
||||
|
||||
exit 0
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
48
Kapitel1/ExitCodes.tex
Executable file
@ -0,0 +1,48 @@
|
||||
\section{Exit Codes}
|
||||
\begin{flushleft}
|
||||
Jeder erfolgreicher oder auch nicht erfolgreicher Ausfruf eines Programms gibt einen Exit Code zur"uck.
|
||||
Dieser Exit Code kann mit {\ttfamily \$?} abgefragt werden. In einem Script kann man auch seleber Exit
|
||||
Codes definieren und zur"uckgeben.\\[2ex]
|
||||
|
||||
Eine Auswahl an Default Exit Codes sind in der nachfolgenden Tabelle ausgelistet.\\
|
||||
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{|l|p{8cm}|} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\hline \rowcolor{hellgrau}\emph{\textbf{Exit Code}} & \emph{\textbf{Beschreibung}} \\
|
||||
\hline 0 & Command wurde erfolgreich ausgef"uhrt\\
|
||||
\hline 1 & Command wurde nicht erfolgreich ausgef"uhrt\\
|
||||
\hline 2 & Empty Function\\
|
||||
\hline 126 & Command invoked cannot execute\\
|
||||
\hline 127 & Command nicht gefunden\\
|
||||
\hline 128 & Invalid Argument\\
|
||||
\hline 130 & Script terminated mit Control-C\\
|
||||
\hline 255 & Exit status out of range, nur 0 bis 255 sind erlaubt\\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\caption{Liste der Dateien}
|
||||
\end{table}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Exit Codes, label=lst:bash]
|
||||
uws@tux>cat exitcodes.sh
|
||||
#!/bin/env bash
|
||||
printf "\nHello World!\n\n"
|
||||
echo $? # return 0, command successfully
|
||||
|
||||
printfb "\nHello World!\n\n"
|
||||
echo $? # return 127, command not found
|
||||
|
||||
exit 21
|
||||
|
||||
uws@tux>./exitcodes.sh
|
||||
|
||||
Hello World
|
||||
|
||||
0
|
||||
./exitcodes.sh: Zeile 5: printfb: Kommando nicht gefunden.
|
||||
127
|
||||
|
||||
uws@tux>echo $?
|
||||
21
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
25
Kapitel1/Index.tex
Executable file
@ -0,0 +1,25 @@
|
||||
\chapter{Allgemein}
|
||||
|
||||
%-------------------------------------------
|
||||
% load other documents
|
||||
%------------------------------------------
|
||||
\input{Kapitel1/Variablen}
|
||||
%\include{Kapitel1/String}
|
||||
\newpage
|
||||
\input{Kapitel1/DatumBashScripte}
|
||||
\newpage
|
||||
\input{Kapitel1/AusgabeShell}
|
||||
\input{Kapitel1/SetBefehl}
|
||||
\newpage
|
||||
\input{Kapitel1/ParameterAuswerten}
|
||||
\input{Kapitel1/ShellScriptTesten}
|
||||
\newpage
|
||||
\input{Kapitel1/Tabs}
|
||||
\input{Kapitel1/BefehleMehrzeilig}
|
||||
\input{Kapitel1/BefehleVerketten}
|
||||
\newpage
|
||||
\input{Kapitel1/XARGS}
|
||||
\newpage
|
||||
\input{Kapitel1/SignaleTraps}
|
||||
\newpage
|
||||
\input{Kapitel1/Exec}
|
25
Kapitel1/ParameterAuswerten.tex
Executable file
@ -0,0 +1,25 @@
|
||||
\section{Programm Parameter auswerten}
|
||||
\begin{justify}
|
||||
M\"ochte man bei einem selbst erstellten Script einen Parameter \"ubergeben, so erfolgt die Auswertung in diesem Script mittels einer {\ttfamily case, if} oder {\ttfamily while} Schleife. Die auszuwertende Variablen fangen immer mit dem {\ttfamily \$} an. In der Variable {\ttfamily \$0} steht der Name des Scripts, inklusive des Verzeichnisses. Die Variablen {\ttfamily \$1 .. \$9} enthalten dann die Parameter zum Auswerten. In der Variable {\ttfamily \$\#} steht die Anzahl der \"ubergebenen Parameter. Die Variablen Nummer k\"onnen auch mit {\ttfamily \{\}} kenttlich ge,macht werden, wie zum Beispiel {\ttfamily \$\{1\}}.\\
|
||||
Kommandozeilen-Parameter werden mit {\ttfamily \$} abgefragt. In der nachfolgenden Tabelle ist eine Auflistung der wichtigsten Parameter.
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{3cm}p{13cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Parameter}} & \emph{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\$0 & Name des ausgef\"uhrten Shell Scripts, incl. des Pfades\\
|
||||
\$\# & Anzahl der \"ubergebenen Parameter\\
|
||||
\$1 \$2 \$3 ... & Erster, zweiter, dritter, ... Parameter\\
|
||||
\$* & Alle Kommandozeilen-Parameter (\$1 \$2 \$3 ...)\\
|
||||
\$@ & Wie \$*\\
|
||||
"\$@" & Expandiert zu: \$1 \$2 \$3 ...\\
|
||||
\$\$ & Prozessnummer der Shell\\
|
||||
\$- & Ausgabe der aktuellen Shell Optionen\\
|
||||
\$? & Ausgabe des Return-Codes von dem letzten ausgef\"uhrten Befehl\\
|
||||
\$! & Prozessnummer des zuletzt ausgef"uhrten Hintergrund Prozesses\\
|
||||
\$HOST & Ausgabe der Umgebungsvariable HOST\\
|
||||
\end{tabular}
|
||||
\caption{Parameter}
|
||||
\end{table}
|
||||
\end{justify}
|
||||
|
37
Kapitel1/SetBefehl.tex
Executable file
@ -0,0 +1,37 @@
|
||||
\section{Set-Befehl im Bash Script}
|
||||
\begin{flushleft}
|
||||
Der Befehl {\ttfamily set -\hspace{0.01cm}- ...} in einem Bash Script weist alle folgenden Argumenten den Variablen {\ttfamily \$1, \$2, \$3, ...} zu. Den Inhalt der letzten belegten Variable kann man dann mit {\ttfamily \${!\#}} auslesen.\\[2ex]
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Set]
|
||||
uws@tux>cat MeinScript.sh
|
||||
#!/bin/env bash
|
||||
# Script fuer den set Befehl
|
||||
#
|
||||
SOURCEPATH=/home/uws/Dateien
|
||||
set -- {SOURCEPATH}/bild_???.jpg # Einlesen der Dateien in $1, $2 usw
|
||||
lastname=${!#} # Letzter Name steht hier.
|
||||
echo "Lastname: ${lastname}" # Ausgabe auf dem Schirm
|
||||
\end{lstlisting}
|
||||
Mit {\ttfamily set -x} kann man in der Shell oder auch in einem Script sich die Verarbeitungsschritte
|
||||
sich anzeigen lassen. Mit {\ttfamily set +x} wird dieses wieder abgeschaltet. Der Parameter {\ttfamily -e} sorgt dafür, das jeder Fehler eines Kommandos innerhalb des Script für einen Abbruch sorgt. Wird der Parameter {\ttfamily -u} gesetzt, so müssen Variablen initialisiert werden. Und ein {\ttfamily -o pipefail} sorgt dafür, das Fehler innerhalb einer Pipe Anweisung verdeckt werden. Die verkürzte Schreibform ist {\ttfamily set -euxo pipefail}.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel pipefail]
|
||||
uws@tux>cat DemoSet.sh
|
||||
#!/usr/bin/env bash
|
||||
printf "Ohne set -o pipefail\n"
|
||||
grep hans uws.txt | sort
|
||||
printf "Return Code: $?\n"
|
||||
printf "Mit set -o pipefail\n"
|
||||
set -o pipefail
|
||||
grep hans uws.txt | sort
|
||||
printf "Return Code: $?\n"
|
||||
|
||||
uws@tux>./DemoSet.sh
|
||||
Ohne set -o pipefail
|
||||
grep: uws.txt: Datei oder Verzeichnis nicht gefunden
|
||||
Return Code: 0
|
||||
Mit set -o pipefail
|
||||
grep: uws.txt: Datei oder Verzeichnis nicht gefunden
|
||||
Return Code: 2
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
15
Kapitel1/ShellScriptTesten.tex
Executable file
@ -0,0 +1,15 @@
|
||||
\section{Shell Script Testen}
|
||||
\begin{flushleft}
|
||||
Ein Shell Script kann man vor einer Ausf"uhrung mit {\ttfamily sh <option>} testen. In der nachfolgenden Auflistung sind einige M"oglichkeiten beschrieben.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Script Testen, label=lst:bash]
|
||||
uws@tux># Syntax Test. Alle Kommandos werden gelesen, aber nicht ausgefuehrt
|
||||
uws@tux>sh -n <script_name>
|
||||
|
||||
uws@tux># Ausgabe der Shell Kommandos in der gelesenen Form
|
||||
uws@tux>sh -v <script_name>
|
||||
|
||||
uws@tux># Ausgabe der Shell Kommandos, nach der Durchfuehrung aller Ersetzungen
|
||||
uws@tux>sh -x <script_name>
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
130
Kapitel1/SignaleTraps.tex
Executable file
@ -0,0 +1,130 @@
|
||||
\section{Signale (Traps)}
|
||||
\begin{justify}
|
||||
Signale, wie z.B. ein {\ttfamily Ctrl + c} kann man in einem Programm abfangen. Hierzu wird in dem Programm ein entsprechender {\ttfamily trap} definiert.\\
|
||||
Syntax: {\ttfamily trap 'action' signal}\\
|
||||
Als Signal kann entweder der Name oder die Nummer angegeben werden. Es k"onnen auch mehrere Signale f"ur eine Aktion angegeben werden. Soll ein Signal ignoriert werden, so wird bei {\ttfamily Action} nichts angegeben.
|
||||
Die nachfolgende Liste listet die f"ur ein Programm relevanten Traps auf.\\
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{2cm}p{1cm}p{13cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Signal}} & \emph{\textbf{Nr.}} & \emph{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
SIGHUB & 1 & Das Programm h"angt\\
|
||||
SIGINT & 2 & Ein Ctrl + C wurde gemacht\\
|
||||
SIGQUIT & 3 & Sendet ein Ctrl + D\\
|
||||
SIGFPE & 8 & Eine illegale mathematische Operation wurde gemacht\\
|
||||
SIGKILL & 9 & Der Prozess wird mit kill beendet\\
|
||||
SIGALRM & 14 & Alarm Clock (f"ur Timer)\\
|
||||
SIGTERM & 15 & Der Prozess wird mit kill beendet\\
|
||||
\end{tabular}
|
||||
\caption{Auswahl Traps}
|
||||
\end{table}
|
||||
Eine vollst"andige Liste erh"alt man, wenn man in der Console ein {\ttfamily kill -l} eingibt.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Alle Traps, label=lst:bash]
|
||||
uws@tux>kill -l
|
||||
1) SIGHUB 2) SIGINT 3) SIGQUIT 4) SIGILL
|
||||
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
|
||||
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
|
||||
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
|
||||
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
|
||||
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
|
||||
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
|
||||
29) SIGIO 30) SIGPWR 31) SIGSYS 32) SIGRTMIN
|
||||
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
|
||||
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+4
|
||||
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+4
|
||||
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
|
||||
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
|
||||
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
|
||||
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
|
||||
63) SIGRTMAX-1 64) SIGRTMAX
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
Einige Beispiele, um Traps in einem Script abzufangen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=trap1.sh, label=lst:bash]
|
||||
uws@tux>cat trap1.sh
|
||||
#!/bin/env bash
|
||||
# Funktion trap zum Abfangen von Signalen
|
||||
# Name: trap1.sh
|
||||
# Signal SIGINT (2) (Strg + C)
|
||||
trap 'echo SIGINT erhalten' 2
|
||||
|
||||
i=0
|
||||
while [ $1 -lt 5 ]
|
||||
do
|
||||
echo "Habe Zeit, warte ein wenig!"
|
||||
sleep 2
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
\end{lstlisting}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=trap2.sh, label=lst:bash]
|
||||
uws@tux>cat trap2.sh
|
||||
#!/bin/env bash
|
||||
# Funktion trap zum Abfangen von Signalen
|
||||
# Hier werden nun mehrere Signale abgefangen
|
||||
# Name: trap2.sh
|
||||
# Signal SIGINT (2) (Strg + C)
|
||||
trap 'echo SIGINT erhalten' 2
|
||||
# Signal SIGTERM (15) kill -TERM PID_of_trap2
|
||||
trap 'echo SIGTERM erhalten' 15
|
||||
|
||||
i=0
|
||||
while [ $1 -lt 5 ]
|
||||
do
|
||||
echo "Warte noch immer! ($$)"
|
||||
sleep 5
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
\end{lstlisting}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=trap3.sh, label=lst:bash]
|
||||
uws@tux>cat trap3.sh
|
||||
#!/bin/env bash
|
||||
# Funktion trap zum Abfangen von Signalen
|
||||
# Hier werden nun mehrere Signale abgefangen
|
||||
# Name: trap3.sh
|
||||
# Signal SIGINT und SIGTERM abfangen
|
||||
trap 'echo SIGINT / SIGTERM erhalten' 2 15
|
||||
|
||||
i=0
|
||||
while [ $1 -lt 5 ]
|
||||
do
|
||||
echo "Das dauert aber! ($$)"
|
||||
sleep 5
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=trap4.sh, label=lst:bash]
|
||||
uws@tux>cat trap4.sh
|
||||
#!/bin/env bash
|
||||
# Funktion trap zum Abfangen von Signalen
|
||||
# Eine Funktion (Signalhandler) einrichten
|
||||
# Name: trap4.sh
|
||||
sighandler_INT() {
|
||||
printf "Habe das Signal SIGINT empfangen.\n"
|
||||
printf "Soll das Script beendet werden? (j/n) : "
|
||||
read
|
||||
if [[ $REPLY = "j" ]]
|
||||
then
|
||||
echo "Good by!"
|
||||
exit 0;
|
||||
fi
|
||||
}
|
||||
|
||||
# Signal SIGINT abfangen
|
||||
trap 'sighandler_INT' 2
|
||||
|
||||
i=0
|
||||
while [ $1 -lt 5 ]
|
||||
do
|
||||
echo "Bitte nicht stören! ($$)"
|
||||
sleep 5
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
\end{lstlisting}
|
||||
\end{justify}
|
25
Kapitel1/String.tex
Executable file
@ -0,0 +1,25 @@
|
||||
\section{String-Operationen}
|
||||
\begin{justify}
|
||||
In der nachfolgenden Tabelle werden wichtige String-Operationen aufgelistet.
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{4cm}p{12cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Operation}} & \emph{\textbf{Bedeutung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\verb|${#var}| & Länge von \verb|${var}| (Variablenwert) in Zeichen\\
|
||||
\verb|${var:-text}| & "text", wenn var leer ist oder nicht existiert, sonst \verb|${var}|\\
|
||||
\verb|${var:+text}| & "", falls var leer ist oder nicht existiert, sonst "text"\\
|
||||
\verb|${var:=text}| & var="text" falls var leer ist oder nicht existiert, sonst \verb|${var}|\\
|
||||
\verb|${var:offset:länge}| & \verb|${var}| ab Position offset vom Anfang (wenn negativ: vom Ende) mit (optional) länge Zeichen\\
|
||||
\verb|${var:#muster}| & \verb|${var}| ohne muster am Anfang (minimale Übereinstimmung)\\
|
||||
\verb|${var:##muster}| & \verb|${var}| ohne muster am Anfang (maximale Übereinstimmung)\\
|
||||
\verb|${var:%muster}| & \verb|${var}| ohne muster am Ende (minimale Übereinstimmung)\\
|
||||
\verb|${var:%%muster}| & \verb|${var}| ohne muster am Ende (maximale Übereinstimmung)\\
|
||||
\verb|${var/muster}| & \verb|${var}| ohne das erste muster\\
|
||||
\verb|${var//muster}| & \verb|${var}| ohne alle muster\\
|
||||
\verb|${var/muster/text}| & \verb|${var}|, wobei das erste muster durch text ersetzt wurde\\
|
||||
\verb|${var//muster/text}| & \verb|${var}|, wobei alle muster durch text ersetzt wurden\\
|
||||
\end{tabular}
|
||||
\caption{String-Operationen}
|
||||
\end{table}
|
||||
\end{justify}
|
16
Kapitel1/Tabs.tex
Executable file
@ -0,0 +1,16 @@
|
||||
\section{Tabs setzten}
|
||||
\begin{justify}
|
||||
Die Abst"ande f"ur die Tabs kann man mit {\ttfamily tabs <wert>} setzten. Sie gelten dann f"ur die Console und auch in den Scripten.
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{1cm}p{15cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Wert}} & \emph{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
-0 & L"oscht alle Tabs\\
|
||||
-8 & Setzt die default Werte\\
|
||||
+4 & Tabs mit dem Abstand von 4 setzten\\
|
||||
+2,6,12 & Tabs mit den Abst"anden 2,6,12 setzten\\
|
||||
\end{tabular}
|
||||
\caption{Tabs Werte}
|
||||
\end{table}
|
||||
\end{justify}
|
BIN
Kapitel1/Variablen.pdf
Normal file
270
Kapitel1/Variablen.tex
Executable file
@ -0,0 +1,270 @@
|
||||
\section{Variablen}
|
||||
\subsection{Definition}
|
||||
\begin{justify}
|
||||
%\begin{flushleft}
|
||||
Jede Variable hat einen Namen und einen Wert. Typen kennt die Bash nur eingeschr\"angt. Um eine Variable in einem Bash Script zu definieren, kann der Befehl {\ttfamily declare\index{declare}} benutzt werden.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiele]
|
||||
uws@tux>wert=3+7
|
||||
uws@tux>echo $wert
|
||||
3+7
|
||||
|
||||
uws@tux>declare -i wert
|
||||
uws@tux>echo $wert
|
||||
3+7
|
||||
|
||||
uws@tux>wert=3+7
|
||||
uws@tux>echo $wert
|
||||
10
|
||||
|
||||
uws@tux>declare -u choise
|
||||
uws@tux>choise=yes
|
||||
uws@tux>echo $chaoise
|
||||
YES
|
||||
|
||||
uws@tux>declare -l choise
|
||||
uws@tux>choise=YES
|
||||
uws@tux>echo $choise
|
||||
yes
|
||||
\end{lstlisting}
|
||||
Hier eine kurze Liste der Typen.
|
||||
\begin{longtable}[l]{p{1.5cm}p{14.5cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Attribut}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Attribut}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Attribute} \\
|
||||
\endlastfoot
|
||||
-p & Zeigt alle Attribute aller Variablen an\\
|
||||
-i & Arithmetische Auswertung\\
|
||||
-a & Array Index\\
|
||||
-A & Array Namen\\
|
||||
-u & Der Wert wird in Gro"ss buchstaben umgewandelt\\
|
||||
-l & Der Wert wird in Kleinbuchstaben umgewandelt\\
|
||||
-r & Nach der Zuweisung kann dieser Wert nicht mehr ver"andert werden, sie ist dann eine Konstante\\
|
||||
\end{longtable}
|
||||
\newpage
|
||||
Weitere Informationen hierzu gibt es auf der Seite \url{http://tldp.org/LDP/abs/html/declareref.html}.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Zuweisung mittels Befehl, label=lst:bash]
|
||||
uws@tux>strMachine="Computer Name: `uname -a | cut -d ' ' -f 2`"
|
||||
uws@tux>echo $strMachine
|
||||
Computer Name: tux
|
||||
\end{lstlisting}
|
||||
Die auszuf\"uhrenden Befehle stehen zwischen R"uckwertigen Hochkommas.\\
|
||||
Auch mit {\ttfamily typeset} werden Variablen mit einem Typ definiert. Standardm\"assig werden auch hier Variablen ohne Angabe des Typs als {\ttfamily string} Variable deklariert. Die Angabe eines Wertes ist optional.\\
|
||||
Die Syntax f"ur {\ttfamily typeset} ist:\\[1ex]
|
||||
{\ttfamily typset [option] [variable] [=wert]}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel typeset, label=lst:bash]
|
||||
uws@tux>cat example1.sh
|
||||
#!/bin/env bash
|
||||
typeset -i var1=2 # Variable var1 als integer mit einem Wert
|
||||
|
||||
typeset +i var1 # Abschalten der definition
|
||||
\end{lstlisting}
|
||||
\begin{longtable}[l]{p{1.5cm}p{14.5cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Typeset Options} \\
|
||||
\endlastfoot
|
||||
-a & Array\\
|
||||
-i & Integer\\
|
||||
-r & Konstante (read only)\\
|
||||
-x & Varaible exportieren\\
|
||||
-f & Zeigt Funktionen mit ihrer Definition an\\
|
||||
-fx & Exportiert eine Funktion\\
|
||||
-F & Zeigt Funktionen ohne ihre Definition an\\
|
||||
\end{longtable}
|
||||
%-------------------------------------------------------------------------------
|
||||
% Variablen laenge
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{Variablen l\"ange}
|
||||
Die L\"ange eines Strings, die einer Variable zugewiesen worden ist, kann man folgenderma\ss en ermitteln.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Variable l\"ange]
|
||||
uws@tux>cat varlength.sh
|
||||
#!/bin/bash
|
||||
A="$1" X=${#A}
|
||||
echo -n "*" # Ausgabe ohne newline
|
||||
printf " Laenge der Variable A: ${X}\n"
|
||||
|
||||
uws@tux>./varlength.sh Weihnachten
|
||||
* Laenge der Variable A: 11
|
||||
\end{lstlisting}
|
||||
%-------------------------------------------------------------------------------
|
||||
% Variablen default
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{Variablen default}
|
||||
Falls ein Parameter / Variable nicht gesetzt wird, so kann man ein Default Wert f"ur diese Variable setzen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Variable default]
|
||||
uws@tux>cat varDefault.sh
|
||||
#!/bin/bash
|
||||
MinSize=${1:-50M}
|
||||
printf "Minimum Size: $MinSize\n"
|
||||
|
||||
uws@tux>./varDefault.sh
|
||||
Minimum Size: 50M
|
||||
|
||||
uws@tux>./varDefault.sh 125M
|
||||
Minimum Size: 125M
|
||||
\end{lstlisting}
|
||||
%-------------------------------------------------------------------------------
|
||||
% Variablen Warnung
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{Variablen Warnung}
|
||||
Wenn ein Parameter / Variable nicht definiert wird, so wird dann eine Message ausgegeben. Nicht interktive Shells terminieren anschlie"send.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Variable Warnung]
|
||||
uws@tux>cat varWarnung.sh
|
||||
#!/bin/bash
|
||||
FileName=${1:?Missing filename.}
|
||||
printf "File Name: $FileName\n"
|
||||
|
||||
uws@tux>./varWarnung.sh
|
||||
varWarnung.sh: Zeile 2: 1: Missing filename.
|
||||
|
||||
uws@tux>./varWarnung.sh Mail.log
|
||||
File Name: Mail.log
|
||||
\end{lstlisting}
|
||||
%-------------------------------------------------------------------------------
|
||||
% Array
|
||||
%-------------------------------------------------------------------------------
|
||||
\newpage
|
||||
\subsection{Array}
|
||||
Ein Array wird mit {\ttfamily declare -a} oder mit {\ttfamily declare -A} definiert. Bei der ersten Zuweisung werden die Werte mit einer Indexnummer versehen und bei der zweiten Zuweisung kann man einen Bezeichner selbst "ubergeben. Die Syntax f"ur die Zuweisung von Werten ist folgenderma\ss en.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Syntax Array]
|
||||
declare -a <arrayname>
|
||||
arrayname=(wert1 wert2 wert3)
|
||||
arrayname[indexnr]=wert
|
||||
\end{lstlisting}
|
||||
Wird keine Index Nummer bei der Zuweisung der Werte angegeben, so startet die Index Nummer bei {\ttfamily 0} f\"ur den ersten Wert.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiele Array]
|
||||
uws@tux>declare -a MyUser=('Harry' 'Paul' 'Walter')
|
||||
uws@tux>for ((i=0;i<=2;i++)); do echo User: ${MyUser[$i]}; done
|
||||
User: Harry
|
||||
User: Paul
|
||||
User: Walter
|
||||
|
||||
uws@tux>echo ${MyUser[@]}
|
||||
Harry Paul Walter
|
||||
|
||||
uws@tux>echo ${!MyUser[@]}
|
||||
0 1 2
|
||||
|
||||
uws@tux># Anstelle von @ kann auch ein * genommen werden
|
||||
uws@tux>echo ${#MyUser[*]}
|
||||
3
|
||||
|
||||
uws@tux>for i in ${!MyUser[*]}; do echo User: ${MyUser[$i]}; done
|
||||
User: Harry
|
||||
User: Paul
|
||||
User: Walter
|
||||
|
||||
uws@tux>#Wert hinzufuegen zum array
|
||||
uws@tux>MyUser+=('Andrea' 'Silvia')
|
||||
uws@tux>echo ${MyUser[@]}
|
||||
Harry Paul Walter Andrea Silvia
|
||||
|
||||
uws@tux>#Loeschen von Werten vom array
|
||||
uws@tux>unset -v MyUser[2]
|
||||
uws@tux>echo ${MyUser[@]}
|
||||
Harry Paul Andrea Silvia
|
||||
|
||||
uws@tux>declare -A MyText[Start]='Starts here'
|
||||
uws@tux>MyText['End Text']='The Last one'
|
||||
uws@tux>echo ${MyValue['End Text']} - ${MyValue[Start]}
|
||||
The Last one - Starts here
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
\subsection{Gro\ss - / Kleinbuchstaben}
|
||||
Seit der Bash Version 4 kann man ganz einfach den Wert einer Variable in Gro\ss - oder in Kleinbuchstaben umwandeln. Es k"onnen der erste oder alle Buchstaben umgewandelt werden. Mit {\ttfamily -u} wandelt alle Buchstaben in Gro\ss buchstaben um und ein {\ttfamily -l} wandelt alle Buchstaben in Kleinschreibung um.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Gro\ss - / Kleinbuchstaben]
|
||||
uws@tux># Umwandeln in Gro\ss buchstaben
|
||||
uws@tux>declare -u Wert1
|
||||
uws@tux>Wert1=klein
|
||||
uws@tux>echo $Wert1
|
||||
KLEIN
|
||||
|
||||
uws@tux># Umwandeln in Kleinbuchstaben
|
||||
uws@tux>declare -l Wert2
|
||||
uws@tux>Wert2=GROSS
|
||||
uws@tux>echo $Wert2
|
||||
gross
|
||||
\end{lstlisting}
|
||||
Soll der erste Buchstabe umgewandelt werden oder alle Buchstaben, kann man das folgenderma"sen machen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Werte umwandeln]
|
||||
uws@tux>an=paulchen
|
||||
uws@tux>echo ${an^}
|
||||
Paulchen
|
||||
uws@tux>echo ${an^^}
|
||||
PAULCHEN
|
||||
uws@tux>echo ${an^^c}
|
||||
paulChen
|
||||
|
||||
uws@tux>PAULCHEN
|
||||
uws@tux>echo ${an,}
|
||||
pAULCHEN
|
||||
uws@tux>echo ${an,,}
|
||||
paulchen
|
||||
uws@tux>echo ${an,,U}
|
||||
PAuLCHEN
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
%-------------------------------------------------------------------------------
|
||||
% Subsection: String-Operationen
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{String-Operationen}
|
||||
In der nachfolgenden Tabelle werden die String-Operationen aufgelistet.
|
||||
\begin{longtable}[l]{p{4.0cm}p{12.0cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Operation}} & \multicolumn {1}{l}{\textbf{Bedeutung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Operation}} & \multicolumn {1}{l}{\textbf{Bedeutung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{String-Operationen} \\
|
||||
\endlastfoot
|
||||
\verb|${#var}| & L"ange von \verb|${var}| (Variablenwert) in Zeichen \\
|
||||
\verb|${var:-text}| & "text, wenn var leer oder nicht existiert, sonst \verb|${var}| \\
|
||||
\verb|${var:+text}| & \verb|""|, falls var leer oder nicht existiert, sonst "text" \\
|
||||
\verb|${var:=text}| & var="text" falls var leer oder nicht existiert \\
|
||||
\verb|${var:offset:länge}| & \verb|${var}| ab Position offset vom Anfang (wenn negativ: vom Ende) mit (optional) l"ange Zeichen \\
|
||||
\verb|${var:#muster}| & \verb|${var}| ohne muster am Anfang (minimale "Ubereinstimmung) \\
|
||||
\verb|${var:##muster}| & \verb|${var}| ohne muster am Anfang (maximale "Ubereinstimmung) \\
|
||||
\verb|${var:%muster}| & \verb|${var}| ohne muster am Anfang (minimale "Ubereinstimmung) \\
|
||||
\verb|${var:%%muster}| & \verb|${var}| ohne muster am Anfang (maximale "Ubereinstimmung) \\
|
||||
\verb|${var/muster}| & \verb|${var}| ohne das erste muster \\
|
||||
\verb|${var//muster}| & \verb|${var}| ohne alle muster \\
|
||||
\verb|${var/muster/text}| & \verb|${var}|, wobei das erste muster durch text ersetzt wurde \\
|
||||
\verb|${var//muster/text}| & \verb|${var}|, wobei alle muster durch text ersetzt wurden \\
|
||||
\end{longtable}
|
||||
|
||||
%\end{flushleft}
|
||||
\end{justify}
|
50
Kapitel1/Vorlage.tex
Executable file
@ -0,0 +1,50 @@
|
||||
\section{Bash}
|
||||
\begin{flushleft}
|
||||
kann dann mit {\ttfamily source ./.bashrc} geladen werden.%\\[2ex]
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel .bashrc, label=lst:bash]
|
||||
# define alias
|
||||
alias dir='ls --color=auto --format=vertical
|
||||
alias ls='ls -lahF --color=tty'
|
||||
alias df='df -hT'
|
||||
alias du='du -sch'
|
||||
# define variables
|
||||
strBackupPach=/backup/tuxserver
|
||||
strBackupLog=backup.log
|
||||
export strBackupPath strBackupLog
|
||||
export GREP\_OPTIONS='- -color=auto'
|
||||
export GREP\_COLOR='1;32' # Ansi color light green
|
||||
# Functions
|
||||
back() {
|
||||
cd $strBackupPath
|
||||
vi $strBackupLog
|
||||
}
|
||||
# Berechtigungen setzen f"uer neuangelegte Objekte\\
|
||||
umask 022
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{flushleft}
|
||||
In der nachfolgenden Tabelle werden die Konfigurationsdateien aufgelistet, die bei einem Login/Aufruf einer Shell in der Reihenfolge verarbeitet werden.\\[2ex]
|
||||
|
||||
%
|
||||
% Hier kommt eine Tabelle, 4 Spalten
|
||||
%
|
||||
% \emph und \textbf => Kursiv und Fett
|
||||
%
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{|l|l|l|p{8cm}|} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\hline \rowcolor{hellgrau}\emph{\textbf{Konfigurations}} & \emph{\textbf{Login}} & \emph{\textbf{Interaktive}} & \ \\
|
||||
\rowcolor{hellgrau}\emph{\textbf{Datei}} & \emph{\textbf{Shell}} & \emph{\textbf{Shell}} & \emph{\textbf{Beschreibung}}\\
|
||||
\hline /etc/profile & X & - & Systemweit, wird bei einem Update "uberschrieben.\\
|
||||
\hline /etc/profile.local & X & - & Systemweit, bleibt bei einem Update erhalten.\\
|
||||
\hline /etc/bash.bashrc & X & - & Systemweit, wird bei einem Update "uberschrieben.\\
|
||||
\hline \~/.bashrc & X & X & Benutzerkonfiguration.\\
|
||||
\hline \~/.alias & X & X & Benutzerkonfiguration. In dieser Datei k"onnen die Alias verwaltet werden.\\
|
||||
\hline \~/.bash\_profile & X & X & Benutzerkonfiguration Wird eine der Dateien\\
|
||||
\hline \~/.bash\_login & X & X & gefunden, so wird diese verarbeitet, auch wenn\\
|
||||
\hline \~/.profile & X & X & die anderen Dateien vorhanden sind.\\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\caption{Liste der Dateien}
|
||||
\end{flushleft}
|
58
Kapitel1/XARGS.tex
Executable file
@ -0,0 +1,58 @@
|
||||
\section{XARGS}
|
||||
\begin{flushleft}
|
||||
Das Programm {\ttfamily xargs} wird mit Pipes benutzt und dabei werden alle Standardeingaben gelesen
|
||||
und als Argumente an ein anderes Programm weitergegeben. Nachfolgend sind ein paar Beispiele,
|
||||
die den Einsatz von {\ttfamily xargs} zeigen.\\[2ex]
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiele xargs, label=lst:bash]
|
||||
uws@tux>echo 1 2 3 4 5 6 | xargs
|
||||
1 2 3 4 5 6
|
||||
|
||||
uws@tux>echo 1 2 3 4 5 6 | xargs -n 2
|
||||
1 2
|
||||
3 4
|
||||
5 6
|
||||
|
||||
uws@tux>echo 1 2 3 4 5 6 | xargs -p -n 2
|
||||
echo 1 2 ?... y
|
||||
1 2
|
||||
echo 3 4 ?... n
|
||||
echo 5 6 ?... y
|
||||
5 6
|
||||
|
||||
uws@tux>ls *.sh | xargs -n 1 echo File:
|
||||
File: Backup.sh
|
||||
File: Restore.sh
|
||||
\end{lstlisting}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiele xargs, label=lst:bash]
|
||||
uws@tux>cat inhalt.txt
|
||||
Dieser Text steht in einer Zeile
|
||||
|
||||
uws@tux>xargs --arg-file=inhalt.txt -n 1
|
||||
Dieser
|
||||
Text
|
||||
steht
|
||||
in
|
||||
einer
|
||||
Zeile
|
||||
|
||||
uws@tux>cut -d: -f1 /etc/passwd | sort | xargs -n 1 echo User:
|
||||
User: at
|
||||
User: avahi
|
||||
User: bin
|
||||
\end{lstlisting}
|
||||
|
||||
L"oschen von Dateien mit einer Abfrage vor dem l"oschen. Damit auch Dateien gel"oscht werden, die
|
||||
ein Leerzeichen enthalten, wrd bei {\ttfamily find} die Option {\ttfamily -print0} und bei
|
||||
{\ttfamily xargs} die Option {\ttfamily -0} gesetzt.\\
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiele Delete mti xargs, label=lst:bash]
|
||||
uws@tux>find . -name "*.sh" -print0 | xargs -0 -p -n 1 rm -rf
|
||||
rm -rf ./Backup.sh ?... y
|
||||
rm -rf ./Restore.sh ?... n
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
30
Kapitel2/CaseAnweisung.tex
Executable file
@ -0,0 +1,30 @@
|
||||
\section{Case Anweisung}
|
||||
\begin{flushleft}
|
||||
Mir der Anweisung {\ttfamily case} k"onnen die Kommandozeilen Parameter ausgewertet werden. Das Ende
|
||||
einer Auswertung wird mit einem doppelten Semikolon abgeschlossen, da ein Semikolon als Trennzeichen
|
||||
f"ur Kommandos gilt. Eine {\ttfamily case} Anweisung wird mit einem {\ttfamily esac} (end case) abgeschlossen.\\
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel case, label=lst:bash]
|
||||
#!/bin/env bash
|
||||
#Als erstes wird der Parameter uebergeben
|
||||
action=$1
|
||||
|
||||
#Hier faengt die Auswertung and
|
||||
case "$action" in
|
||||
-a)
|
||||
<Befehle>
|
||||
;;
|
||||
-v)
|
||||
<Befehle>
|
||||
;;
|
||||
|
||||
#Mehrfachauswertung
|
||||
-j*|-J*|-y*|-Y*)
|
||||
<Befehle> ;;
|
||||
*)
|
||||
echo " Usage: uschi.sh [-a] [-v]"
|
||||
esac
|
||||
exit 0
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
158
Kapitel2/ForSchleife.tex
Executable file
@ -0,0 +1,158 @@
|
||||
\section{For Schleife}
|
||||
\begin{flushleft}
|
||||
|
||||
Die {\ttfamily for} Schleife erh"alt eine Liste von Werten und f"uhrt die Kommandos mit jedem
|
||||
Wert aus. M"ochte man eine Schleife abbrechen, so wird hierzu der Befehl {\ttfamily break [n]}
|
||||
benutzt. Um wieder am Anfang zu springen, benutz man den Befehl {\ttfamily continue [n]}.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel For Schleife]
|
||||
uws@tux>cat example.sh
|
||||
#!/bin/env bash
|
||||
for a in $1
|
||||
do
|
||||
printf "\n\tUebergabe Wert: ${a}\n"
|
||||
done
|
||||
\end{lstlisting}
|
||||
Hier ein Beispiel f"ur {\ttfamily break / continue}.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Break / Continue]
|
||||
uws@tux>cat example1.sh
|
||||
#!/bin/env bash
|
||||
for a in 1 2 3 4 5
|
||||
do
|
||||
if [ ${a} -eq 2 ]
|
||||
then
|
||||
continue
|
||||
fi
|
||||
if [ ${a} -eq 4 ]
|
||||
then
|
||||
break
|
||||
fi
|
||||
printf "\n\tWerte: ${a}\n"
|
||||
done
|
||||
\end{lstlisting}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Example2.sh]
|
||||
uws@tux>cat example2.sh
|
||||
#!/bin/env bash
|
||||
i=1
|
||||
wochentage="Montag Dienstag Mittwoch Donnerstag Freitag"
|
||||
for tage in ${wochentage}
|
||||
do
|
||||
printf "\n\tWochentag $((i++)) : ${tage}\n"
|
||||
done
|
||||
|
||||
uws@tux>./example2.sh
|
||||
Wochentag 1 : Montag
|
||||
Wochentag 2 : Dienstag
|
||||
Wochentag 3 : Mittwoch
|
||||
Wochentag 4 : Donnerstag
|
||||
Wochentag 5 : Freitag
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Example3.sh]
|
||||
uws@tux>cat example3.sh
|
||||
#!/bin/env bash
|
||||
i=1
|
||||
cd ~/bin
|
||||
for item in *
|
||||
do
|
||||
printf "\n\tItem $((i++)) : ${item}\n"
|
||||
done
|
||||
|
||||
uws@tux>./example3.sh
|
||||
Item 1 : example.sh
|
||||
Item 2 : example1.sh
|
||||
Item 3 : example2.sh
|
||||
Item 4 : example3.sh
|
||||
\end{lstlisting}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Example4.sh]
|
||||
uws@tux>cat example4.sh
|
||||
#!/bin/env bash
|
||||
i=1
|
||||
for file in /etc/[abcd]*.conf
|
||||
do
|
||||
printf "\n\tFile $((i++)) : ${file}\n"
|
||||
done
|
||||
|
||||
uws@tux>./example4.sh
|
||||
File 1 : /etc/alsa-pulse.conf
|
||||
File 2 : /etc/asound-pulse.conf
|
||||
File 3 : /etc/blkid.conf
|
||||
File 4 : /etc/dhcpclient.conf
|
||||
\end{lstlisting}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Example5.sh]
|
||||
uws@tux>cat example5.sh
|
||||
#!/bin/env bash
|
||||
i=1
|
||||
for file in $(ls ~/bin/*.sh)
|
||||
do
|
||||
printf "\n\tFile $((i++)) : ${file}\n"
|
||||
done
|
||||
|
||||
uws@tux>./example5.sh
|
||||
File 1 : /home/uws/bin/example.sh
|
||||
File 2 : /home/uws/bin/example1.sh
|
||||
File 3 : /home/uws/bin/example2.sh
|
||||
File 4 : /home/uws/bin/example3.sh
|
||||
File 5 : /home/uws/bin/example4.sh
|
||||
File 6 : /home/uws/bin/example5.sh
|
||||
\end{lstlisting}
|
||||
Beispiel einer {\ttfamily for} Schleife in einer Zeile.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Eine Zeile]
|
||||
uws@tux>for a in 01 02 03; do ls bild${a}.jpg; done
|
||||
bild01.jpg
|
||||
bild02.jpg
|
||||
bild03.jpg
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
In dem n"achsten Beispiel wird automatisch hochgez"ahlt und in Schritten.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=example6.sh]
|
||||
uws@tux>cat example6.sh
|
||||
#!/bin/env bash
|
||||
for ((i=1;i<=5;i++)); do
|
||||
echo "$i: "
|
||||
done
|
||||
|
||||
#von 1 bis 10
|
||||
for i in {1..10}
|
||||
do
|
||||
printf "Number: $1\n"
|
||||
done
|
||||
|
||||
#von 1 bis 10, aber in 2er Schritten
|
||||
for i in {1..10..2}
|
||||
do
|
||||
printf "Number: $i\n"
|
||||
done
|
||||
\end{lstlisting}
|
||||
Nun wird eine Datei eingelesen und nur einzelne Spalten werden ausgegeben.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=example7.sh]
|
||||
uws@tux>cat example7.sh
|
||||
for SHARE in $(grep -i "nfs" /etc/fstab | cut -d' ' -f2)
|
||||
do
|
||||
printf "\nShare to mount: $SHARE"
|
||||
done
|
||||
\end{lstlisting}
|
||||
Die Ausgabe erfolgt mittels einer Array-Variable.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=example8]
|
||||
uws@tux>declare -a MyUser=('uws' 'paul' 'Hans')
|
||||
|
||||
uws@tux>for ((i=0;i<=2;i++));do echo User: ${MyUser[$i]};done
|
||||
User: uws
|
||||
User: paul
|
||||
User: Hans
|
||||
|
||||
uws@tux>for i in ${!MyUser[*]};do printf "User: ${MyUser[$i]};done
|
||||
User: uws
|
||||
User: paul
|
||||
User: Hans
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
16
Kapitel2/Functions.tex
Executable file
@ -0,0 +1,16 @@
|
||||
\section{Functions}
|
||||
\begin{flushleft}
|
||||
|
||||
In einem Bash Script m"ussen die {\ttfamily functions} an Anfang definiert werden, da sie sonst nicht g"ultig sind. {\ttfamily Functions} k"onnen auch in einer separaten Datei stehen, die dann mit per {\ttfamily ./<pfad>/<datei>} geladen werden. Mit {\ttfamily source <pfad/datei>} kann man sie Datei auch laden. Die Angabe von {\ttfamily function} vor dem Namen ist nicht zwingend, man kann sie auch weglassen.\\
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=ExampleFunc.sh, label=lst:bash]
|
||||
uws@tux>cat ExampleFunc.sh
|
||||
#!/bin/env bash
|
||||
function <name>()
|
||||
{
|
||||
<Befehl>
|
||||
<Befehl>
|
||||
}
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
141
Kapitel2/IfAnweisung.tex
Executable file
@ -0,0 +1,141 @@
|
||||
\section{If Anweisung}
|
||||
\subsection{Allgemein}
|
||||
\begin{flushleft}
|
||||
M"ochte man eine Bedingung abfragen, so nutzt man hierzu eine {\ttfamily if} Abfrage. Die allgemeine Form der {\ttfamily if} Abfrage ist:\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Syntax]
|
||||
if Bedingung
|
||||
then Anweisung
|
||||
else Anweisung
|
||||
fi
|
||||
\end{lstlisting}
|
||||
Au\ss erdem k"onnen mehrere {\ttfamily else if} Abfragen erstellt werden. Der Befehl hierzu ist {\ttfamily elif}.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Syntax elif]
|
||||
if Bedingung1
|
||||
then Anweisung
|
||||
elif Bedingung2
|
||||
then Anweisung
|
||||
else Anweisung
|
||||
fi
|
||||
\end{lstlisting}
|
||||
Mehrere Vergleiche mit {\ttfamily und} oder mit {\ttfamily oder} kann folgenderma{\ss}en gemacht werden.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Meherer Vergleich]
|
||||
# || => oder
|
||||
if [[ "$Text1" == Connected* || "$Text1" == Verbunden ]]
|
||||
|
||||
# && => und
|
||||
if [[ "$Text1" == Connected* && "$Text1" == Verbunden ]]
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
%-------------------------------------------------------------------------------
|
||||
% Subsection: Beispiele
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{Beispiele}
|
||||
Um abzufragen, ob eine Datei vorhanden ist, so nutzt man hierzu die Option {\ttfamily test -r}.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Datei existiert]
|
||||
uws@tux>cat exist.sh
|
||||
#!/bin/env bash
|
||||
if test -r /home/uws/scripts/sicherung.sh
|
||||
then
|
||||
bash /home/uws/scripts/startmount.sh
|
||||
fi
|
||||
\end{lstlisting}
|
||||
Handelt es sich nicht um eine Datei, sondern um ein Verzeichnis, so wird hierzu die Option {\ttfamily -d} verwendet. Die Option {\ttfamily -L} pr"uft, ob es sich um ein symbolischen Link handelt. Mit der Option {\ttfamily -f} wird "uberpr"uft, ob es sich um eine gew"ohnliche Datei handelt. Man kann mit {\ttfamily test} auch mehrere "Uberpr"ufungen ausf"uhren. Daf"ur wird die Option {\ttfamily -a} (f"ur und) und die Option {\ttfamily -o} (f"ur oder) benutzt.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Pfad check]
|
||||
uws@tux>cat exist1.sh
|
||||
#!/bin/env bash
|
||||
if test -d /home/uws/scripts -a -f /home/uws/scripts/MyBackup.conf
|
||||
then
|
||||
bash /home/uws/scripts/startmount.sh
|
||||
fi
|
||||
\end{lstlisting}
|
||||
Anstelle von {\ttfamily test} kann die "Uberpr"ufung mittels eckigen Klammern erfolgen.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Pfad Check]
|
||||
uws@tux>cat exist2.sh
|
||||
#!/bin/env bash
|
||||
if [ -d /home/uws/scripts -a -f /home/uws/scripts/MyBackup.conf ]
|
||||
then
|
||||
bash /home/uws/scripts/startmount.sh
|
||||
fi
|
||||
\end{lstlisting}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Vergleich]
|
||||
uws@tux>cat exist3.sh
|
||||
#!/bin/env bash
|
||||
if [ "$1" = "tux" ]; then
|
||||
echo "Hallo Pinguin."
|
||||
else
|
||||
echo "Hallo $1, es wurde die \$1 Variable ausgelesen."
|
||||
fi
|
||||
exit 0
|
||||
\end{lstlisting}
|
||||
"Uberpr"ufen, ob eine Datei existiert.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Datei exist]
|
||||
uws@tux>cat exist4.sh
|
||||
#!/bin/env bash
|
||||
strUser=$1
|
||||
if [ ! -r ~/.dbuser/$strUser ]; then
|
||||
echo "File not exist"
|
||||
else
|
||||
echo "File exist"
|
||||
fi
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
Existiert das Verzeichnis nicht, wird es angelegt.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Create Path]
|
||||
if [ ! -d ~/.dbuser ]; then
|
||||
mkdir ~/.dbuser
|
||||
fi
|
||||
\end{lstlisting}
|
||||
Ist die Variable leer? Bei {\ttfamily -n} => wahr, wenn gef"ullt, {\ttfamily -z} => wahr, wenn leer.\\
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Variable leer]
|
||||
if [ -z "${strV1}" ]; then
|
||||
printf "\nVariable is empty.\n"
|
||||
fi
|
||||
\end{lstlisting}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Variable leer, label=lst:bash]
|
||||
#!/bin/env bash
|
||||
Var1=""
|
||||
[ -z "${Var1}" ] && echo "Variable leer: Ja" || echo "Variable leer: Nein"
|
||||
\end{lstlisting}
|
||||
%-------------------------------------------------------------------------------
|
||||
% Subsection: Beispiele Vergleichen
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{Beispiele Vergleichen}
|
||||
Eine Variable nach einem Muster vergleichen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Muster]
|
||||
#!/bin/env bash
|
||||
# Syntax: demo01.sh -s 5x7
|
||||
# :h = Stillen Modus einschalten, Pogramm beenden.
|
||||
# s: = Ein zusaetzlicher Wert wird bei der Eingabe erwartet
|
||||
while getopts ":hs:" opt; do
|
||||
case ${opt} in
|
||||
s)
|
||||
# Der Operator =~ bedeutet, das der Wert rechts des Operators
|
||||
# als regulaeren Ausdruck interpretiert wird.
|
||||
if [[ ${OPTARG} =~ ^[0-9]+x[0-9]+$ ]];
|
||||
then
|
||||
w=${OPTARG%x*} # Wert vor dem x
|
||||
h=${OPTARG#*x} # Wert nach dem x
|
||||
printf "Wert w: ${w}\n"
|
||||
printf "Wert h: ${h}\n"
|
||||
else
|
||||
printf "Ungültige Eingabe.\n"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
\end{lstlisting}
|
||||
|
||||
\end{flushleft}
|
14
Kapitel2/Index.tex
Executable file
@ -0,0 +1,14 @@
|
||||
\chapter{Schleifen / Bedingungen}
|
||||
|
||||
%-------------------------------------------
|
||||
% load other documents
|
||||
%------------------------------------------
|
||||
\input{Kapitel2/Vergleichsparameter}
|
||||
\newpage
|
||||
\input{Kapitel2/CaseAnweisung}
|
||||
\input{Kapitel2/IfAnweisung}
|
||||
\newpage
|
||||
\input{Kapitel2/WhileUntilSchleife}
|
||||
\newpage
|
||||
\input{Kapitel2/ForSchleife}
|
||||
\input{Kapitel2/Functions}
|
36
Kapitel2/Vergleichsparameter.tex
Executable file
@ -0,0 +1,36 @@
|
||||
\section{Vergleichsparameter}
|
||||
\begin{justify}
|
||||
Zahlen werden nicht mit {\ttfamily =, >=, <=} verglichen, sondern mit {\ttfamily -eq, -ge, -gt, -ne, -lt, -le}. Nachfolgend sind ein paar m"ogliche Vergleiche ausgelistet.
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{2cm}p{4cm}p{10cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Ausdruck}} & \emph{\textbf{Beispiel}} & \emph{\textbf{Erkl"arung}} \\
|
||||
\hline
|
||||
\hline
|
||||
-a Datei & \verb|[ -a uschi.txt ]| & Wahr, wenn die Datei vorhanden ist\\
|
||||
-d Verzeichnis & \verb|[ -d /home ]| & Wahr, wenn das Verzeichnis vorhanden ist\\
|
||||
-e Datei & \verb|[ -e uschi.txt ]| & Wahr, wenn die Datei vorhanden ist\\
|
||||
-f Datei & \verb|[ -f uschi.txt ]| & Wahr, wenn die Datei vorhanden ist\\
|
||||
-G Datei & \verb|[ -G uschi.txt ]| & Wahr, wenn die Datei vorhanden ist und eff. Gruppen-ID geh"ort\\
|
||||
-g Datei & \verb|[ -g uschi.txt ]| & Wahr, wenn die Datei vorhanden ist und set-group-id gesetzt ist\\
|
||||
-k Datei & \verb|[ -k uschi.txt ]| & Wahr, wenn das Sticky Bit gesetzt ist\\
|
||||
-L Datei & \verb|[ -L uschi.sh ]| & Wahr, wenn es sich um ein Symbolischen Link handelt\\
|
||||
-O Datei & \verb|[ -O uschi.sh ]| & Wahr, wenn Datei vorhanden ist und eff User-ID geh"ort\\
|
||||
-r Datei & \verb|[ -r uschi.sh ]| & Wahr, wenn die Datei lesbar ist\\
|
||||
-S Datei & \verb|[ -S uschi.sh ]| & Wahr, wenn die Datei ein Socket ist\\
|
||||
-s Datei & \verb|[ -s uschi.txt ]| & Wahr, wenn die Datei gr"o""sser 0 ist\\
|
||||
-u Datei & \verb|[ -u uschi.sh ]| & Wahr, wenn das Bit set-user-id gesetzt ist\\
|
||||
-w Datei & \verb|[ -w uschi.txt ]| & Wahr, wenn die Datei existiert und Schreibzugriffe erlaubt sind\\
|
||||
-x Datei & \verb|[ -x uschi.sh ]| & Wahr, wenn die Datei existiert und die Ausf"uhrung erlaubt ist\\
|
||||
-z str1 & \verb|[ -z "\$1" ]| & Wahr, wenn der Wert leer ist\\
|
||||
-n String & \verb|[ -n "\$name" ]| & Wahr, wenn die Variable nicht leer ist\\
|
||||
str1 = str2 & \verb|[ "\$1" = "uwe" ]| & Wahr, wenn beide Werte identisch sind\\
|
||||
str1 != str2 & \verb|[ "\$1" != "uwe" ]| & Wahr, wenn die Werte ungleich sind\\
|
||||
z1 -eq z2 & \verb|[ 1 -eq \$summe ]| & Wahr, wenn beide Zahlen gleich gro"ss sind\\
|
||||
z1 -lt z2 & \verb|[ 10 -lt \$1 ]| & Wahr, wenn die erste Zahl kleiner ist als die zweite Zahl\\
|
||||
z1 -gt z2 & \verb|[ 20 -gt \$1 ]| & Wahr, wenn die erste Zahl gr"o"sser ist als die zweite Zahl\\
|
||||
z1 -ne z2 & \verb|[ \$1 -ne 10 ]| & Wahr, wenn beide Zahlen ungleich sind\\
|
||||
!ausdruck & \verb|[ ! 1 -eq \$1 ]| & Wahr, wenn der Ausdruck falsch ist\\
|
||||
\end{tabular}
|
||||
\caption{Vergleiche}
|
||||
\end{table}
|
||||
\end{justify}
|
60
Kapitel2/WhileUntilSchleife.tex
Executable file
@ -0,0 +1,60 @@
|
||||
\section{While / Until Schleife}
|
||||
\begin{flushleft}
|
||||
Die Pr"ufung der Bedingung erfolgt bei {\ttfamily while} vor der Abarbeitung der Anweisung
|
||||
und bei {\ttfamily until} erst nach der Abarbeitung.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel While]
|
||||
#!/bin/env bash
|
||||
count=0
|
||||
while [ $count -le 10 ]
|
||||
do
|
||||
echo $count
|
||||
count=$[count+1] # Zaehler un 1 hochzaehlen
|
||||
done
|
||||
exit 0
|
||||
\end{lstlisting}
|
||||
Als n"achstes Beispiel wird eine while Bedingung in einer Zeile geschieben.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=While in einer Zeile]
|
||||
uws@tux>while true; do cat Liste.txt && sleep5; done
|
||||
|
||||
uws@tux>while true; do cat Liste.txt; sleep 5; done
|
||||
\end{lstlisting}
|
||||
% next example
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel Network, label=lst:bash]
|
||||
#!/bin/env bash
|
||||
PING_HOST=$1
|
||||
printf "\nWaiting for network "
|
||||
while [ $(ping -w1 -c1 $PING_HOST | grep -c "0 received") - eq 1 ]; do
|
||||
printf "."
|
||||
done
|
||||
|
||||
printf "\n\nNetwork is now up\n"
|
||||
for i in $(grep noauto /etc/fstab | grep -o "[^]*"); do
|
||||
mount $i
|
||||
done
|
||||
\end{lstlisting}
|
||||
% next example
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel, label=lst:bash]
|
||||
#!/bin/env bash
|
||||
while [ "$1" != "" ]
|
||||
do
|
||||
[ "$1" == "-b" ] && BACKUP=true && printf "Backup now!\n" && shift
|
||||
[ "$1" == "-r" ] && RRSTORE=true && printf "Restore or what else.\n" && shift
|
||||
[ "$1" == "-h" ] && HELPNEED=true && printf "Help needed?\n" && shift
|
||||
done
|
||||
\end{lstlisting}
|
||||
% next example
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel until, label=lst:bash]
|
||||
#!/bin/env bash
|
||||
i=1
|
||||
until [ $1 -gt 6 ]
|
||||
do
|
||||
printf "Tick Tack $i\n"
|
||||
i=$(( i+1 ))
|
||||
done
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
535
Kapitel3/Dialog.tex
Executable file
@ -0,0 +1,535 @@
|
||||
\section{Dialog}
|
||||
\begin{justify}
|
||||
In der Shell kann man grafische Dialoge aufrufen, um in eine Interaktion mit dem Anwender zu treten. Die grafischen Dialoge k"onnen mit dem Befehl {\ttfamily dialog} auch in einer {\ttfamily SSH Sitzung} aufgerufen werden, da der Befehl keinen X-Server ben"otigt. Die Syntax lautet: {\ttfamily dialog [optionen] [dialog\_aufruf] "Text" [breite] [h"ohe]}. Steht ein X-Server zu Verf"ugung, so kann man den Befehl {\ttfamily xdialog} nehmen. Damit man {\ttfamily xdialog} verwenden kann, muss die Software von http://xdialog.dyns.net heruntergeladen und installiert werden.\\
|
||||
In einer KDE Umgebung kann man die Dialoge mit dem Befehl {\ttfamily kdialog} aufrufen. Eine "Ubersicht der Dialoge erh"alt man, wenn man {\ttfamily kdialog} ohne Parameter in der Shell aufruft.\\
|
||||
Folgende Dialog Aufrufe gibt es:
|
||||
\begin{longtable}[l]{p{3cm}p{13cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Dialog}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Dialog}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Liste Dialog} \\
|
||||
\endlastfoot
|
||||
\verb|--|buildlist & Liste zum zusammen stellen\\
|
||||
\verb|--|calendar & Kalender\\
|
||||
\verb|--|checklist & Auswahlliste\\
|
||||
\verb|--|dselect & Auswahl Verzeichnis\\
|
||||
\verb|--|editbox & Editor\\
|
||||
\verb|--|form & Formbox\\
|
||||
\verb|--|fselect & Verzeichnis und Dateiauswahl\\
|
||||
\verb|--|gauge & Fortschrittsanzeige\\
|
||||
\verb|--|infobox & Informationsausgabe\\
|
||||
\verb|--|inputbox & Eingabe Dialog\\
|
||||
\verb|--|inputmenu & Menu\\
|
||||
\verb|--|menu & Menu\\
|
||||
\verb|--|mixedform & Mixed Form\\
|
||||
\verb|--|mixedgauge & Mixed Fortschrittsanzeige\\
|
||||
\verb|--|msgbox & Informationsausgabe\\
|
||||
\verb|--|passwordbox & Wie inputbox, die Eingabe wird mit Sternechen angezeigt\\
|
||||
\verb|--|passwordform & Wie passwordbox.\\
|
||||
\verb|--|pause & Pause mit Fortschrittsbalken\\
|
||||
\verb|--|prgbox & Programm Box\\
|
||||
\verb|--|programbox & Programm Box\\
|
||||
\verb|--|progressbox & Fortschritts Box\\
|
||||
\verb|--|radiolist & Wie checklist, aber hier nur eine Auswahl m"oglich\\
|
||||
\verb|--|rangebox & Range Box\\
|
||||
\verb|--|tailbox & Wie tail -f\\
|
||||
\verb|--|tailboxbg & Wie tail -f \&\\
|
||||
\verb|--|textbox & Viewer f"ur Ascii Dateien mit der M"oglichkeit zum bl"attern\\
|
||||
\verb|--|timebox & Ausgabe der Uhrzeit\\
|
||||
\verb|--|treeview & Liste als Tree\\
|
||||
\verb|--|yesno & Ja / Nein Dialog\\
|
||||
\end{longtable}
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{3cm}p{13cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Optionen}} & \emph{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
--title & "Uberschrift f"ur die Dialog Box\\
|
||||
--backtitle & "Uberschrift f"ur den Hintergrund der Shell\\
|
||||
--begin & Startposition y x\\
|
||||
--cancel-label & "Uberschreibt das Label des Cancel Buttons\\
|
||||
--clear & Der Inhalt des Bildschirms wird nach dem beenden gel"oscht\\
|
||||
--exit-label & "Uberschreibt das Label des Exit Buttons\\
|
||||
--defaultno & Setzt den Fokus auf den Button No\\
|
||||
--extra-button & Zeigt zwischen Ok und Cancel einen extra Button an\\
|
||||
--extra-label & Label Name des Extra Buttons\\
|
||||
--print-maxsize & Ausgabe der Maximum Gr"o\ss e der Dialoge, wird auch mit dem Befehl {\ttfamily resize} angezeigt\\
|
||||
\end{tabular}
|
||||
\caption{Optionen f"ur Dialog}
|
||||
\end{table}
|
||||
\begin{justify}
|
||||
Alle Optionen kann man sich in der Manpage anzeigen lassen. Mit {\ttfamily dialog --help} wird in der Shell die Hilfe angezeigt. Wird bei der Angabe der H"ohe und Breite eine {\ttfamily 0} angegeben, so werden Standardwerte hierf"ur genommen. Den Return Code, ob {\ttfamily Ok} oder {\ttfamily Abbrechen} gedr"uckt wurde, kann man mit der Variable {\ttfamily \$?} auslesen. Der Return Code bei {\ttfamily Ok} ist eine {\ttfamily 0} und bei {\ttfamily Abbrechen} eine {\ttfamily 1}.
|
||||
\end{justify}
|
||||
\newpage
|
||||
%------------------------- buildlist ---------------------------------
|
||||
\subsection{buildlist}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-buildlist <h"ohe> <breite> <h"ohe auswahl> <tag1> <item1> <status1> ...}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Buildlist, label=lst:bash]
|
||||
uws@tux>dialog --buildlist "Your choise:" 0 0 0 01 "Wert 1" off 02 "Wert 2" on 03 "Wert3" off
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/buildlist.png}
|
||||
\caption{Dialog buildlist}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ calendar -----------------------------------
|
||||
\subsection{Calendar}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-calendar "Text" \hspace{0.5ex} <h"ohe> <breite> <tag> <monat> <jahr>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Calendar, label=lst:bash]
|
||||
uws@tux>dialog --calendar "Datum:" 0 0 17 08 2010
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/calendar.png}
|
||||
\caption{Dialog calendar}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
|
||||
%------------------------ checklist ----------------------------------
|
||||
\subsection{Checklist}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-checklist "Text" \hspace{0.5ex} <h"ohe> <breite> <zeilen> <eintr"age> <liste...>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Checklist, label=lst:bash]
|
||||
uws@tux>dialog --checklist "Bitte ausw"ahlen::" 20 30 3 \
|
||||
01 "Erste Auswahl" on\
|
||||
02 "Zweite Auswahl" off\
|
||||
03 "Dritte Auswahl" off
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/checklist.png}
|
||||
\caption{Dialog checklist}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ dselect -----------------------------------
|
||||
\subsection{dselect}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-dselect <directory> <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Dselect, label=lst:bash]
|
||||
uws@tux>dialog --dselect /var/log 10 20
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/dselect.png}
|
||||
\caption{Dialog dselect}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
|
||||
%------------------------ editbox -----------------------------------
|
||||
\subsection{editbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-editbox <file> <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Editbox, label=lst:bash]
|
||||
uws@tux>dialog --dselect Bashing.out 0 0
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/editbox.png}
|
||||
\caption{Dialog editbox}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
|
||||
%------------------------ form --------------------------------------
|
||||
\subsection{Form}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-form "Text" \hspace{0.5ex} <h"ohe> <breite> <form\_h"ohe> <text> <y> <x> <text> <y> <x> <flen> <ilen>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Form, label=lst:bash]
|
||||
uws@tux>dialog --form "Info" 0 0 0 "User: " 1 1 "uws" 1 10 10 0 \
|
||||
"Shell: " 2 1 "Bash" 2 10 15 0
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/form.png}
|
||||
\caption{Dialog form}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ fselect -----------------------------------
|
||||
\subsection{Fselect}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-fselect <directory> <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Fselect, label=lst:bash]
|
||||
uws@tux>dialog --fselect /var/log 10 20
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/fselect.png}
|
||||
\caption{Dialog fselect}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ gauge -------------------------------------
|
||||
\subsection{Gauge}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-gauge "Text" \hspace{0.5ex} <h"ohe> <breite> <fertigstellung}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Gauge, label=lst:bash]
|
||||
uws@tux>dialog --gauge "Fertig zu:" 7 30 50
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/gauge.png}
|
||||
\caption{Dialog gauge}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
|
||||
Beispiel eines laufenden Balkens.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Praxis Beispiel, label=lst:bash]
|
||||
for I in $(seq 1 100); do
|
||||
echo $I | dialog --backtitle "Progress Status" --gauge "Fertig zu:" 8 50 0
|
||||
sleep 0.01
|
||||
done
|
||||
\end{lstlisting}
|
||||
|
||||
%------------------------ infobox -----------------------------------
|
||||
\subsection{Infobox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-infobox "Text" \hspace{0.5ex} <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Infobox, label=lst:bash]
|
||||
uws@tux>dialog --infobox "Es ist bald Mittag." 5 30
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/infobox.png}
|
||||
\caption{Dialog infobox}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ inputbox ----------------------------------
|
||||
\subsection{Inputbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-inputbox "Text" \hspace{0.5ex} <h"ohe> <breite> <?>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Inputbox, label=lst:bash]
|
||||
uws@tux>cat demo_inputbox.sh
|
||||
#!/bin/env bash
|
||||
dialog --inputbox "Dein Name bitte:" 10 60 2> eingabe.tmp
|
||||
clear
|
||||
echo "dein Name lautet: `cat eingabe.tmp`"
|
||||
exit 0
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/inputbox.png}
|
||||
\caption{Dialog inputbox}
|
||||
\end{figure}
|
||||
|
||||
Anstelle von {\ttfamily cat} kann man die Datei auch mit {\ttfamily <} einlesen.
|
||||
F"ur eine Passwort Eingabe gibt es die {\ttfamily passwordbox}.
|
||||
|
||||
\newpage
|
||||
|
||||
%------------------------ inputmenu ---------------------------------
|
||||
\subsection{Inputmenu}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-inputmenu "Text" \hspace{0.5ex} <h"ohe> <breite> <menu\_h"ohe> <tag1> <item1> ...}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Inputmenu, label=lst:bash]
|
||||
uws@tux>dialog --inputmenu "Menu:" 30 50 10 "01" "Montag" "02" "Dienstag"
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/inputmenu.png}
|
||||
\caption{Dialog inputmenu}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ menu --------------------------------------
|
||||
\subsection{Menu}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-menu "Text" \hspace{0.5ex} <h"ohe> <breite> <menu\_h"ohe> <tag1> <item1> ...}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Menu, label=lst:bash]
|
||||
uws@tux>dialog --menu "Menu:" 30 50 10 "A" "Taschenrechner"
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/menu.png}
|
||||
\caption{Dialog menu}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
|
||||
%------------------------ mixedform ---------------------------------
|
||||
\subsection{Mixedform}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-mixedform "Text" \hspace{0.5ex} <h"ohe> <breite> <form height> <label1> <l\_y1> <l\_x1> <item1> <i\_y1> <i\_x1>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Mixedform, label=lst:bash]
|
||||
uws@tux>dialog --mixedform "Mixed Form:" 0 0 0 "A" 1 1 "B" 2 2 "C" "D" "E"
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/mixedform.png}
|
||||
\caption{Dialog mixedform}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ mixedgauge --------------------------------
|
||||
\subsection{Mixedgauge}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-mixedgauge "Text" \hspace{0.5ex} <h"ohe> <breite> <percent> [tag1 item1]}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Mixedgauge, label=lst:bash]
|
||||
uws@tux>dialog --mixedgauge "Mixed Gauge" 0 0 30 "A" "text1"
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/mixedgauge.png}
|
||||
\caption{Dialog mixedgauge}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ msgbox ------------------------------------
|
||||
\subsection{Msgbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-menu "Text" \hspace{0.5ex} <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Msgbox, label=lst:bash]
|
||||
uws@tux>dialog --msgbox "Hier koennte ihr Text stehen." 8 30
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/msgbox.png}
|
||||
\caption{Dialog msgbox}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
%------------------------- passwordbox -------------------------------
|
||||
\subsection{Passwordbox}
|
||||
Aufruf und auch die Syntax ist die gleiche wie bei der {\ttfamily inputbox}.
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/passwordbox.png}
|
||||
\caption{Dialog passwordbox}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- passwordform -------------------------------
|
||||
\subsection{Passwordform}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-passwordform "Text" \hspace{0.5ex} <h"ohe> <breite> <form\_h"ohe> <label1> <l\_y1> <l\_x1> <item1> <i\_y1> <i\_x1> <flen1> <ilen1>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Passwordform, label=lst:bash]
|
||||
uws@tux>dialog --passwordform "Passwort:" 0 0 0 "Type" 1 1 "Item1" 1 1 5 5
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/passwordform.png}
|
||||
\caption{Dialog passwordform}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- pause --------------------------------------
|
||||
\subsection{Pause}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-pause "Text" \hspace{0.5ex} <h"ohe> <breite> <sekunden>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Pause, label=lst:bash]
|
||||
uws@tux>dialog --pause "Wait for " 8 0 20
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/pause.png}
|
||||
\caption{Dialog pause}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
%------------------------- prgbox -------------------------------------
|
||||
\subsection{Prgbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-pause "Text" "command" \hspace{0.5ex} <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Prgbox, label=lst:bash]
|
||||
uws@tux>dialog --prgbox "Prgbox: " "ls -l" 8 50
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/prgbox.png}
|
||||
\caption{Dialog prgbox}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- programbox ---------------------------------
|
||||
\subsection{Programbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-programbox "Text" \hspace{0.5ex} <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Programbox, label=lst:bash]
|
||||
uws@tux>dialog --programbox "Command: " 0 0
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/programbox.png}
|
||||
\caption{Dialog programbox}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- progressbox --------------------------------
|
||||
\subsection{Progressbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-progressbox "Text" \hspace{0.5ex} <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Msgbox, label=lst:bash]
|
||||
uws@tux>dialog --progressbox "Fortschritt:" 10 60
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/progressbox.png}
|
||||
\caption{Dialog progressbox}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
%------------------------- radiolist ----------------------------------
|
||||
\subsection{Radiolist}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-radiolist "Text" \hspace{0.5ex} <h"ohe> <breite> <list\_h"ohe> <tag1> <item1> <status1>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Radiolist, label=lst:bash]
|
||||
uws@tux>dialog --radiolist "Auswahl:" 0 0 0 "A" "Hund" "off" "02" "Katze" "On"
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/radiolist.png}
|
||||
\caption{Dialog radiolist}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- rangebox -----------------------------------
|
||||
\subsection{Rangebox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-rangebox "Text" \hspace{0.5ex} <h"ohe> <breite> <min> <max> <default>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Rangebox, label=lst:bash]
|
||||
uws@tux>dialog --rangebox "Range: " 0 5 1 20 5
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/rangebox.png}
|
||||
\caption{Dialog rangebox}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- tailbox ------------------------------------
|
||||
\subsection{Tailbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-tailbox <file> <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=tailbox, label=lst:bash]
|
||||
uws@tux>dialog --tailbox "/home/uws/.bashrc" 10 40
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/tailbox.png}
|
||||
\caption{Dialog tailbox}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
%------------------------- tailboxbg ------------------------------------
|
||||
\subsection{Tailboxbg}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-tailboxbg <file> <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=tailbox, label=lst:bash]
|
||||
uws@tux>dialog --tailboxbg "/home/uws/.bashrc" 10 40
|
||||
\end{lstlisting}
|
||||
|
||||
%------------------------- textbox ------------------------------------
|
||||
\subsection{Textbox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-textbox <file> <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Textbox, label=lst:bash]
|
||||
uws@tux>dialog --textbox "/home/uws/.bashrc" 25 70
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/textbox.png}
|
||||
\caption{Dialog textbox}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- timebox ------------------------------------
|
||||
\subsection{Timebox}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-timebox "Text" \hspace{0.5ex} <h"ohe> <breite> <stunde> <minute> <sekunde>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Timebox, label=lst:bash]
|
||||
uws@tux>dialog --timebox "Aktuelle Zeit:" 0 30 13 09 22
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/timebox.png}
|
||||
\caption{Dialog timebox}
|
||||
\end{figure}
|
||||
|
||||
%------------------------ treeview ------------------------------------
|
||||
\subsection{Treeview}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-treeview "Text" \hspace{0.5ex} <h"ohe> <breite> <list\-height> <tag1> <item1> <status1> <depth1>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Treeview, label=lst:bash]
|
||||
uws@tux>dialog --treeview "Liste" 0 0 0 "A" "root" off 1 "B" "usr" on 2
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/treeview.png}
|
||||
\caption{Dialog treeview}
|
||||
\end{figure}
|
||||
|
||||
%------------------------- yesno --------------------------------------
|
||||
\subsection{YesNo}
|
||||
Syntax: {\ttfamily dialog -\hspace{0.01cm}-yesno "Text" \hspace{0.5ex} <h"ohe> <breite>}
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=YesNo, label=lst:bash]
|
||||
uws@tux>dialog --yesno "Steuern sparen?" 5 20
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/dialog/yesno.png}
|
||||
\caption{Dialog yesno}
|
||||
\end{figure}
|
||||
\end{justify}
|
8
Kapitel3/Index.tex
Executable file
@ -0,0 +1,8 @@
|
||||
\chapter{GUI}
|
||||
|
||||
%-------------------------------------------
|
||||
% load other documents
|
||||
%------------------------------------------
|
||||
\input{Kapitel3/Yad}
|
||||
\newpage
|
||||
\input{Kapitel3/Dialog}
|
BIN
Kapitel3/Pictures/dialog/buildlist.png
Executable file
After Width: | Height: | Size: 5.2 KiB |
BIN
Kapitel3/Pictures/dialog/calendar.png
Executable file
After Width: | Height: | Size: 13 KiB |
BIN
Kapitel3/Pictures/dialog/checklist.png
Executable file
After Width: | Height: | Size: 9.8 KiB |
BIN
Kapitel3/Pictures/dialog/dselect.png
Executable file
After Width: | Height: | Size: 11 KiB |
BIN
Kapitel3/Pictures/dialog/editbox.png
Executable file
After Width: | Height: | Size: 107 KiB |
BIN
Kapitel3/Pictures/dialog/form.png
Executable file
After Width: | Height: | Size: 5.0 KiB |
BIN
Kapitel3/Pictures/dialog/fselect.png
Executable file
After Width: | Height: | Size: 12 KiB |
BIN
Kapitel3/Pictures/dialog/gauge.png
Executable file
After Width: | Height: | Size: 2.5 KiB |
BIN
Kapitel3/Pictures/dialog/infobox.png
Executable file
After Width: | Height: | Size: 2.0 KiB |
BIN
Kapitel3/Pictures/dialog/inputbox.png
Executable file
After Width: | Height: | Size: 4.6 KiB |
BIN
Kapitel3/Pictures/dialog/inputmenu.png
Executable file
After Width: | Height: | Size: 9.0 KiB |
BIN
Kapitel3/Pictures/dialog/menu.png
Executable file
After Width: | Height: | Size: 4.9 KiB |
BIN
Kapitel3/Pictures/dialog/mixedform.png
Executable file
After Width: | Height: | Size: 4.3 KiB |
BIN
Kapitel3/Pictures/dialog/mixedgauge.png
Executable file
After Width: | Height: | Size: 5.7 KiB |
BIN
Kapitel3/Pictures/dialog/msgbox.png
Executable file
After Width: | Height: | Size: 3.1 KiB |
BIN
Kapitel3/Pictures/dialog/passwordbox.png
Executable file
After Width: | Height: | Size: 3.7 KiB |
BIN
Kapitel3/Pictures/dialog/passwordform.png
Executable file
After Width: | Height: | Size: 4.0 KiB |
BIN
Kapitel3/Pictures/dialog/pause.png
Executable file
After Width: | Height: | Size: 3.8 KiB |
BIN
Kapitel3/Pictures/dialog/prgbox.png
Executable file
After Width: | Height: | Size: 4.9 KiB |
BIN
Kapitel3/Pictures/dialog/programbox.png
Executable file
After Width: | Height: | Size: 1.5 KiB |
BIN
Kapitel3/Pictures/dialog/progressbox.png
Executable file
After Width: | Height: | Size: 2.1 KiB |
BIN
Kapitel3/Pictures/dialog/radiolist.png
Executable file
After Width: | Height: | Size: 6.4 KiB |
BIN
Kapitel3/Pictures/dialog/rangebox.png
Executable file
After Width: | Height: | Size: 3.7 KiB |
BIN
Kapitel3/Pictures/dialog/tailbox.png
Executable file
After Width: | Height: | Size: 3.4 KiB |
BIN
Kapitel3/Pictures/dialog/tailbox.png.png
Executable file
After Width: | Height: | Size: 6.1 KiB |
BIN
Kapitel3/Pictures/dialog/textbox.png
Executable file
After Width: | Height: | Size: 18 KiB |
BIN
Kapitel3/Pictures/dialog/timebox.png
Executable file
After Width: | Height: | Size: 4.8 KiB |
BIN
Kapitel3/Pictures/dialog/treeview.png
Executable file
After Width: | Height: | Size: 5.1 KiB |
BIN
Kapitel3/Pictures/dialog/yesno.png
Executable file
After Width: | Height: | Size: 3.2 KiB |
BIN
Kapitel3/Pictures/yad/Choise.png
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
Kapitel3/Pictures/yad/Formular.png
Normal file
After Width: | Height: | Size: 58 KiB |
BIN
Kapitel3/Pictures/yad/Password.png
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
Kapitel3/Pictures/yad/Systeminfo.png
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
Kapitel3/Pictures/yad/Zahlen.png
Normal file
After Width: | Height: | Size: 20 KiB |
425
Kapitel3/Yad.tex
Normal file
@ -0,0 +1,425 @@
|
||||
\section{Yad}
|
||||
\subsection{Allgemein}
|
||||
\begin{justify}
|
||||
Mit Yad gibt es ein Modernes Dialog-Toolkit. Dieses Toolkit bietet eine große Anzahl an Dialogen.\\
|
||||
Eine Auswahl an Globale Optionen sind nachfolgend aufgelistet:
|
||||
\begin{longtable}[l]{p{6cm}p{10cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Fensterpositionen} \\
|
||||
\endlastfoot
|
||||
\verb|--|width=Breite & voreingestellt Breite\\
|
||||
\verb|--|height=Höhe & voreingestellte Höhe\\
|
||||
\verb|--|geometry=Breite,Höhe+X,Y & Größe und (optional) Position\\
|
||||
\end{longtable}
|
||||
\begin{longtable}[l]{p{6cm}p{10cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Timeout} \\
|
||||
\endlastfoot
|
||||
\verb|--|timeout=Sekunden & Zeit zum schließen des Fensters\\
|
||||
\verb|--|time-out-indicator=Position & Position des Indikators\\
|
||||
\end{longtable}
|
||||
\begin{longtable}[l]{p{6cm}p{10cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Beschriftungen und Icons} \\
|
||||
\endlastfoot
|
||||
\verb|--|text=Text & Text in Fenster, nicht edierbar\\
|
||||
\verb|--|text-align=Ausrichtung & Ausrichtung des Textes\\
|
||||
\verb|--|selectable-labes & erlaubt Texte mit der Maus zu markieren und kopieren\\
|
||||
\verb|--|no-markuo & ignoriert Markup-Anweisungen\\
|
||||
\verb|--|image=Bild & Bild oder Icon links im Fenster\\
|
||||
\verb|--|image-on-top & Bild oder Icon erscheint oberhalb statt links\\
|
||||
\verb|--|icon-theme=Theme & verwendetes Theme (voreingestellt default)\\
|
||||
\end{longtable}
|
||||
\begin{longtable}[l]{p{6cm}p{10cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Schalter} \\
|
||||
\endlastfoot
|
||||
\verb|--|button=Text:ID & Button Text mit einer Optionalen ID. Zwei ID's sidn von Yad belegt. ID 252 beim Verlassen mit Esc und 70 wenn eine Timeout vorliegt.\\
|
||||
\verb|--|no-buttons & Keine Buttons darstellen, gut für Timeout\\
|
||||
\verb|--|buttons-layout=Layout & Varianten: spread, edge, start, end, center\\
|
||||
\end{longtable}
|
||||
\begin{longtable}[l]{p{6cm}p{10cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Fenster} \\
|
||||
\endlastfoot
|
||||
\verb|--|title=Titel & Fenstertitel\\
|
||||
\verb|--|window-icon=Icon & erklärendes Icon\\
|
||||
\verb|--|undecorated & Fenster ohne Dekoration\\
|
||||
\verb|--|borders=Breite & Breite des Randes in Pixel\\
|
||||
\verb|--|sticks & Dialog erscheint auf allen Desktops\\
|
||||
\verb|--|fixed & feste Fenstergröße\\
|
||||
\verb|--|center & Dialog in der Mitte des Desktops\\
|
||||
\verb|--|on-top & öffnet des Dialog über allen anderen Fenstern\\
|
||||
\verb|--|skip-taskbar & Fenster erscheint nicht in Taskbar\\
|
||||
\end{longtable}
|
||||
\begin{longtable}[l]{p{6cm}p{10cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wirkung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Diverses} \\
|
||||
\endlastfoot
|
||||
\verb|-|h & Kurzhilfe\\
|
||||
\verb|--|display=Display & verwendetes Display\\
|
||||
\verb|--|rest=Datei & Datei mit zusätzlichen Optionen\\
|
||||
\verb|--|separator & Trennzeichen, Standard ist ein |\\
|
||||
\verb|--|item-separator & Trennzeichen übergabe Argumente\\
|
||||
\end{longtable}
|
||||
\begin{longtable}[l]{p{6cm}p{10cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Dialog}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Dialog}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Dialoge} \\
|
||||
\endlastfoot
|
||||
\verb|--|about & About Dialog\\
|
||||
\verb|--|app & Applikation Auswahl\\
|
||||
\verb|--|calendar & Kalender\\
|
||||
\verb|--|color & Farbauswahldialog\\
|
||||
\verb|--|dnd & Drag und Drop-Fenster\\
|
||||
\verb|--|entry & Texteinträge (editierbar)\\
|
||||
\verb|--|file & Dateiauswahl\\
|
||||
\verb|--|font & Schriftauswahl\\
|
||||
\verb|--|form & Formulare\\
|
||||
\verb|--|icons & Icons als Buttons\\
|
||||
\verb|--|html & HTML Dialog\\
|
||||
\verb|--|list & allgemeine Listen\\
|
||||
\verb|--|multi-progress & mehrfache Fortschrittsanzeigen\\
|
||||
\verb|--|notebook & Notizbuch\\
|
||||
\verb|--|notification & Desktop-Benachrichtigungen\\
|
||||
\verb|--|print & Druckdialog\\
|
||||
\verb|--|progress & Fortschrittsanzeige\\
|
||||
\verb|--|scale & Schieberegler\\
|
||||
\verb|--|text-info & Infotext\\
|
||||
\end{longtable}
|
||||
\begin{justify}
|
||||
Mit {\ttfamily yad \verb|--|help} kann man sich alle Optionen und Parameter sich anzeigen lassen. Mit z.B. {\ttfamily yad \verb|--|help-app} werden Informationen über den Dialog Type App angezeigt.
|
||||
\end{justify}
|
||||
\newpage
|
||||
%-------------------------------------------------------------------------------
|
||||
% Subsection: Beispiele
|
||||
%-------------------------------------------------------------------------------
|
||||
\subsection{Beispiele}
|
||||
Es gibt Dialoge, die nicht so richtig unter Wayland laufen. Damit diese auch unter Wayland laufen, wird vor jedem Aufruf von yad ein {\ttfamily GDK_BACKEND=x11} gesetzt.
|
||||
%---------- Formulare
|
||||
\subsubsection{Formulare}
|
||||
Die nächsten 5 Beispiele sind aus dem Wiki der Ubuntuusers.de.
|
||||
\begin{longtable}[l]{p{3cm}p{13cm}}
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Typ}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endfirsthead
|
||||
%\multicolumn{2}{c}{{\bfseries \tablename \thetable{} continued from previous page.}} \\
|
||||
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
||||
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Typ}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
\endhead
|
||||
%\multicolumn{2}{r}{{\bfseries \tablename \thetable{} Continued on next page}} \\
|
||||
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
||||
\endfoot
|
||||
\caption{Feldtypen} \\
|
||||
\endlastfoot
|
||||
RO & Read only\\
|
||||
NUM & Numerisch\\
|
||||
CHK & Checkbox\\
|
||||
CBE & editierbare Combobox\\
|
||||
FL & Schaltfläche für Dateiauswahl\\
|
||||
SFL & Feld zum erstellen einer datei\\
|
||||
DIR & Schaltfläche für Verzeichniswahl\\
|
||||
CDIR & Feld zum Erstellen eines Verzeichnisses\\
|
||||
FN & Schaltfläche für Schriftauswahl\\
|
||||
MFL & Mehrfache Dateiauswahl\\
|
||||
DT & Datumsfeld\\
|
||||
CLR & Farbauswahl\\
|
||||
BTN & Schaltfläche\\
|
||||
LBL & Überschrift\\
|
||||
\end{longtable}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Formular]
|
||||
uws@tux>yad \
|
||||
--title="Field Optionen" \
|
||||
--form \
|
||||
--item-separator=, \
|
||||
--separator=" " \
|
||||
--field="Nur lesbares Feld:RO" Text \
|
||||
--field="Auswahl einer Zahl:NUM" \
|
||||
--field="Ich bin eine Checkbox:CHK" \
|
||||
--field="Editierbares Feld:CBE" Text \
|
||||
--field="Dateiauswahl:FL" \
|
||||
--field="Hier kannst du eine Datei erstellen:SFL" \
|
||||
--field="Verzeichnisauswahl:DIR" \
|
||||
--field="Hier kannst du ein Verzeichnis erstellen:CDIR" \
|
||||
--field="Schriftartauswahl:FN" \
|
||||
--field="Hier kannst du mehrere Dateien wählen:MFL" \
|
||||
--field="Wähle ein Datum:DT" \
|
||||
--field="Wähle eine Farbe:CLR" \
|
||||
--field="Ich bin ein klickbarer Button:BTN" \
|
||||
--field="Ich bin eine Überschrift:LBL" \
|
||||
--field="Texteingabe:TEXT" \
|
||||
--button="Ich bin Button 1" \
|
||||
--button="Ich bin Button 2" \
|
||||
--button="Ich bin Button 3" \
|
||||
--button="Ich bin Button 4"
|
||||
\end{lstlisting}
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/yad/Formular.png}
|
||||
\caption{Formular}
|
||||
\end{figure}
|
||||
\newpage
|
||||
%---------- Passwortabfrage
|
||||
\subsubsection{Passwortabfrage}
|
||||
Eine Passwordabfrage, wo die Eingabe nicht im Klartext angezeigt wird.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Passwortabfrage]
|
||||
uws@tux>cat Password.sh
|
||||
#! /usr/bin/env bash
|
||||
eingabe="$(yad --entry --hide-text --button="Auswerten" --title="Passwort" --text="Gebe ein Passwort ein:")"
|
||||
|
||||
yad --info --text="Die Eingabe war $eingabe"
|
||||
\end{lstlisting}
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/yad/Password.png}
|
||||
\caption{Formular}
|
||||
\end{figure}
|
||||
\newpage
|
||||
%---------- Zahlenabfrage
|
||||
\subsubsection{Zahlenabfrage}
|
||||
Einen numerischen Wert kann man folgendermaßen machen.
|
||||
\begin{lstlisting}[captionpos=b, caption=Zahlenabfrage]
|
||||
uws@tux>cat Zahlen.sh
|
||||
#! /usr/bin/env bash
|
||||
array=($(yad \
|
||||
--item-separator="," \
|
||||
--separator="\\n" \
|
||||
--form \
|
||||
--field="Wähle einen Wert":NUM 5,1..10,1))
|
||||
|
||||
yad --info --text="Der Wert ist ${array[0]}"
|
||||
\end{lstlisting}
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/yad/Zahlen.png}
|
||||
\caption{Formular}
|
||||
\end{figure}
|
||||
\newpage
|
||||
%---------- Menüdialog
|
||||
\subsubsection{Menüdialog Panel}
|
||||
Ein Menüdialog, der im Panel angezeigt wird, kann man so erstellen.
|
||||
\begin{lstlisting}[captionpos=b, caption=Menüdialog Panel]
|
||||
uws@tux>cat MenuePanel.sh
|
||||
#! /usr/bin/env bash
|
||||
#Pipe erstellen
|
||||
PIPE="$HOME/.pipe.tmp"
|
||||
rm $PIPE
|
||||
mkfifo $PIPE
|
||||
exec 3<> $PIPE
|
||||
#Yad Dialog erstellen
|
||||
GDK_BACKEND=x11 yad --notification --listen <&3 &
|
||||
#Menüeinträge definieren
|
||||
echo "menu:\
|
||||
Notify ausgeben!notify-send "...Test"|\
|
||||
CD Fach öffnen!eject|\
|
||||
Gedit öffnen!gedit|\
|
||||
VLC Player öffnen!vlc|\
|
||||
My Script!/pfad/zum/script/script.sh|\
|
||||
Beispielbutton 5!echo "Button 5 wurde gedrückt" " >&3
|
||||
#Icon des Menübuttons definieren
|
||||
echo "icon:/pfad/zum/icon.png" >&3
|
||||
#Name des Menüs definieren
|
||||
echo "tooltip:Beispielmenü" >&3
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
%---------- Entscheidungsabfrage
|
||||
\subsubsection{Entscheidungabfrage}
|
||||
\begin{lstlisting}[captionpos=b, caption=Entscheidungsabfrage]
|
||||
uws@tux>cat choise.sh
|
||||
#! /usr/bin/env bash
|
||||
#YAD Dialog erstellen
|
||||
yad --title="CD auswerfen" --text="Willst du das CD-Laufwerk öffnen?" --button="Ja, ich will" --button="Nein, lass es drinnen" --image=/pfad/zum/bild/im/fenster.png
|
||||
#Returnwert speichern
|
||||
ret=$?
|
||||
#Auswerten des Returnwertes
|
||||
if [ $ret = 0 ] #Wenn der Benutzer auf JA drückt,
|
||||
then #dann führe folgenden Befehl aus
|
||||
eject #Befehl
|
||||
else #Ansonsten führe diesen Befehl aus
|
||||
exit 1 #Befehl (hier exit 1 für beenden)
|
||||
fi
|
||||
\end{lstlisting}
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/yad/Choise.png}
|
||||
\caption{Formular}
|
||||
\end{figure}
|
||||
\newpage
|
||||
%---------- Systeminfo
|
||||
\subsubsection{Systeminfo}
|
||||
Dieses Beispiel ist von dem Yad-Guide.
|
||||
\begin{lstlisting}[captionpos=b, caption=Systeminfo]
|
||||
uws@tux>cat Systeminfo.sh
|
||||
#! /usr/bin/env bash
|
||||
KEY=$RANDOM
|
||||
|
||||
function show_mod_info {
|
||||
TXT="\\n<span face='Monospace'>$(modinfo $1 | sed 's/&/\&/g;s/</\</g;s/>/\>/g')</span>"
|
||||
yad --title="Module information" \
|
||||
--window-icon="application-x-addon" \
|
||||
--button="yad-close" \
|
||||
--width=500 \
|
||||
--image="application-x-addon" --text="$TXT"
|
||||
}
|
||||
export -f show_mod_info
|
||||
|
||||
# CPU tab
|
||||
lscpu | sed -r "s/:[ ]*/\n/" |\
|
||||
GDK_BACKEND=x11 yad --plug=$KEY --tabnum=1 --image=cpu --text="CPU information" \
|
||||
--list --no-selection --column="Parameter" --column="Value" &
|
||||
|
||||
# Memory tab
|
||||
sed -r "s/:[ ]*/\n/" /proc/meminfo |\
|
||||
GDK_BACKEND=x11 yad --plug=$KEY --tabnum=2 --image=media-memory \
|
||||
--text="Memory usage information" \
|
||||
--list --no-selection --column="Parameter" --column="Value" &
|
||||
|
||||
# Harddrive tab
|
||||
df -T | tail -n +2 | awk '{printf "%s\n%s\n%s\n%s\n%s\n%s\n", $1,$7, $2, $3, $4, $6}' |\
|
||||
GDK_BACKEND=x11 yad --plug=$KEY --tabnum=3 --image=drive-harddisk \
|
||||
--text="Disk space usage" \
|
||||
--list --no-selection --column="Device" \
|
||||
--column="Mountpoint" --column="Type" \
|
||||
--column="Total:sz" --column="Free:sz" \
|
||||
--column="Usage:bar" &
|
||||
|
||||
# PCI tab
|
||||
lspci -vmm | sed 's/\&/\&/g' | grep -E "^(Slot|Class|Vendor|Device|Rev):" | cut -f2 |\
|
||||
GDK_BACKEND=x11 yad --plug=$KEY --tabnum=4 --text="PCI bus devices" \
|
||||
--list --no-selection --column="ID" --column="Class" \
|
||||
--column="Vendor" --column="Device" --column="Rev" &
|
||||
|
||||
# Modules tab
|
||||
awk '{printf "%s\n%s\n%s\n", $1, $3, $4}' /proc/modules | sed "s/[,-]$//" |\
|
||||
GDK_BACKEND=x11 yad --plug=$KEY --tabnum=5 --text="Loaded kernel modules" \
|
||||
--image="application-x-addon" --image-on-top \
|
||||
--list --dclick-action='bash -c "show_mod_info %s"' \
|
||||
--column="Name" --column="Used" --column="Depends" &
|
||||
|
||||
# Battery tab
|
||||
( acpi -i ; acpi -a ) | sed -r "s/:[ ]*/\n/" | GDK_BACKEND=x11 yad --plug=$KEY --tabnum=6 \
|
||||
--image=battery --text="Battery state" --list --no-selection \
|
||||
--column="Device" --column="Details" &
|
||||
|
||||
# Sensors tab
|
||||
SENSORS=($(sensors | grep -E '^[^:]+$'))
|
||||
sid=1
|
||||
cid=1
|
||||
|
||||
for s in ${SENSORS[@]}; do
|
||||
echo -e "s$sid\n<b>$s</b>\n"
|
||||
sensors -A "$s" | tail -n +2 | while read ln; do
|
||||
[[ $ln == "" ]] && continue
|
||||
echo "$cid:s$sid"
|
||||
echo $ln | sed -r 's/:[ ]+/\n/'
|
||||
((cid++))
|
||||
done
|
||||
((sid++))
|
||||
done | GDK_BACKEND=x11 yad --plug=$KEY --tabnum=7 --text="Temperature sensors information" \
|
||||
--list --tree --tree-expanded --no-selection --column="Sensor" --column="Value" &
|
||||
|
||||
# Main dialog
|
||||
TXT="<b>Hardware system information</b>\\n\\n"
|
||||
TXT+="\\tOS: $(lsb_release -ds) on $(hostname)\\n"
|
||||
TXT+="\\tKernel: $(uname -sr)\\n\\n"
|
||||
TXT+="\\tUptime: <i>$(uptime)</i>"
|
||||
|
||||
GDK_BACKEND=x11 yad --notebook --window-icon="dialog-information" \
|
||||
--width=600 --height=450 --title="System info" --text="$TXT" --button=yad-close \
|
||||
--key=$KEY --tab="CPU" --tab="Memory" --tab="Disks" --tab="PCI" --tab="Modules" \
|
||||
--tab="Battery" --tab="Sensors" --active-tab=${1:-1}
|
||||
\end{lstlisting}
|
||||
\begin{figure}[ht]
|
||||
\centering
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/yad/Systeminfo.png}
|
||||
\caption{Formular}
|
||||
\end{figure}
|
||||
\end{justify}
|
24
Kapitel4/AusgabeDateiName.tex
Executable file
@ -0,0 +1,24 @@
|
||||
\section{Ausgabe Datei Name}
|
||||
\begin{flushleft}
|
||||
Die Ausgabe des Script Namens wird mit {\ttfamily basename \$0} gemacht.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoBasename.sh, label=lst:bash]
|
||||
uws@tux>cat DemoBasename.sh
|
||||
#!/bin/env bash
|
||||
echo Script Name: $(basename $0)
|
||||
\end{lstlisting}
|
||||
|
||||
M"ochte man aus einer Pfadangabe nur den Dateiname haben, so wird hierzu nur
|
||||
{\ttfamily basename} mit dem Pfad aufgerufen. Soll auch die Extension abgeschnitten
|
||||
werden, so wird nach der Angabe der Datei noch die Extension geschrieben.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoBasename1.sh, label=lst:bash]
|
||||
uws@tux>basename /daten/script/MyScript.py
|
||||
MyScript.py
|
||||
|
||||
uws@tux>basename /daten/script/MyScript.py .py
|
||||
MyScript
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
16
Kapitel4/AusgabeDateiPath.tex
Executable file
@ -0,0 +1,16 @@
|
||||
\section{Ausgabe Datei Pfad}
|
||||
\begin{flushleft}
|
||||
Die Ausgabe des Script Pfades kann man mit oder ohne den Namen des Scripts ermitteln.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoScriptPath.sh]
|
||||
uws@tux>cat DemoScriptPath.sh
|
||||
#!/usr/bin/env bash
|
||||
printf "Script Pfad without Name: $(dirname $(realpath "$0"))\n"
|
||||
printf " Script Pfad with Name: $(realpath "$0")\n"
|
||||
|
||||
uws@tux>./DemoScriptPath.sh
|
||||
Script Pfad without Name: /home/uws/scripts
|
||||
Script Pfad with Name: /home/uws/scripts/DemoScriptPath.sh
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
16
Kapitel4/AusgabeVersion.tex
Executable file
@ -0,0 +1,16 @@
|
||||
\section{Ausgabe Version}
|
||||
\begin{flushleft}
|
||||
Die Ausgabe der Version seines Scripts k"onnte folgenden Inhalt haben.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoVersion.sh, label=lst:bash]
|
||||
uws@tux>./DemoVersion.sh -V
|
||||
DemoVersion.sh 1.2
|
||||
Copyright © 2018 Seab@er Software
|
||||
Lizenz GPLv3+: GNU GPL Version 3 oder hoeher <http://gnu.org/licenses/gpl.html>
|
||||
Dies ist eine freie Software: Sie koennen sie aendern und weitergeben.
|
||||
Es gibt keinerlei Garantien, soweit wie es das Gesetz erlaubt.
|
||||
|
||||
Geschrieben von Uwe Schimanski
|
||||
\end{lstlisting}
|
||||
|
||||
\end{flushleft}
|
78
Kapitel4/AuswahlMenuSelect.tex
Executable file
@ -0,0 +1,78 @@
|
||||
\section{Auswahlmenu mit Select}
|
||||
\begin{flushleft}
|
||||
Mit einem {\ttfamily select} in einem Script kann man ein Auswahlmenu darstellen.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoSelect.sh]
|
||||
uws@tux>cat DemoSelect.sh
|
||||
#!/bin/env bash
|
||||
declare -A WERT # Array deklarieren
|
||||
WERT["Montag Backup"]="backup.Mo.sh"
|
||||
WERT["Dienstag Backup"]="backupDi.sh"
|
||||
WERT["Mittwoch Backup"]="backupMi.sh"
|
||||
|
||||
printf "\n\tBitte Backup ausw"ahlen: \n\n"
|
||||
#
|
||||
# Das Ausrufezeichen vor der Array Variable gibt die Array Index No.
|
||||
# aus und das @ Zeichen zwischen den [] gibt dann alle Index No. aus.
|
||||
#
|
||||
select Backup in "${!WERT[@]}";
|
||||
do
|
||||
BACKUPSCRIPT=${WERT[${BACKUP}]}
|
||||
TITLE="${BACKUP}"
|
||||
printf "\n\tDas ${TITLE} mit dem Script ${BACKUPSCRIPT} wird ausgefuehrt.\n\n"
|
||||
~/bin/${BACKUPSCRIPT}
|
||||
exit 0
|
||||
done
|
||||
|
||||
uws@tux>./DemoSelect.sh
|
||||
Bitte Backup auswaehlen:
|
||||
|
||||
1) Dienstag Backup
|
||||
2) Montag Backup
|
||||
3) Mittwoch Backup
|
||||
#? 2
|
||||
|
||||
Das Montag Backup wird mit dem Script backupMo.sh ausgefuehrt.
|
||||
|
||||
\end{lstlisting}
|
||||
|
||||
Hier ist noch ein weiteres Beispiel f"ur eine {\ttfamily select} Anweisung.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoSelect1.sh]
|
||||
uws@tux>cat DemoSelect1.sh
|
||||
#!/bin/env bash
|
||||
WDR2="http://www.wdr2.de"
|
||||
SPORT="http://www.sportschau.de"
|
||||
|
||||
printf "\n\tBitte Web Seite auswaehlen: \n\n"
|
||||
select A in WDR2\ Seite Sportschau\ Seite;
|
||||
do
|
||||
case $A in
|
||||
WDR*)
|
||||
printf "\n\tFolgende URL wird aufgerufen: ${WDR2}.\n\n"
|
||||
firefox ${WDR2}
|
||||
exit 0
|
||||
;;
|
||||
SPORT*)
|
||||
printf "\n\tFolgende URL wird aufgerufen: ${SPORT}.\n\n"
|
||||
firefox ${SPORT}
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
uws@tux>./DemoSelect1.sh
|
||||
|
||||
Bitte Web Seite auswaehlen:
|
||||
|
||||
1) WDR Seite
|
||||
2) Sportschau Seite
|
||||
#? 1
|
||||
|
||||
Folgende URL wird aufgerufen: http://www.wdr2.de.
|
||||
|
||||
\end{lstlisting}
|
||||
|
||||
\end{flushleft}
|
48
Kapitel4/BackupScript.tex
Executable file
@ -0,0 +1,48 @@
|
||||
\section{Backup Script}
|
||||
\begin{flushleft}
|
||||
Mit diesem Script wird ein Backup von einem Verzeichnis ersteelt und die Sicherungsarchive
|
||||
werden hochgez"ahlt. Ist das Sicherungsverzeichnis nicht vorhanden, so wird es angelegt.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Backup.sh, label=lst:bash]
|
||||
uws@tux>cat backup.sh
|
||||
#!/bin/env bash
|
||||
#
|
||||
# Seab@er Software - Backup Script
|
||||
#
|
||||
BACKUPDIR=/daten/backup
|
||||
SOURCEDIR=/daten/bilder
|
||||
TIMESTAMP=backup-timestamp.dat
|
||||
|
||||
# Backup Verzeichnis vorhanden?
|
||||
|
||||
if [ ! -d "${BACKUPDIR}" ]; then
|
||||
echo "Das Verzeichnis ${BACKUPDIR} ist nicht vorhanden"
|
||||
echo "und wird nun angelegt! "
|
||||
mkdir -p ${BACKUPDIR}
|
||||
fi
|
||||
set -- ${BACKUPDIR}/backup-???.tgz # Alle Backups einlesen in $1, $2 usw.
|
||||
lastname=${!#} # Letzter Backup Name
|
||||
backupnr=${lastname##*backup-} # Pfad und backup- entfernen
|
||||
backupnr=${backupnr%%.*} # Alles hinter dem ersten "." Entfernen
|
||||
backupnr=${backupnr//\?/0} # Keine Backups vorhanden, dann 0
|
||||
backupnr=$[10#${backupnr}] # Fuehrende Nullen entfernen, (siehe in
|
||||
# der Bash-Manpage unter base#)
|
||||
|
||||
if [ "$[backupnr++]" -ge 999]; then # Erhoehen des Wertes um 1
|
||||
echo "Error: Schon 999 Backups vorhanden! "
|
||||
exit 1
|
||||
fi
|
||||
|
||||
backupnr=000${backupnr} # Nullen voranstellen
|
||||
backupnr=${backupnr: -3} # Die letzten 3 Ziffern herausschneiden,
|
||||
# das Leerzeichen vor dem - ist noetig.
|
||||
filename=backup-${backupnr}.tgz
|
||||
echo "Sichere veraenderte Daten in ${filename}."
|
||||
|
||||
# Es erfolgt ein inkrementelles Backup, Schalter -g
|
||||
|
||||
tar -czf ${BACKUPDIR}/${filename} -g ${BACKUPDIR}/$ {TIMESTAMP} ${SOURCEDIR}
|
||||
\end{lstlisting}
|
||||
|
||||
\end{flushleft}
|
44
Kapitel4/Benutzereingabe.tex
Executable file
@ -0,0 +1,44 @@
|
||||
\section{Benutzereingabe}
|
||||
\begin{justify}
|
||||
M"ochte man den Benutzer eine Eingabe erm"oglichen, so wird hierzu der Befehl {\ttfamily read} verwendet. Werden mehr Worte als definierte Variablen eingegeben, so bekommt die letzte Variablen den Rest des Textes. Werden weniger Worte eingegeben, so bleiben die letzten Variablen leer.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoRead.sh, label=lst:bash]
|
||||
uws@tux>cat DemoRead.sh
|
||||
#!/bin/env bash
|
||||
printf "\nBitte Werte eingeben: \n"
|
||||
read str1 str2 str3 str4
|
||||
printf "\nFolgende Werte wurden eingegeben: $str1 $str2 $str3 $str4\n"
|
||||
\end{lstlisting}
|
||||
F"ur {\ttfamily read} gibt es folgende Optionen:
|
||||
\begin{table}[ht]
|
||||
\begin{tabular}{p{2cm}p{14cm}} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\rowcolor{hellgrau}\emph{\textbf{Optionen}} & \emph{\textbf{Beschreibung}} \\
|
||||
\hline
|
||||
\hline
|
||||
-n<anzahl> & Maximale Anzahl an Zeichen. Wurde die maximale Anzahl erreicht, so wird die Eingabe mit einem Enter automatisch abgeschlossen\\
|
||||
-s & Silent Mode, die Eingabe wird nicht angezeigt. z.B. bei einer Passwort Abfrage\\
|
||||
-t<sec> & Hiermit wird ein Timeout f"ur die Eingabe gesetzt. Der R"uckgabewert bei keiner Eingabe ist dann 0\\
|
||||
-p & Ausgabe eines Textes\\
|
||||
\end{tabular}
|
||||
\caption{Optionen f"ur Dialog}
|
||||
\end{table}
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Textausgabe, label=lst:bash]
|
||||
uws@tux>read -n1 -s -p "Press any key" CHOISE
|
||||
\end{lstlisting}
|
||||
Wenn nach {\ttfamily read} keine Variable angegeben wird, so kann man den eingegebenen Wert mit {\ttfamily reply} auswerten.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoReadReply, label=lst:bash]
|
||||
uws@tux>cat DemoReadReply.sh
|
||||
#!/bin/env bash
|
||||
# Default Value for read is reply
|
||||
printf "\nWas ist deine Lieblingsfarbe? \n"
|
||||
read
|
||||
printf "\nDeine Lieblingsfarbe ist: $REPLY\n"
|
||||
printf "\nWas ist dein Lieblingsverein? \n"
|
||||
read VEREIN
|
||||
printf "\nDein Lieblingsverein ist: $VEREIN\n"
|
||||
printf "und deine Lieblingsfarbe ist: $REPLY\n"
|
||||
exit 0
|
||||
\end{lstlisting}
|
||||
\end{justify}
|
20
Kapitel4/CheckRoot.tex
Normal file
@ -0,0 +1,20 @@
|
||||
\section{Check Root}
|
||||
\begin{flushleft}
|
||||
Soll das Script der User {\ttfamily root} ausführen und man möchte Überprüfen, das es sich um den User root handelt, so kann man das folgendermaßen machen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=CheckRoot.sh]
|
||||
@uws@tux>cat CheckRoot.sh
|
||||
#!/bin/env bash
|
||||
function CheckRootID() {
|
||||
ROOT_ID=$(id -u $USER)
|
||||
printf "Root ID: $ROOT_ID\n"
|
||||
|
||||
if [ "$ROOT_ID" == "0" ]
|
||||
then
|
||||
printf "Root User found.\n"
|
||||
else
|
||||
printf "This script run only under root account!\n"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
\end{lstlisting}
|
107
Kapitel4/DateiRead.tex
Normal file
@ -0,0 +1,107 @@
|
||||
\section{Datei einlesen}
|
||||
%\subsection{Up / Down}
|
||||
\begin{flushleft}
|
||||
Eine Datei kann man mit {\ttfamily IFS} und {\ttfamily read} einlesen. Die Datei wird direkt in Variablen gespeichert.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Read Datei]
|
||||
uws@tux>cat DemoRead.sh
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Einlesen der Datei mit dem Feldtrenner :
|
||||
while IFS=: read BENUTZERNAME A B C D HOMEDIR E; do
|
||||
printf "$BENUTZERNAME -> $HOMEDIR\n"
|
||||
done < /etc/passwd
|
||||
|
||||
# Als Tabelle
|
||||
printf "|%23s | %-30s |\n" "Username" "Homedir"
|
||||
printf "+%23s + %-30s +\n" "-----------------------" "------------------------------"
|
||||
while IFS=: read BENUTZERNAME A B C D HOMEDIR E; do
|
||||
printf "|%23s | %-30s |\n" $BENUTZERNAME $HOMEDIR
|
||||
done < /etc/passwd
|
||||
printf "+%23s + %-30s +\n" "-----------------------" "------------------------------"
|
||||
|
||||
# Einlesen der Datei mit dem Feldtrenner Leerzeichen
|
||||
while IFS=" " read A1 A2; do
|
||||
printf "$A1 : $A2\n"
|
||||
done < liste.txt
|
||||
|
||||
# Return code Auswertung
|
||||
declare -a RESULT
|
||||
for SRV in $(cat server.lst)
|
||||
do
|
||||
ssh -x ${USER}@${SRV} 'ls' 2> /dev/null
|
||||
status=$?
|
||||
if [[ $status -gt 0 ]]
|
||||
then
|
||||
RESULT+=("$SRV")
|
||||
printf "Server: ${SRV}\n"
|
||||
else
|
||||
printf " Ok: ${SRV}\n"
|
||||
fi
|
||||
done
|
||||
|
||||
for i in ${!RESULT[*]}
|
||||
do
|
||||
printf "Not Ok: ${RESULT[$i]}\n"
|
||||
done
|
||||
|
||||
uws@tux>cat liste.txt
|
||||
Erste Zeile
|
||||
Zweite Zeile
|
||||
Letzte Zeile
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoReadFile.sh]
|
||||
uws@tux>cat daten.xt
|
||||
Das ist Zeile 1.
|
||||
Das ist Zeile 2.
|
||||
Das ist Zeile 3.
|
||||
Das ist Zeile 4.
|
||||
|
||||
uws@tux>cat DemoReadFile.sh
|
||||
#!/bin/env bash
|
||||
while read line
|
||||
do
|
||||
echo -e "$line \n"
|
||||
done < daten.txt
|
||||
\end{lstlisting}
|
||||
Eine Datei einlesen, in der die Daten durch einen Separator getrennt sind.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoReadFileSeparator.sh]
|
||||
uws@tux>cat DemoReadFileSeparator.sh
|
||||
#!/bin/env bash
|
||||
while IFS=':' read user pass uid gid full home shell
|
||||
do
|
||||
echo -e "$full:\n\
|
||||
PSEUDO: $user\n\
|
||||
UID : $uid\n\
|
||||
GID : $gid\n\
|
||||
HOME : $home\n\
|
||||
Shell : $shell\n\n"
|
||||
done < /etc/passwd
|
||||
\end{lstlisting}
|
||||
Die Kommentar Zeilen einer Datei nicht auswerten. In dem nachfolgenden Beispiel werden sie extra ausgegeben.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoReadFileComments.sh]
|
||||
uws@tux>cat DemoReadFileComments.sh
|
||||
!#/bin/env bash
|
||||
i=1
|
||||
while IFS=';' SOURCE BACKUP
|
||||
do
|
||||
if [ "${SOURCE:0:1}" != "#" ]; then
|
||||
printf "\tQuell Path: $SOURCE\n"
|
||||
printf "\tBackup Path: $BACKUP\n\n"
|
||||
else
|
||||
printf "\tComment $((i++)); $SOURCE\n\n"
|
||||
fi
|
||||
done <$1
|
||||
\end{lstlisting}
|
||||
Eine Variable kann folgenderma\ss{}en eine Datei einlesen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=VariableRead]
|
||||
uws@tux>export VALUE=`cat pd.dat`
|
||||
|
||||
uws@tux>export VALUE=$(<pid.dat)
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
89
Kapitel4/DateiTabelle.tex
Executable file
@ -0,0 +1,89 @@
|
||||
\section{Datei als Tabelle ausgeben}
|
||||
\begin{flushleft}
|
||||
Eine Datei Zeilenweise einlesen kann mit einem {\ttfamily while read} gemacht werden.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Datei als Tabelle]
|
||||
uws@tux>cat DemoTable.sh
|
||||
#!/usr/bin/env bash
|
||||
# Als Tabelle
|
||||
printf "|%23s | %-30s |\n" "Username" "Homedir"
|
||||
printf "+%23s + %-30s +\n" "-----------------------" "------------------------------"
|
||||
while IFS=: read BENUTZERNAME A B C D HOMEDIR E; do
|
||||
printf "|%23s | %-30s |\n" $BENUTZERNAME $HOMEDIR
|
||||
done < /etc/passwd
|
||||
printf "+%23s + %-30s +\n" "-----------------------" "------------------------------"
|
||||
|
||||
uws@tux>./DemoTabelle.sh
|
||||
| Username | Homedir |
|
||||
+----------------------- + ------------------------------ +
|
||||
| root | /root |
|
||||
| nobody | / |
|
||||
| dbus | / |
|
||||
| bin | / |
|
||||
| daemon | / |
|
||||
| mail | /var/spool/mail |
|
||||
|
||||
\end{lstlisting}
|
||||
\newpage
|
||||
Eine automatische Ermittlung der Feldlänge kann man mit dem folgenden Beispiel machen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Datei als Tabelle]
|
||||
uws@tux>cat data.txt
|
||||
Hans;Wurst
|
||||
Andrea;Krautwurst
|
||||
Paulchen;Panther
|
||||
Karl;Mai
|
||||
Susanne;Sorglos
|
||||
|
||||
uws@tux>cat DemoTable1.sh
|
||||
#!/usr/bin/env bash
|
||||
pheader()
|
||||
{
|
||||
printf "+"
|
||||
for ((i=1;i<=${1};i++));do printf "-"; done
|
||||
printf "+"
|
||||
for ((i=1;i<=${2};i++));do printf "-"; done
|
||||
printf "+\n"
|
||||
}
|
||||
declare -i a b
|
||||
a=0
|
||||
b=0
|
||||
|
||||
while IFS=";" read f1 f2; do
|
||||
if [ ${#f1} -gt ${a} ];
|
||||
then
|
||||
a=${#f1}
|
||||
fi
|
||||
if [ ${#f2} -gt ${b} ];
|
||||
then
|
||||
b=${#f2}
|
||||
fi
|
||||
done < data.txt
|
||||
|
||||
# print table header
|
||||
|
||||
pheader ${a} ${b}
|
||||
printf "|%${a}s|%-${b}s|\n" "First" "Last"
|
||||
printf "|%${a}s|%-${b}s|\n" "Name" "Name"
|
||||
pheader ${a} ${b}
|
||||
|
||||
# print table values
|
||||
while IFS=";" read f1 f2; do
|
||||
printf "|%${a}s|%-${b}s|\n" $f1 $f2
|
||||
done < ./data.txt
|
||||
pheader ${a} ${b}
|
||||
|
||||
uws@tux>./DemoTable1.sh
|
||||
+--------+----------+
|
||||
| First|Last |
|
||||
| Name|Name |
|
||||
+--------+----------+
|
||||
| Hans|Wurst |
|
||||
| Andrea|Krautwurst|
|
||||
|Paulchen|Panther |
|
||||
| Karl|Mai |
|
||||
| Susanne|Sorglos |
|
||||
+--------+----------+
|
||||
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
18
Kapitel4/DateiWrite.tex
Normal file
@ -0,0 +1,18 @@
|
||||
\section{Datei schreiben}
|
||||
%\subsection{Up / Down}
|
||||
\begin{flushleft}
|
||||
Es gibt mehrere Möglichkeiten in einer Datei Daten / Informationen zu schreiben.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Write Datei]
|
||||
LOG=~/log/ausgabe.log
|
||||
# Ausgabe umleiten
|
||||
# Datei wird neu angelegt
|
||||
printf "Hier könnte ihre Werbung stehen.\n" > ${LOG}
|
||||
|
||||
# Daten an Datei anhängen
|
||||
printf "Werbung Werbung Werbung\n" >> ${LOG}
|
||||
|
||||
# Alternative mit tee -a = append
|
||||
printf "Und noch mehr Werbung.\n" | tee -a ${LOG}
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
39
Kapitel4/DateiZurLaufzeit.tex
Executable file
@ -0,0 +1,39 @@
|
||||
\section{Datei zur Laufzeit erstellen}
|
||||
\begin{flushleft}
|
||||
Eine Datei zur Laufzeit kann mit der sogenannten {\ttfamily Here-Dokumentation} erstellt werden. Als Trenner
|
||||
kann jede Zeichenfolge verwendet werden, meistens wird {\ttfamily EOF} als Trenner verwendet. Alles was
|
||||
zwischen den Trenner steht, wird in eine neue Datei ausgegeben.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoCreateFile.sh, label=lst:bash]
|
||||
uws@tux>cat DemoCreateFile.sh
|
||||
#!/bin/env bash
|
||||
cat <<EOF > "NewScript"
|
||||
#!/bin/env bash
|
||||
# Laufzeiterstelltes Script
|
||||
#
|
||||
printf "\n\tHier geht es los!\n"
|
||||
ls -lash
|
||||
EOF
|
||||
\end{lstlisting}
|
||||
|
||||
Wird in einem Script Taps verwendet, so muss ein {\ttfamily -} Zeichen vor dem Trenner gesetzt werden.
|
||||
Dann werden die Taps ignoriert.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoCreateFileTaps.sh, label=lst:bash]
|
||||
uws@tux>cat DemoCreateFileTaps.sh
|
||||
#!/bin/env bash
|
||||
cat <<-EOF > "NewScriptTaps"
|
||||
#!/bin/bash
|
||||
# Laufzeiterstelltes Script mit Taps
|
||||
#
|
||||
if test -r /home/uws/bin/test.txt
|
||||
then
|
||||
printf "\n\tHier geht es los!\n"
|
||||
ls -lash
|
||||
fi
|
||||
EOF
|
||||
\end{lstlisting}
|
||||
|
||||
\end{flushleft}
|
10
Kapitel4/EraseFiles.tex
Normal file
@ -0,0 +1,10 @@
|
||||
\section{Erase Files}
|
||||
%\subsection{Up / Down}
|
||||
\begin{flushleft}
|
||||
In einem Verzeichnis eine Datei löschen, aber nur, wenn das Verzeichnis auch vorhanden ist.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Erase Files]
|
||||
cd /path/to/delete || exit
|
||||
rm file
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
56
Kapitel4/EvalBeispiel.tex
Executable file
@ -0,0 +1,56 @@
|
||||
\section{Eval}
|
||||
\begin{flushleft}
|
||||
Mit {\ttfamily Eval} is es m"oglich, Kommandos auszuf"uhren, als w"urden sie in der Shell ausgef"uhrt.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Commands.sh, label=lst:bash]
|
||||
uws@tux>cat commands.sh
|
||||
#!/bin/env bash
|
||||
while true
|
||||
do
|
||||
printf "\tCommand: "
|
||||
read
|
||||
eval $REPLY
|
||||
done
|
||||
|
||||
uws@tux>./commands.txt
|
||||
Command: ls *.sh
|
||||
commands.sh DemoEval.sh
|
||||
Comand: echo $$
|
||||
16061
|
||||
Command: exit
|
||||
uws@tux>
|
||||
|
||||
\end{lstlisting}
|
||||
|
||||
Es besteht die M"oglichkeit, indirekt auf eine Variable zugreifen zu k"onnen. In dem n"achsten
|
||||
Beispiel wird dieses gemacht.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoEval.sh, label=lst:bash]
|
||||
uws@tux>cat commands.sh
|
||||
#!/bin/env bash
|
||||
Mo=backupMo
|
||||
Di=backupDi
|
||||
Mi=backupMi
|
||||
Do=backupDo
|
||||
Fr=backupFr
|
||||
Sa=backupSa
|
||||
So=backupSo
|
||||
|
||||
tag=`date +"%a"`
|
||||
eval backup=$$tag
|
||||
printf "\n\tDas Backup Script $backup wird ausgefuehrt.\n\n"
|
||||
./$backup
|
||||
|
||||
uws@tux>./DemoEval.sh
|
||||
|
||||
Das Backup Script: backupDi wird ausgefuehrt.
|
||||
./backupDi
|
||||
|
||||
\end{lstlisting}
|
||||
|
||||
Im ersten Durchlauf wird aus {\ttfamily \$\$tag => \$Di}. Die Variable {\ttfamily \$Di} hat den Vert {\ttfamily backupDi}
|
||||
und dieses wird im zweiten Durchlauf der Variable {\ttfamily backup} zugewiesen.
|
||||
|
||||
\end{flushleft}
|
38
Kapitel4/Index.tex
Executable file
@ -0,0 +1,38 @@
|
||||
\chapter{Beispiele}
|
||||
|
||||
%-------------------------------------------
|
||||
% load other documents
|
||||
%------------------------------------------
|
||||
\input{Kapitel4/BackupScript}
|
||||
\newpage
|
||||
\input{Kapitel4/Benutzereingabe}
|
||||
\newpage
|
||||
\input{Kapitel4/Teilstring}
|
||||
\newpage
|
||||
\input{Kapitel4/EvalBeispiel}
|
||||
\newpage
|
||||
\input{Kapitel4/AuswahlMenuSelect}
|
||||
\newpage
|
||||
\input{Kapitel4/DateiZurLaufzeit}
|
||||
\newpage
|
||||
\input{Kapitel4/Scriptoptionen}
|
||||
\newpage
|
||||
\input{Kapitel4/AusgabeVersion}
|
||||
%\newpage
|
||||
\input{Kapitel4/AusgabeDateiName}
|
||||
\input{Kapitel4/AusgabeDateiPath}
|
||||
\newpage
|
||||
\input{Kapitel4/LetBefehl}
|
||||
%\newpage
|
||||
\input{Kapitel4/NetworkUp}
|
||||
\include{Kapitel4/NetworkMAC}
|
||||
\input{Kapitel4/DateiRead}
|
||||
\newpage
|
||||
\input{Kapitel4/DateiTabelle}
|
||||
\input{Kapitel4/DateiWrite}
|
||||
\input{Kapitel4/Password}
|
||||
\input{Kapitel4/ListFiles}
|
||||
\input{Kapitel4/EraseFiles}
|
||||
\newpage
|
||||
\input{Kapitel4/CheckRoot}
|
||||
\include{Kapitel4/StringEncode}
|
21
Kapitel4/LetBefehl.tex
Executable file
@ -0,0 +1,21 @@
|
||||
\section{Let}
|
||||
\begin{flushleft}
|
||||
Mit {\ttfamily let} kann man Berechnungen durchf"uhren.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoLet.sh, label=lst:bash]
|
||||
uws@tux>let "m=4*1024" && echo -e "\tErgebnis: $m"
|
||||
Ergebnis: 4096
|
||||
|
||||
@uws@tux>cat DemoLet.sh
|
||||
#!/bin/env bash
|
||||
let wert1=20
|
||||
let wert2=10
|
||||
|
||||
let m=$wert1+$wert2 && echo -e "Ergebnis: $m"
|
||||
uws@tux>bash DemoGerade.sh
|
||||
|
||||
Wert von Day: 326
|
||||
Wert von MDAY: 0
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
11
Kapitel4/ListFiles.tex
Normal file
@ -0,0 +1,11 @@
|
||||
\section{List Files}
|
||||
%\subsection{Up / Down}
|
||||
\begin{flushleft}
|
||||
In einem Verzeichnis die Dateien auflisten.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=List Files]
|
||||
for f in *; do
|
||||
printf "$f\n"
|
||||
done
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
47
Kapitel4/NetworkMAC.tex
Executable file
@ -0,0 +1,47 @@
|
||||
\section{Generate MAC-Adresse / UUID}
|
||||
%\subsection{Up / Down}
|
||||
\begin{flushleft}
|
||||
Braucht man eine MAC-Adresse f"ur eine Netzwerkkonfiguration von virtuellen Maschinen und auch eine UUID dazu, so kann man das mit dem folgenden Script machen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Generiere MAC / UUID]
|
||||
uws@tux>cat GenMacUuid.sh
|
||||
#!/usr/bin/env bash
|
||||
# Generate MAC-Address and UUID
|
||||
# Author: Uwe Schimanski
|
||||
# Version: 19.01.08
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
printf "\nGenereate MAC-Address with prefix\n=================================\n"
|
||||
printf "MAC: 00-60-2F-%02X-%02X-%02X\n" $[RANDOM%256] $[RANDOM%256] $[RANDOM%256]
|
||||
printf "MAC: 00-60-2F-" && cut -b 7-11,24-26 /proc/sys/kernel/random/uuid
|
||||
printf "\nGenerate MAC-Address without prefix\n"
|
||||
printf "===================================\n"
|
||||
printf "MAC: %02X-%02X-%02X-%02X-%02X-%02X\n" $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256]
|
||||
|
||||
UUID1=$(tr -dc a-h0-9 < /dev/random | head -c 8 | xargs)
|
||||
UUID2=$(tr -dc a-h0-9 < /dev/random | head -c 4 | xargs)
|
||||
UUID3=$(tr -dc a-h0-9 < /dev/random | head -c 4 | xargs)
|
||||
UUID4=$(tr -dc a-h0-9 < /dev/random | head -c 4 | xargs)
|
||||
UUID5=$(tr -dc a-h0-9 < /dev/random | head -c 12 | xargs)
|
||||
printf "\nGenerate UUID\n"
|
||||
printf "=============\n"
|
||||
printf "UUID: $UUID1-$UUID2-$UUID3-$UUID4-$UUID5\n"
|
||||
printf "UUID: " && cat /proc/sys/kernel/random/uuid
|
||||
|
||||
uws@tux>bash GenMacUuid.sh
|
||||
|
||||
Genereate MAC-Address with prefix
|
||||
=================================
|
||||
MAC: 00-60-2F-90-FB-BB
|
||||
MAC: 00-60-2F-86-bc-60
|
||||
|
||||
Generate MAC-Address without prefix
|
||||
===================================
|
||||
MAC: E0-AF-5F-91-7E-30
|
||||
|
||||
Generate UUID
|
||||
=============
|
||||
UUID: 5df61c8g-7f5e-46d6-621b-ddgcg3a4gegf
|
||||
UUID: cf11bf04-e9bc-484f-8642-92ca5c636167
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
19
Kapitel4/NetworkUp.tex
Executable file
@ -0,0 +1,19 @@
|
||||
\section{Network}
|
||||
\subsection{Up / Down}
|
||||
\begin{flushleft}
|
||||
Ein einfacher Netzwerktest, ob das Netz vorhanden ist..
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoNetwork.sh, label=lst:bash]
|
||||
uws@tux>bash DemoNetwork.sh
|
||||
#!/bin/env bash
|
||||
HOST_NAME=\$1
|
||||
|
||||
printf "Warte auf das Netz"
|
||||
|
||||
while [ $(ping -w1 -c1 $HOST_NAME | grep -c "0 received") -eq 1 ]; do
|
||||
printf "."
|
||||
done
|
||||
printf "\nNetz ist up.\n"
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
17
Kapitel4/Password.tex
Normal file
@ -0,0 +1,17 @@
|
||||
\section{Generate Password}
|
||||
%\subsection{Up / Down}
|
||||
\begin{flushleft}
|
||||
Mit dem folgenden Funktion kann man sich ein Password erstellen lassen.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Generiere Password]
|
||||
genpasswd() {
|
||||
local l = $1
|
||||
[ "$1" == "" ] && l = 20
|
||||
tr -dc A-Za-z0-9_: < /dev/urandom | \
|
||||
head -c ${l} | xargs
|
||||
}
|
||||
|
||||
uws@tux># Oder
|
||||
uws@tux>shuf -er -n20 {A..Z}{a..z}{0..9} | tr -d '\n'
|
||||
\end{lstlisting}
|
||||
\end{flushleft}
|
68
Kapitel4/Scriptoptionen.tex
Executable file
@ -0,0 +1,68 @@
|
||||
\section{Scriptoptionen}
|
||||
\begin{flushleft}
|
||||
M"ochte man in einem Script Optionen / Parameter "ubergeben und auswerten, so geschieht
|
||||
das mit {\ttfamily getopts}. Alle Optionen / Parameter k"onnen in beliebiger Reihenfolge gesetzt werden.
|
||||
Erfolgt nach einer Option / Parameter ein {\ttfamily Doppelpunkt}, wie im unteren Beispiel nach dem
|
||||
{\ttfamily f}, so wird f"ur diese Option / Parameter ein Wert ben"otigt.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoOption.sh, label=lst:bash]
|
||||
uws@tux>cat DemoOption.sh
|
||||
#!/bin/env bash
|
||||
while getops "abf:hv" Arg; do
|
||||
case ${Arg} in
|
||||
a) printf "Die Option -a wurde "ubergeben.";;
|
||||
b) printf "Die Option -b wurde "ubergeben.";;
|
||||
f) if [ -f ${OPTARG} ]; then
|
||||
filename=${OPTARG}
|
||||
fi
|
||||
;;
|
||||
h) printf "Die Syntax lautet: ...";;
|
||||
v) verbose=y;;
|
||||
esac
|
||||
done
|
||||
\end{lstlisting}
|
||||
|
||||
Script Optionen / Parameter k"onnen auch mit einer Kombination von {\ttfamily while} und
|
||||
{\ttfamily case} ausgewertet werden.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoOptionWithCase.sh, label=lst:bash]
|
||||
uws@tux>cat DemoDemoOptionWithCase.sh
|
||||
#!/bin/env bash
|
||||
VERBOSE="0"
|
||||
OUTFILE=""
|
||||
|
||||
while [ $# -gt 0 ]
|
||||
do
|
||||
case $1 in
|
||||
-h|--help)
|
||||
printf "\n"
|
||||
printf "Folgende Optionen sind zulaessig:\n"
|
||||
printf "-h|--help : Diese Hilfe\n"
|
||||
printf "-v|--verbose : Statusinformatione n\n"
|
||||
printf "-o|--outfile <file> : Ausgabe in <file>\ n\n"
|
||||
exit 0
|
||||
;;
|
||||
-v|--verbose)
|
||||
VERBOSE="1"
|
||||
;;
|
||||
-o|--outfile)
|
||||
shift # Parameter $2 wird $1
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
printf "Error: No File Name\n" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
OUTFILE="$1"
|
||||
;;
|
||||
*)
|
||||
printf "Unbekannte Option: $1\n" 1>&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
\end{lstlisting}
|
||||
|
||||
\end{flushleft}
|
56
Kapitel4/StringEncode.tex
Normal file
@ -0,0 +1,56 @@
|
||||
\section{String Encode \& Decode}
|
||||
\begin{flushleft}
|
||||
Einen String Ver- und Entschlüsseln wird in diesem Beispiel mit {\ttfamily openssl} gemacht. Das ganze wurde in einer Funktion erstellt, kann aber auch als alleiniges Script gemacht werden.
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Function scrypt]
|
||||
#==========================================================================
|
||||
# Function: scrypt
|
||||
#==========================================================================
|
||||
# Encode and Decode a string.
|
||||
scrypt() {
|
||||
COLOR_GREEN='\033[0;32m'
|
||||
COLOR_BLUE='\033[0;34m'
|
||||
RESET='\033[0m'
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
printf "${COLOR_BLUE} Syntax und Info mit -h oder --help.${RESET}\n"
|
||||
fi
|
||||
|
||||
while [ $# -gt 0 ]
|
||||
do
|
||||
case $1 in
|
||||
-h|--help)
|
||||
printf "\n"
|
||||
printf "${COLOR_GREEN}Syntax:${RESET}\n"
|
||||
printf " $(basename $0) [-h|--help] [-e] [-d]\n\n"
|
||||
printf "${COLOR_GREEN}-e:${RESET}\n"
|
||||
printf " Verschlüsseln eines Strings.\n\n"
|
||||
printf "${COLOR_GREEN}-d:${RESET}\n"
|
||||
printf " Entschlüsslen eines Secrets.\n\n"
|
||||
;;
|
||||
-v|--version)
|
||||
printf "$(basename $0) ${Version}\n"
|
||||
printf "Copyright (C) 2025 Seab@er Software\n"
|
||||
printf "Lizenz GPLv3 +: GNU GPL Version 3 oder hoeher <https://www.gnu.org/licenses/gpl.html>\n"
|
||||
printf "Dies ist eine freie Software : Sie koennen sie aendern und weitergeben.\n"
|
||||
printf "Es gibt keinerlei Garantien , soweit wie es das Gesetz erlaubt.\n\n"
|
||||
printf "Geschrieben von Uwe Schimanski\n"
|
||||
;;
|
||||
-e|--encode)
|
||||
secret=$(echo ""$2"" | openssl enc -e -des3 -base64 -pass pass:Password -pbkdf2)
|
||||
printf "${COLOR_GREEN} Secret:${RESET} ${secret}\n"
|
||||
shift 2
|
||||
;;
|
||||
-d|--dencode)
|
||||
secret=$(echo ""$2"" | openssl enc -d -des3 -base64 -pass pass:Password -pbkdf2)
|
||||
printf "${COLOR_GREEN} Secret:${RESET} ${secret}\n"
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
printf "Unbekannte Option: $1\n" 1>&2
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
}
|
||||
\end{lstlisting}
|
41
Kapitel4/Teilstring.tex
Executable file
@ -0,0 +1,41 @@
|
||||
\section{Teilstring}
|
||||
\begin{flushleft}
|
||||
Einen String kann man mit verschiedenen Techniken zerlegen. Mit {\ttfamily \%} wird der String von
|
||||
rechts abgeschnitten und bei {\ttfamily \#} von links.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Teilstring Examples, label=lst:bash]
|
||||
uws@tux>FOO='1234567890'
|
||||
uws@tux>echo ${FOO:0:6}
|
||||
123456
|
||||
|
||||
uws@tux>echo ${FOO:2:3}
|
||||
345
|
||||
|
||||
uws@tux>FOO=mein.txt.dat
|
||||
echo ${FOO%.*}
|
||||
mein.txt
|
||||
|
||||
uws@tux>echo ${FOO%%.*}
|
||||
mein
|
||||
|
||||
uws@tux>FOO=/daten/test/mein.txt.dat # schneidet bis zum ersten / ab.
|
||||
uws@tux>echo ${FOO#*/}
|
||||
daten/text/mein.txt.dat
|
||||
|
||||
uws@tux>echo ${FOO#*.} # schneidet alles ab, bis zum ersten Punkt
|
||||
txt.dat
|
||||
|
||||
uws@tux>echo ${FOO##*/} # schneidet alles ab, bis zum letzen /
|
||||
mein.txt.dat
|
||||
|
||||
uws@tux>echo ${FOO##*.} # schneidet alles bis zum letzten Punkt ab
|
||||
dat
|
||||
|
||||
uws@tux>BAT_STAT=99%
|
||||
uws@tux>BAT_STAT=${BAT_STAT%%%*
|
||||
uws@tux>echo $BAT_STAT
|
||||
99
|
||||
\end{lstlisting}
|
||||
|
||||
\end{flushleft}
|
21
Kapitel4/WertGerade.tex
Executable file
@ -0,0 +1,21 @@
|
||||
\section{Wert gerade/ungerade}
|
||||
\begin{flushleft}
|
||||
M"ochte man wissen, ob ein Integer Wert einer Variable gerade oder ungerade ist, so gibt
|
||||
man den Parameter {\ttfamily %2} mit an. Ist der Wert eine {\ttfamily gerade} Zahl, so wird eine
|
||||
{\ttfamily 0} als R"uckgabewert ausgegeben. Bei einer ungeraden Zahl eine {\ttfamily 1}.
|
||||
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=DemoGerade.sh, label=lst:bash]
|
||||
@uws@tux>cat DemoGerade.sh
|
||||
#!/bin/env bash
|
||||
DAY=$(date +%j)
|
||||
typeset -i MDAY
|
||||
MDAY=$DAY%2
|
||||
printf "\nWert von DAY: ${DAY}\n"
|
||||
printf "\nWert von MDAY: ${MDAY}\n"
|
||||
|
||||
uws@tux>bash DemoGerade.sh
|
||||
|
||||
Wert von Day: 326
|
||||
Wert von MDAY: 0
|
||||
\end{lstlisting}
|
50
README.md
@ -1,3 +1,51 @@
|
||||
# MyBashScriptBook
|
||||
|
||||
Alles über Bash Coding
|
||||
Alles über Bash Coding
|
||||
|
||||
Folgende Themen werden in dem Buch beschrieben:
|
||||
|
||||
* Allgemein \
|
||||
 Variablen \
|
||||
 Datum in Bash Skripte \
|
||||
 Ein- und Ausgabe auf der Shell \
|
||||
 Set-Befehl im Bash Script \
|
||||
 Programm Parameter auswerten \
|
||||
 Shell Script testen \
|
||||
 Tabs setzen \
|
||||
 Befehle mehrzeilig \
|
||||
 Befehle verketten \
|
||||
 XARGS \
|
||||
 Signale (Traps) \
|
||||
 Exec
|
||||
* Schleifen / Bedingungen \
|
||||
 Vergleichsparameter \
|
||||
 Case Anweisung \
|
||||
 If Anweisung \
|
||||
 While / Until Schleife \
|
||||
 For Schleife \
|
||||
 Functions
|
||||
* GUI \
|
||||
 Yad \
|
||||
 Dialog
|
||||
* Beispiele \
|
||||
 Backup Script \
|
||||
 Benutzereingabe \
|
||||
 Teilstring \
|
||||
 Eval \
|
||||
 Auswahlmenu mit Select \
|
||||
 Datei zur Laufzeit erstellen \
|
||||
 Scriptoptionen \
|
||||
 Ausgabe Version \
|
||||
 Ausgabe Datei Name \
|
||||
 Aosgabe Datei Pfad \
|
||||
 Let \
|
||||
 Network \
|
||||
 Generate MAC-Adresse / UUID \
|
||||
 Datei einlesen \
|
||||
 Datei als Tablle ausgeben \
|
||||
 Datei schreiben \
|
||||
 Generate Password \
|
||||
 List Files \
|
||||
 Erase Files \
|
||||
 Check Root \
|
||||
 String Encode & Decode
|
37
Vorlage.tex
Executable file
@ -0,0 +1,37 @@
|
||||
\section{Bash}
|
||||
\begin{flushleft}
|
||||
it {\ttfamily .bashrc\index{.bashrc}} geladen werden.\\
|
||||
|
||||
%
|
||||
% Listing
|
||||
%
|
||||
\listBash
|
||||
\begin{lstlisting}[captionpos=b, caption=Beispiel .bashrc, label=lst:bash]
|
||||
umask 022
|
||||
\end{lstlisting}
|
||||
|
||||
%
|
||||
% Hier kommt eine Tabelle, 4 Spalten
|
||||
%
|
||||
% \emph und \textbf => Kursiv und Fett
|
||||
%
|
||||
|
||||
\begin{table}[ht] % Platzierung auf der Stelle und top
|
||||
\begin{tabular}{|l|l|l|p{8cm}|} % l => Text left, c => center, r => right, p => zeilenumbruch
|
||||
\hline \rowcolor{hellgrau}\emph{\textbf{Konfigurations}} & \emph{\textbf{Login}} & \emph{\textbf{Interaktive}} & \ \\
|
||||
\rowcolor{hellgrau}\emph{\textbf{Datei}} & \emph{\textbf{Shell}} & \emph{\textbf{Shell}} & \emph{\textbf{Beschreibung}}\\
|
||||
\hline /etc/profile & X & - & Systemweit, wird bei einem Update "uberschrieben.\\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\caption{Liste der Dateien}
|
||||
\end{flushleft}
|
||||
|
||||
%
|
||||
% Einfügen eines Bildes
|
||||
%
|
||||
\begin{figure}[ht]
|
||||
\centering % platzierung mitte
|
||||
\includegraphics[scale=0.5]{Kapitel3/Pictures/buildlist.png}
|
||||
\caption{Dialog buildlist}
|
||||
\end{figure}
|
||||
|
13
Vorwort.tex
Executable file
@ -0,0 +1,13 @@
|
||||
\chapter{Vorwort}
|
||||
% Mit "a wird der Umlaut ä
|
||||
\begin{justify}
|
||||
Ich besch"aftige mich schon seit dem Jahr 1993 mit Unix. Unsere CAD Software lief damals auf Unix Maschinen und 1998 habe ich dann Linux auf meinen Rechnern installiert. Damals habe ich SuSE installiert und jetzt l"auft OpenSuSE bei mir immer noch auf 2 Laptops. Auf den anderen Rechnern ist nun Manjaro installiert.\\[2ex]
|
||||
Die Shell unter Linux ist ein m"achtiges Werkzeug. Daher ist es unerl"asslich, das man sich mit Scripten das Leben erleichtert. Damit man ein Nachschlagewerk hat, habe ich mich entschlossen, eine Dokumentation "uber das Scripten in der Bash zu schreiben. Alle Scripte wurden in der Bash Shell ausprobiert und k"onnen auch in einer anderen Shell verwendet werden.\\[2ex]
|
||||
%\ \\
|
||||
Als OS Systeme wurden von mir OpenSuSE und Manjaro verwendet.\\[2ex]
|
||||
%\ \\
|
||||
Bei Fragen und Anregungen k"onnt ihr mir gerne ein Mail schicken.\\
|
||||
\begin{figure}[ht]
|
||||
\includegraphics[scale=0.3]{picture/Mail.png}
|
||||
\end{figure}
|
||||
\end{justify}
|
BIN
picture/Mail.jpeg
Executable file
After Width: | Height: | Size: 32 KiB |
BIN
picture/Mail.png
Executable file
After Width: | Height: | Size: 650 B |
10
picture/Mail.svg
Executable file
After Width: | Height: | Size: 14 KiB |
63
structure.tex
Executable file
@ -0,0 +1,63 @@
|
||||
%----------------------------------------------------------------------------------------
|
||||
% VARIOUS REQUIRED PACKAGES AND CONFIGURATIONS
|
||||
%----------------------------------------------------------------------------------------
|
||||
|
||||
\usepackage[ngerman]{babel}
|
||||
%\usepackage[latin1]{inputenc} % f<>r Umlaute
|
||||
\usepackage[T1]{fontenc}
|
||||
\usepackage{xcolor} % Farben f<>r die Box
|
||||
\usepackage{colortbl} % Farben f<>r Tabellen
|
||||
\usepackage{framed} % colorbox
|
||||
\usepackage{listings} % Listings
|
||||
\usepackage{caption} % Captions
|
||||
\usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm,headsep=10pt,a4paper]{geometry} % Page margins
|
||||
\colorlet{shadecolor}{gray!10} % f<>r die grau box
|
||||
\emergencystretch 1.5em %\hbox overfull error
|
||||
\usepackage[colorlinks=true,urlcolor=blue,linkcolor=green]{hyperref} % Für Hyperlinks
|
||||
\usepackage{graphicx} % Bilder einbinden
|
||||
\usepackage{makeidx} % Index Erstellung
|
||||
\usepackage{ragged2e} % Textausrichtung
|
||||
\usepackage{longtable} % Lange Tabellen ueber zwei Seiten
|
||||
|
||||
%----------------------------------------------------------------------------------------
|
||||
% Counter for TOC (subsubsection) Examle: 8.14.1.1
|
||||
%----------------------------------------------------------------------------------------
|
||||
\setcounter{tocdepth}{4}
|
||||
\setcounter{secnumdepth}{4}
|
||||
|
||||
%----------------------------------------------------------------------------------------
|
||||
% Define color
|
||||
%----------------------------------------------------------------------------------------
|
||||
|
||||
\definecolor{hellgrau}{rgb}{0.90,0.90,0.90} % Hellgrau
|
||||
\definecolor{verde}{rgb}{0.25,0.5,0.35}
|
||||
\definecolor{jpurple}{rgb}{0.5,0,0.35}
|
||||
\definecolor{darkgreen}{rgb}{0.0, 0.2, 0.13}
|
||||
|
||||
%----------------------------------------------------------------------------------------
|
||||
% Define new Commands
|
||||
%----------------------------------------------------------------------------------------
|
||||
|
||||
\newcommand{\listBash}{
|
||||
\lstset{
|
||||
language=bash,
|
||||
basicstyle=\ttfamily\small,
|
||||
keywordstyle=\color{jpurple}\bfseries,
|
||||
stringstyle=\color{red},
|
||||
commentstyle=\color{verde},
|
||||
morecomment=[s][\color{blue}]{/**}{*/},
|
||||
extendedchars=true,
|
||||
showspaces=false,
|
||||
showstringspaces=false,
|
||||
%numbers=left,
|
||||
numberstyle=\tiny,
|
||||
breaklines=true,
|
||||
backgroundcolor=\color{cyan!10},
|
||||
breakautoindent=true,
|
||||
captionpos=b,
|
||||
xleftmargin=0pt,
|
||||
tabsize=2
|
||||
}}
|
||||
|
||||
\newcommand{\bs}{\verb|\|}
|
||||
\renewcommand\familydefault{cmss} % Font: Computer Modern Sans
|