+ All Categories
Home > Documents > Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account...

Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account...

Date post: 16-Oct-2020
Category:
Upload: others
View: 11 times
Download: 0 times
Share this document with a friend
74
Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash
Transcript
Page 1: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Before we start● create a github account (or login)● create a project: docker● start a terminal with bash

Page 2: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

BashingEine kleine Einfuehrung in die Bash

Author: [email protected]

Lizenz CC BY-SA 3.0 DE

Page 3: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

MissionDer Workshop versucht dem Anfänger der Bash-Programmierung ein paar Klippen und deren Umschiffung aufzuzeigen. Alles was man leicht bei Google finden kann, weil man von selbst die richtige Frage stellt, wird nur kurz gestreift. Schwerpunktmäßig werden Fragen beantwortet, die man selbst nicht stellen würde, weil man nicht weiß, dass man die Antwort braucht.

Page 4: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Also

●Keine ewigen Wahrheiten●Keine abgefahrenen Tricks●Für Profis wenig Neues●Nur ein paar Tips zum Überleben

Page 5: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Für wen schreibe ich?

● Für mich oder andere?● Interaktiv oder batch?

● Erste Annahme:für mich● Mehr später ...

Page 6: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Anfang

● Man nehme einen EditorTYPE #!/bin/bash● In die erste Zeile

Wirklich ?

Page 7: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Wir haben einen Vorgang

● Tippen immer wieder die gleichen Befehle ins Terminal

● Geht das nicht besser?

Page 8: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

.bash_history

● Guter Anfang!● Aber nicht von Dauer!● Also skript: Type: bash

Page 9: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Beispiel: git

➢ mkdir $HOME/bin➢ mkdir $HOME/git➢ cd $HOME/git ➢ git clone https://github.com/koospal/docker

➢ cd docker➢ touch Dockerfile➢ git add Dockerfile➢ git commit➢ git push

Page 10: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

.bash_history - skript Type:bash exittail $HOME/.bash_history > $HOME/bin/chggitprojectchmod +x $HOME/bin/chggitproject

Page 11: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Umgebungsvariablen ➢ echo $HOME➢ echo $EDITOR➢ $EDITOR $HOME/bin/chggitproject

Page 12: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Skeleton - Minimalform #!/bin/bash #main

Page 13: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Wozu das Ganze?

Vorgänge auf der Commandline automatisieren!

Page 14: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Wie?

Meist Quick and Dirty!

1)Auf der Commandline ausprobieren2)In das Skeleton kopieren3)Verfeinern

Page 15: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Skeleton mit Parameter #!/bin/bash

#parameter check #main

Page 16: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Parameter

${1} .. ${10} .. $0 = Skriptname wie aufgerufen$# = Anzahl der Parameter

$$ = Prozessid

Page 17: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Variable

bestehen aus:Bezeichner = Zeiger auf SpeicherstelleWert = NULL terminated String Auswertung durch $

Zuweisung durch =

Page 18: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Variable auslesen

BINDSP=”dsp”echo ${BINDSP}echo ${BINDSP}logNormalerweise alle global

Page 19: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Skeleton mit USAGE

#!/bin/bashUSAGE="$0 parameter1"NUMBPARA=1#Kein Leerzeichen vor und nach =#parameter checkif [ $# != $NUMBPARA ]; then echo $USAGE exit 1fi #main

Page 20: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Skeleton Umgang Parameter

#assign parameterproject=$1#mainecho kom: using ${project}

Page 21: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Skript main

mkdir $HOME/git > /dev/NULL 2>&1cd $HOME/git git clone \https://github.com/koospal/${project}cd docker$EDITOR Dockerfilegit add Dockerfilegit commitgit pushexit 0

Page 22: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

stdin,stdout,stderr,pipe

wie in C stdin < stdout > stderr 2Pipes |

Page 23: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

stdin,stdout,stderr,pipe

Type:ls > /tmp/files.lstcat < /tmp/files.lstmkdir /tmp/abcmkdir /tmp/abc >/tmp/o 2>&1ls -l |wc -l

Achtung: Betriebssystem !

Page 24: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Variablen zuweisen

Type:NO_OF_FILES=`ls -l |wc -l`● Standardout einer Befehlsfolge

Type:read PRAEFIX● read vom Stdin

Page 25: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

for mit Liste

Type:echo *for i in *; do echo $idone

Page 26: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

*, ? und Listen

● * und ? fuer Dateilistenecho a?b*c● Listen aus Textdateiencat /tmp/files.lst● erzeugte Listen`echo *`

Page 27: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

for zaehlschleife

● Type:for ((i=1;i<10;i++));do echo $idone

Page 28: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Bsp: Logfiles auswerten

●Es gibt viele Tools z.B. Webalizer

●Aber manchmal passen die nicht

Page 29: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Logfiles Beispiel

https://java.de/b/log.csvOder gitAufgabe: cups logs auswerten

Page 30: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Commandlineprogramme

●ls, echo, mkdir, cd●git●Werden mit Parametern aufgerufen

●Input oft von stdin●Output meist auf stdout

Page 31: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

cat

●Ein-/Ausgabe einer Datei

●Oft ein guter Anfang

cat log.csv

Page 32: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

grep

●Ausgabe von Zeilen einer Textdatei, in der ein Muster vorkommt

cat log.csv|grep total

Page 33: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Nur einige Spalten ...

20 +0100] total 227 +0100] total 0Nur != 0

...|awk '{if ($10 != 0)

Page 34: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

awk

Achtung noch eine Programmiersprache!

●Zeilenweise Bearbeitung von strukturierten Textstreams

●Das “Schweizer Messer”●Solaris/MAC/usw: evtl. gawk

print $6,$4,$5,$10}'

Page 35: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

sort

Zeilenweise sortieren von Textstreams

|sort -n -u

Page 36: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

awk mit BEGIN und END

● Initialsierung und Abschluss

|awk '\ BEGIN {z=0}\ {print $0;z=z+$4}\ END{print "sum",z}\ '

Page 37: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Einbau ins Skeleton

Entscheidung:● Mit Parameterbehandlung ● cat $1|oder● nur als Filter

Page 38: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Weshalb cat ?

● Erleichtert den Einbau weiterer Filter

Page 39: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Bsp: Daten von Webseiten

Vorteile bash :●Schnelle Lösung●Nicht 100%Alternativen bedenken:●Andere Sprache?●Fertige Tools?

Page 40: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Emailadressen sammeln

https://www.uni-math.gwdg.de/staff/v2/mitarbeiter.html

Oderhttps://java.de/b/web.htmlcurl -k https://www.uni-math.gwdg.de/staff/v2/mitarbeiter.html

Oder auf github

Page 41: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

cat und grep

cat web.html|grep @●Mal sehen, was geht<td><a class="hplink" href="http://www.uni-math.gwdg.de/tammo/" target="_top">tom Dieck, Tammo</a>, Prof. Dr.<br /><span class="email">[email protected]</span></td>

|grep "class=\"email"

Page 42: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

sed

●Streameditor – Verarbeitung von Textstreams

● z.B. um Störendes zu entfernen

|sed -e "s/<td>.*email//g"

●Nutzt Regular Expressions

Page 43: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

awk mit Fieldseparator

●Zum einfachen extrahieren

|sed -e "s/<\/span//g"\ |awk -F">" '{print $2}'

● F kann regualar Expression sein

Page 44: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Batchbetrieb

●Achtung Falle: PATH !●Programme, die beim USER im Pfad sind, sind es nicht unbedingt bei root

●Je nach Pfad werden verschiedene Versionen der Programme genutzt: solaris-awk oder gnu-awk

Page 45: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Bsp.: Konvertierung

●ics → csv → txt-Liste● http://java.de/roller/blog/page/user_group_treffen→

● http://java.de/ijugtermine.txt

Page 46: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Ein Vorschlag für Kommentare

COMM="echo comment: "COMM=":"NOTE="echo note: "DEBUG="echo debug: "DEBUG=":": bedeutet eingentlich true

Page 47: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Etwas Pseudocode

urlf=icsurl.csv

$COMM create url list$COMM loop all urlsfor url in `awk -F";" '{print $2}' $urlf`; do $NOTE get ics file $url $COMM convert ics file to csv filedone $COMM sort all entries by date$COMM loop all entries

Page 48: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Fehlerbehandlung for url in `awk -F";" '{print $2}' $urlf`; do $NOTE get ics file $url

●Was tun bei einem Fehler?

done

Page 49: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Vorschlag: Funktionen

ERRORSUM=0ERRORVALUES=""ERRORTEXTS=""

errorhandler () {local lasterror=$1local behave=$2local errortext=$3$COMM "$1=errorvalue $2=INIT/CONT/ENDE/STOP $3=text"}

Page 50: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Lokale Variable

●Innerhalb eines Blocks{}●Vor allem in Funktionen

Page 51: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Unbound Variables

echo ${NOINITIAL}oder Schreibfehlerecho ${NO_OF_FILE}

set -u● Skriptabbruch bei unbound Variable

Page 52: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

date – Datum und Zeit

local TIMESTAMP=`date +"%Y%m%d%H%M%S"`

●Kann viel ●Achtung: SystemabhängigLinux: date --date=100 "+%s"MAC: date --date=100 "+%s"

Page 53: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

case – der Schalter

case "$behave" in "INIT") $NOTE $TIMESTAMP $errortext;return $lasterror;; "CONT") return $lasterror;; "ENDE") if [[ $lasterror == 0 ]];then return $lasterror;fi ;; "STOP") $NOTE $TIMESTAMP $errortext;; *) ;;esac

●Kann auch regular Expressions

Page 54: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

if – Bedingung

if [ $lasterror == 0 ];then return $lasterror;fi

●Eigentlich steht da:if test $lasterror == 0 bzw. if test $lasterror -eq 0

●Hauptfehler:Leerzeichenif [[ $lasterror == 0 ]];then return $lasterror;fi

●Tipp[[ ]],== usw. Nutzen●Man kann sehr viel testenz.B. Ist es eine leer Datei?

Page 55: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Rechnen

ERRORSUM=$((ERRORSUM+lasterror))

Ist doch ganz einfach: let a=1+2 a=$((1+4))Integerarithmetik sizeof(int),also 64bit

Page 56: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Rechnen mit float

LANG=de_DE.UTF-8;printf "%f" 2LANG=en_US.UTF-8;printf "%f" 2

LANG=en_US.UTF-8echo 1.1 2|\awk '{printf "%2.2f",$1*$2}'Es gibt auch bc

Page 57: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Weshalb so?

●Ich muss mir nur eine Syntax merken (awk)

●Formatierung eingebaut●Variable, die nicht als Zahl interpretiert werden kann, wird 0

Page 58: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Weshalb will ich das?● Variable, die nicht als Zahl interpretiert werden kann, wird 0

a=1if [[ a != 0 ]]; then echo ja;fiif [[ $a != 0 ]]; then echo ja;fia=1.5if [[ $a > 0 ]]; then echo ja;fiif [[ $a > 1.1 ]]; then echo ja;fi

Page 59: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

exit Fehlerstatus

Sollte das letzte Kommando jedes Skriptes sein.

Page 60: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

“Libaries”

●Die errorfunctions und die initfunctions packe ich in ein “Libary”

#!/bin/bash

. /opt/bin/initfunctions

. /opt/bin/errorfunctionsCOMM=":"errorhandler 0 INIT $0

Page 61: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Große Zahlen

● Pythonskript aufrufen● Beispiel IBAN

Page 62: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Arrays

declare -A dayoftheweekdayoftheweek["Mon"]="mo"dayoftheweek["Tue"]="di"dayoftheweek["Wed"]="mi"dayoftheweek["Thu"]="do"dayoftheweek["Fri"]="fr"dayoftheweek["Sat"]="sa"dayoftheweek["Sun"]="so"

●Assoziativer Arrays

Page 63: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Warten

sleep <Sekunden>

wait <prozess id>startbackroundcmd &pid_startbackroundcmd=$!

Page 64: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Kopf und Fuss

head <textfile> head -1 tabelle.csvtail <textfile> tail -f /var/log/syslog

Page 65: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Wo und was

which <command> which echofile <list of files> file `which echo`find <dir> [options]

Page 66: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Dateien jonglieren

diff <file1> <file2>join <s-file1> <s-file2>tar <options> <tarfile> Warnung vor cp -R besser tar

Page 67: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Endlos und bedingt

Endlosschleifewhile [[ 1 ]]; do echo x;read doneBedingte Ausführungls log.csv && echo ja

Page 68: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Viele Parameter - shift#check all files in /bincd bin;chkallf *#!/bin/bashset -uif [[ $# < 1 ]]; then echo zu wenig Parameterfiwhile [[ $# > 0 ]]; do file $1 shiftdone

Page 69: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Portabel Programmieren

awk=/usr/bin/gnu/awk

cat log.csv| $awk ...

Page 70: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

awk Systemcalls

awk \'{dir="/bin";\cmd="ls "dir;\system(cmd)}'

Page 71: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

vi – manchmal ist er da

ESC (vielleicht mehrmals)→BefehlsmodusDann::q!

Page 72: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

bash ≠ sh

bash v3 ≠ bash v4 ≠ bash v5

[[ ]] (( ))gibt es nur in der bash

Page 73: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Links: http://openbook.rheinwerk-verlag.de/shell_programmierung/

https://www.uni-math.gwdg.de/koospal/website/vortraege/

Page 74: Before we start - uni-goettingen.de€¦ · Java Land 2019 Before we start create a github account (or login) create a project: docker start a terminal with bash

Java Land 2019

Variable und Typen

Varibalen sind Bezeichner,die auf eine Speicherstelleim Hauptspeicher zeigen.Der Wert ist die Bitfolgean der Speicherstelle.Typ ist immer String.


Recommended