This commit is contained in:
Daniel Schubert 2019-01-09 11:55:18 +01:00
parent 87205c16f0
commit fdfe2c362f
13 changed files with 344 additions and 46 deletions

248
5.html
View File

@ -61,7 +61,6 @@
<p>http://programmingexamples.wikidot.com/bash-scripting#toc24
<p>https://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_für_Anfänger/#Quoting
<div>
- script kontrolle ( strg-z etc )
@ -84,7 +83,9 @@ X=20 #variablen sind Case Sensitive
$y= #NULL variable
echo "x = $x"
echo "X = $X"
echo "y = $y"</code></pre>
echo "y = $y"
unset x #variable löschen
echo $x</code></pre>
</section>
<section>
@ -105,58 +106,225 @@ echo $(( 2#101011 )) # Base conversion, not a comment.
</section>
<section>
<h3>Quotes</h3>
<ul>
<li class="fragment ">Double Quotes: " " -Anything enclose in double quotes removed meaning of that characters (except \ and $).</li>
<li class="fragment ">Single quotes: ' ' - Enclosed in single quotes remains unchanged.</li>
<li class="fragment ">Back quote: ` ` - To execute command</li>
<h3>Quoting</h3>
<ul style="font-size: .8em ">
<li class="fragment "><code>" "</code> (Anführungszeichen) Alles zwischen diesen Zeichen ist buchstabengetreu zu interpretieren. Ausnahmen :<code> $ ` " \ </code></li>
<li class="fragment "><code>' ' </code>(Ticks) Alles zwischen diesen Zeichen wird wörtlich genommen, mit Ausnahme eines weiteren <code>'</code>.</li>
<li class="fragment "><code>\</code> (Backslash) Das Zeichen nach einem <code>\</code> wird wörtlich genommen. Anwendung z. B. innerhalb von " ", um ", $ und ` zu entwerten.</li>
<li class="fragment "><code>` `</code> (Backticks) Alles innerhalb wird als Kommando interpretiert.</li>
</ul>
</section>
<section>
<pre><code>$ echo 'Ticks "schützen" Anführungszeichen'
Ticks "schützen" Anführungszeichen
$ echo "Ist dies ein \"Sonderfall\"?"
Ist dies ein "Sonderfall"?
$ echo "Sie haben `ls | wc -l` Dateien in `pwd`"
Sie haben 43 Dateien in /home/dany
$ echo "Der Wert von \$x ist $x"
Der Wert von $x ist 100
# Maskierung der Leerstelle mit \
message="hallo Welt"
message=hello\ Welt
</code></pre>
</section>
<section><pre><code>echo 'So sprach'"'"'s und ging ohne einen Backslash (\) weiter.'
└────┬────┘└┬┘└─────────────────────┬─────────────────────┘
│ │ │
│ │ └ Dritter Bereich: Wieder
│ │ von ' umschlossen, der
│ │ Backlash verliert
│ │ seine Sonderbedeutung.
│ │
│ └ Zweiter Bereich: Von " umschlossen, enthält ein
│ einzelnes '.
└ Erster Bereich: Von ' umschlossen.</code></pre></section>
<section>
<h3>Input - Output redirection</h3>
<ul>
<li class="fragment "> > : output in Datei. Best. Datei wird überschrieben!</li>
<li class="fragment "> >> : output an Datei anhängen.</li>
<li class="fragment "> < : Kommando Input aus Datei lesen</li>
</ul>
<pre><code>#!/bin/bash
echo "ls > file_list"
# > : output in Datei. Best. Datei wird überschrieben!
ls > file_list
echo "ls -la >> file_list"
# >> : output an Datei anhängen.
ls -la >> file_list
echo "cat < file_list"
# < : Kommando Input aus Datei lesen
cat < file_list</code></pre>
</section>
<section>
- arithmetik
Man kann Kommandos auch verkettten:
<table style="font-size: .5em">
<tbody>
<tr>
<td >Kommando1; Kommando2
</td>
<td >fühert die Kommandos hintereinander aus
</td>
</tr>
<tr>
<td >Kommando1 && Kommando2
</td>
<td >führt Kommando2 aus, wenn kommando1 erfolgreich war
</td>
</tr>
<tr>
<td >Kommando1 || Kommando2
</td>
<td >führt Kommando2 aus, wenn kommando1 NICHT erfolgreich war
</td>
</tr>
<tr>
<td >Kommado &
</td>
<td >Führt Kommando im Hintergrund aus
</td>
</tr>
<tr>
<td >Kommando1 & Kommando2
</td>
<td >startet Kommando1 im Hintergrund, Kommando2 im Vordergrund
</td>
</tr>
<tr>
<td >( Kommando1; Kommando2 )
</td>
<td >Führt die beiden K. nacheinander in einer Subshell aus
</td>
</tr>
</tbody>
</table>
let "m = 4 * 1024";echo $m
<pre class="fragment"><code>sudo apt-get update && sudo apt-get upgrade -y
let "m += 15"
let "m -= 3"
let "m /= 5"
let "m %= 10"
let "m++"
let "m--"
tar cf archiv.tar && rm *.doc || echo "fehler"</code></pre>
</section>
let "k = (m < 9) ? 0 : 1"
condition ? value-if-true : value-if-false
<section>
<span style="color: orange">Aufgabe:</span> Erstellt ein script, das den Inhalt eures home in eine Variable <code>liste</code> schreibt, und gebt den Inhalt dieser Variablen in eine Textdatei <code>liste.txt</code> aus.<br />
Hinweis: ls , tee
<pre class="fragment"><code>#!/bin/bash
OUTFILE="liste.txt"
Floating Poin:
echo "32.0 + 1.4" | bc
liste=`ls | tee $OUTFILE `
echo `expr $m + 18`
m=`expr $m + 18`
echo $liste
(( m *= 4 ))
cat $OUTFILE
</code></pre>
</section>
<section>
<h3>exit status</h3>
<pre><code>exit 0</code></pre>
heisst : alles gut gelaufen
<pre><code>exit 1</code></pre>
heisst : kommando fehlgeschlagen
<p> der exit code in scripten kann beliebig gesetzt werden
<p>Standard: exit 0 -> alles fein
</section>
<section>
<h3>Funktionen</h3>
<pre><code>#!/bin/bash
#Funktions Deklaration:
count () {
ls | wc -l
# ls: Liste aller Objekte im Verzeichnis
# wc: Word-Count; mit Attribut -l werden Zeilen gezählt
# in Verbindung mit ls werden also die (nicht versteckten) Objekte gezählt
}
count # Aufruf der Funktion
</code></pre>
</section>
<section>
<pre><code>#!/bin/bash
count2 () {
# überprüfen, ob der erste Parameter ein Verzeichnis ist
if [ -d "$1" ]; then
ls $1 | wc -l
exit 0 # alles OK
else
echo "Ungültiges Verzeichnis: $1"
exit 1 # Fehler
fi
}
count2 "/gibt/es/garnicht" # Aufrufe der Funktion count2
echo "Status: $?"
count2 "/etc"
echo "Status: $?"</code></pre>
</section>
<section>
<div class="fragment"><pre><code>#!/bin/bash
tar -czf myhome_directory.tar.gz /home/$USER
mkdir archiv
mv myhome_directory.tar.gz archiv/
</code></pre>
</div>
</section>
<section>
<pre><code>
Integer Berechnungen
let "m = 4 * 1024";echo $m
let "m += 15"
let "m -= 3"
let "m /= 5"
let "m %= 10"
let "m++"
let "m--"
</code></pre>
let "k = (m < 9) ? 0 : 1"
condition ? value-if-true : value-if-false
<pre><code>
Floating Poin:
echo "32.0 + 1.4" | bc
echo `expr $m + 18`
m=`expr $m + 18`
(( m *= 4 ))
</code></pre>
</section>
<section data-transition="slide" data-background="#4d7e65" data-background-transition="zoom">
@ -164,17 +332,7 @@ cat < file_list</code></pre>
<section>
<h3>Die 5 Level eines Vim Magiers</h3>
<ul>
<li>Level 0: nichts über vim wissen
<li>Level 1: vim basics kennen
<li>Level 2: den visual mode kennen
<li>Level 3: diverse „motions“ kennen
<li>Level 4: den visual mode nicht brauchen
</ul>
<p>https://danielmiessler.com/study/vim/
</section>
</div>
</div>

0
beispiele/4/mysystem.sh Executable file → Normal file
View File

6
beispiele/5/01-vars.sh Normal file → Executable file
View File

@ -5,4 +5,8 @@ X=20 #variables are case sensitive
$y= #NULL variable
echo "x = $x"
echo "X = $X"
echo "y = $y"
echo "y = $y"
unset x
echo "x = $x"

0
beispiele/5/02-comments.sh Normal file → Executable file
View File

8
beispiele/5/03-quoting.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh
echo 'Ticks "schützen" Anführungszeichen'
echo "Ist dies ein \"Sonderfall\"?"
echo "Sie haben `ls | wc -l` Dateien in `pwd`"
echo "Der Wert von \$x ist $x"

View File

@ -0,0 +1,16 @@
#!/bin/bash
count2 () {
if [ -d "$1" ]; then # überprüfen, ob der erste Parameter ein Verzeichnis ist
ls $1 | wc -l # wie oben
exit 0 # alles OK
else
echo "Ungültiges Verzeichnis: $1"
exit 1 # Fehler
fi
}
count2 "/gibt/es/garnicht" # Aufrufe der Funktion count2
echo "Status: $?"
count2 "/etc"
echo "Status: $?"

13
beispiele/5/04-function.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh
fatal() {
msg=$1
echo "Fatal: $msg"
exit 1
}
[ -d /tmp ] || fatal "Verzeichnis /tmp existiert nicht"
[ -w /tmp ] || fatal "Verzeichnis /tmp nicht schreibbar"
TMP=/tmp/mydir
[ -d $TMP ] || mkdir $TMP # tmp-Verz. erzeugen, wenn noch nicht vorhanden

View File

9
beispiele/5/06-dialog.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
# Set PS3 prompt
PS3="Enter the space shuttle to get more information : "
# set shuttle list
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder
do
echo "$shuttle selected"
done

View File

@ -0,0 +1,48 @@
#!/bin/bash
# Set PS3 prompt
PS3="Enter the space shuttle to get more information : "
# set shuttle list
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder
do
case $shuttle in
columbia)
echo "--------------"
echo "Space Shuttle Columbia was the first spaceworthy space shuttle in NASA's orbital fleet."
echo "--------------"
;;
endeavour)
echo "--------------"
echo "Space Shuttle Endeavour is one of three currently operational orbiters in the Space Shuttle."
echo "--------------"
;;
challenger)
echo "--------------"
echo "Space Shuttle Challenger was NASA's second Space Shuttle orbiter to be put into service."
echo "--------------"
;;
discovery)
echo "--------------"
echo "Discovery became the third operational orbiter, and is now the oldest one in service."
echo "--------------"
;;
atlantis)
echo "--------------"
echo "Atlantis was the fourth operational shuttle built."
echo "--------------"
;;
enterprise)
echo "--------------"
echo "Space Shuttle Enterprise was the first Space Shuttle orbiter."
echo "--------------"
;;
pathfinder)
echo "--------------"
echo "Space Shuttle Orbiter Pathfinder is a Space Shuttle simulator made of steel and wood."
echo "--------------"
;;
*)
echo "Error: Please try again (select 1..7)!"
;;
esac
done

View File

36
beispiele/5/09-backup.sh Normal file
View File

@ -0,0 +1,36 @@
#!/bin/bash
# Get date in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"
function file_backup {
wpbackupfile="wordpress-backup-$NOW.tar.gz"
oxidbackupfile="oxid-back-$NOW.tar.gz"
echo 'WP Files Backup --- building tar File...'
tar -czf $wpbackupfile /var/html/www/rvv/wp*
echo '+DONE'
echo 'oxid Files Backup --- building tar File...'
tar -czf $oxidbackupfile /var/html/www/rvv/shop
echo '+DONE'
}
function oxid_db_backup {
host=localhost
user=rvv
pass=xxxxxxxxx
db=webshop
echo 'oxid DB Export ------> '
mysqldump --opt --add-drop-table -h$host -u$user -p$pass $db | gzip > oxid-db-$NOW.sql.gz
}
backupdir=~/backup
cd ~ ; mkdir $backupdir ; cd $backupdir
oxid_db_backup
file_backup
# delete old backups
find /home/dany/backups -mtime +10 -type f -delete

6
beispiele/5/aufgabe1.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
OUTFILE="liste.txt"
liste=`ls | tee $OUTFILE `
echo $liste
cat $OUTFILE