Zum Inhalt springen

Bash/Debug: Unterschied zwischen den Versionen

Aus Foxwiki
Keine Bearbeitungszusammenfassung
Markierung: Zurückgesetzt
K Dirkwagner verschob die Seite Bash/Debugging nach Bash/Debug
 
(20 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
== Debugging Bash scripts ==
'''{{BASEPAGENAME}}''' - Beschreibung


=== Debugging on the entire script ===
== Beschreibung ==


When things don't go according to plan, you need to determine what exactly causes the script to fail. Bash provides extensive debugging features. The most common is to start up the subshell with the <tt>-x</tt> option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.
== Gesamtes Skript ==
; Debugging für das gesamte Skript
Wenn die Dinge nicht nach Plan laufen, müssen Sie herausfinden, was genau die Ursache für das Scheitern des Skripts ist
* Die Bash bietet umfangreiche Debugging-Funktionen
* Die gebräuchlichste ist, die Subshell mit der Option <code>-x</code> zu starten, wodurch das gesamte Skript im Debug-Modus ausgeführt wird
* Die Spuren der einzelnen Befehle und ihrer Argumente werden auf der Standardausgabe ausgegeben, nachdem die Befehle expandiert wurden, aber bevor sie ausgeführt werden


This is the <tt>commented-script1.sh</tt> script ran in debug mode. Note again that the added comments are not visible in the output of the script.
Dies ist das <code>kommentierte-script1.sh</code> Skript, das im Debug-Modus ausgeführt wird
* Beachten Sie auch hier, dass die hinzugefügten Kommentare in der Ausgabe des Skripts nicht sichtbar sind
{| class="wikitable"
|
|}
Es gibt jetzt einen vollwertigen Debugger für die Bash, der bei SourceForge verfügbar ist
* Diese Debugging-Funktionen sind in den meisten modernen Versionen der Bash ab 3.x verfügbar


== Teile eines Skripts ==
; Debugging für Teile des Skripts
Mit dem '''set''' der Bash können Sie die Teile des Skripts, von denen Sie sicher sind, dass sie fehlerfrei sind, im normalen Modus ausführen und nur für problematische Bereiche Debugging-Informationen anzeigen
* Angenommen, wir sind uns nicht sicher, was der '''w'''-Befehl im Beispiel <code>kommentiertes-skript1.sh</code> bewirken wird, dann könnten wir ihn wie folgt in das Skript einschließen:
{| class="wikitable"
|
|}
Die Ausgabe sieht dann wie folgt aus:
{| class="wikitable"
|
|}
Sie können den Debugging-Modus innerhalb desselben Skripts beliebig oft ein- und ausschalten


{| style="border-spacing:0;width:17.173cm;"
Die folgende Tabelle gibt einen Überblick über weitere nützliche Bash-Optionen:
|- style="border:none;padding:0.049cm;"
|| willy:~/scripts> bash -x script1.sh
+ clear


+ echo 'The script starts now.'
'''Tabelle 2-1. Übersicht der eingestellten Debugging-Optionen'''
The script starts now.
{| class="wikitable"
+ echo 'Hi, willy!'
!Kurze Schreibweise
Hi, willy!
!Lange Schreibweise
+ echo
!Ergebnis
 
|-
+ echo 'I will now fetch you a list of connected users:'
|set -f
I will now fetch you a list of connected users:
|set -o noglob
+ echo
|Deaktiviert die Generierung von Dateinamen mit Metazeichen (Globbing)
 
|-
+ w
|set -v
4:50pm up 18 days, 6:49, 4 users, load average: 0.58, 0.62, 0.40
|set -o verbose
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
|Druckt Shell-Eingabezeilen aus, wenn sie gelesen werden
root tty2 - Sat 2pm 5:36m 0.24s 0.05s -bash
willy :0 - Sat 2pm ? 0.00s ? -
willy pts/3 - Sat 2pm 43:13 36.82s 36.82s BitchX willy ir
willy pts/2 - Sat 2pm 43:13 0.13s 0.06s /usr/bin/screen
+ echo
 
+ echo 'I'\''m setting two variables now.'
I'm setting two variables now.
+ COLOUR=black
+ VALUE=9
+ echo 'This is a string: '
This is a string:
+ echo 'And this is a number: '
And this is a number:
+ echo
 
+ echo 'I'\''m giving you back your prompt now.'
I'm giving you back your prompt now.
+ echo
|-
|-
|set -x
|set -o xtrace
|Druckt Befehlsspuren vor der Ausführung des Befehls
|}
|}
There is now a full-fledged debugger for Bash, available at [http://bashdb.sourceforge.net/ SourceForge]. These debugging features are available in most modern versions of Bash, starting from 3.x.
Der Bindestrich wird verwendet, um eine Shell-Option zu aktivieren und ein Plus, um sie zu deaktivieren
* Lassen Sie sich davon nicht verwirren!


=== Debugging on part(s) of the script ===
Im folgenden Beispiel werden diese Optionen in der Befehlszeile demonstriert:
{| class="wikitable"
|
|}
Alternativ können diese Modi auch im Skript selbst angegeben werden, indem die gewünschten Optionen in der ersten Zeile der Shell-Deklaration hinzugefügt werden
* Optionen können kombiniert werden, wie es bei UNIX-Befehlen üblich ist:


Using the set Bash built-in you can run in normal mode those portions of the script of which you are sure they are without fault, and display debugging information only for troublesome zones. Say we are not sure what the w command will do in the example <tt>commented-script1.sh</tt>, then we could enclose it in the script like this:
'''#!/bin/bash <code>-xv</code>'''


 
Wenn Sie den fehlerhaften Teil Ihres Skripts gefunden haben, können Sie '''echo'''-Anweisungen vor jedem Befehl einfügen, bei dem Sie unsicher sind, so dass Sie genau sehen, wo und warum etwas nicht funktioniert
{| style="border-spacing:0;width:17cm;"
* Im Beispielskript <code>commented-script1.sh</code> könnte man das so machen, wobei man immer noch davon ausgeht, dass die Anzeige der Benutzer Probleme macht:
|- style="border:none;padding:0.049cm;"
{| class="wikitable"
|| set -x <nowiki># activate debugging from here</nowiki>
|
w
|}
set +x <nowiki># stop debugging from here</nowiki>
In fortgeschritteneren Skripten kann das '''echo''' eingefügt werden, um den Inhalt von Variablen in verschiedenen Phasen des Skripts anzuzeigen, so dass Fehler aufgedeckt werden können:
|-
{| class="wikitable"
|
|}
|}
Output then looks like this:


== Installation ==
<syntaxhighlight lang="bash" highlight="1" line copy>
</syntaxhighlight>


{| style="border-spacing:0;width:17.173cm;"
== Aufruf ==
|- style="border:none;padding:0.049cm;"
<syntaxhighlight lang="bash" highlight="1" line copy>
|| willy: ~/scripts> script1.sh
</syntaxhighlight>
The script starts now.
Hi, willy!


I will now fetch you a list of connected users:
=== Optionen ===
 
{| class="wikitable sortable options gnu big"
+ w
|-
5:00pm up 18 days, 7:00, 4 users, load average: 0.79, 0.39, 0.33
! Unix !! GNU !! Parameter !! Beschreibung
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
|-
root tty2 - Sat 2pm 5:47m 0.24s 0.05s -bash
| || || ||
willy :0 - Sat 2pm ? 0.00s ? -
willy pts/3 - Sat 2pm 54:02 36.88s 36.88s BitchX willyke
willy pts/2 - Sat 2pm 54:02 0.13s 0.06s /usr/bin/screen
+ set +x
 
I'm setting two variables now.
This is a string:
And this is a number:
 
I'm giving you back your prompt now.
 
willy: ~/scripts>
|-
|-
|}
|}
You can switch debugging mode on and off as many times as you want within the same script.
The table below gives an overview of other useful Bash options:
Table 2-1. Overview of set debugging options


 
=== Parameter ===
{| style="border-spacing:0;width:16.385cm;"
=== Umgebungsvariablen ===
=== Exit-Status ===
{| class="wikitable options col1center big"
|-
|-
! align=center style="border-top:0.75pt double #808080;border-bottom:0.05pt double #808080;border-left:0.75pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | Short notation
! Wert !! Beschreibung
! align=center style="border-top:0.75pt double #808080;border-bottom:0.05pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | Long notation
! align=center style="border-top:0.75pt double #808080;border-bottom:0.05pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | Result
|-
|-
| style="border-top:none;border-bottom:0.05pt double #808080;border-left:0.75pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | set -f
| 0 || Erfolg
| style="border-top:none;border-bottom:0.05pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | set -o noglob
| style="border-top:none;border-bottom:0.05pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | Disable file name generation using metacharacters (globbing).
|-
| style="border-top:none;border-bottom:0.05pt double #808080;border-left:0.75pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | set -v
| style="border-top:none;border-bottom:0.05pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | set -o verbose
| style="border-top:none;border-bottom:0.05pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | Prints shell input lines as they are read.
|-
| style="border-top:none;border-bottom:0.75pt double #808080;border-left:0.75pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | set -x
| style="border-top:none;border-bottom:0.75pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | set -o xtrace
| style="border-top:none;border-bottom:0.75pt double #808080;border-left:0.05pt double #808080;border-right:0.75pt double #808080;padding:0.049cm;" | Print command traces before executing command.
|-
|-
| >0  || Fehler
|}
|}
The dash is used to activate a shell option and a plus to deactivate it. Don't let this confuse you!


In the example below, we demonstrate these options on the command line:
== Anwendung ==
<syntaxhighlight lang="bash" highlight="1" line copy>
</syntaxhighlight>


=== Problembehebung ===


{| style="border-spacing:0;width:17cm;"
== Konfiguration ==
|- style="border:none;padding:0.049cm;"
=== Dateien ===
|| willy:~/scripts> set -v
{| class="wikitable options big"
 
|-
willy:~/scripts> ls
! Datei !! Beschreibung
ls
|-
commented-scripts.sh script1.sh
| ||
 
willy:~/scripts> set +v
set +v
 
willy:~/scripts> ls <nowiki>*</nowiki>
commented-scripts.sh script1.sh
 
willy:~/scripts> set -f
 
willy:~/scripts> ls <nowiki>*</nowiki>
ls: *: No such file or directory
 
willy:~/scripts> touch <nowiki>*</nowiki>
 
willy:~/scripts> ls
<nowiki>* </nowiki> commented-scripts.sh script1.sh
 
willy:~/scripts> rm <nowiki>*</nowiki>
 
willy:~/scripts> ls
commented-scripts.sh script1.sh
|-
|-
| ||
|}
|}
Alternatively, these modes can be specified in the script itself, by adding the desired options to the first line shell declaration. Options can be combined, as is usually the case with UNIX commands:


<nowiki>#!/bin/bash </nowiki><tt>-xv</tt>
<noinclude>
 
== Anhang ==
=== Siehe auch ===
<div style="column-count:2">
<categorytree hideroot=on mode="pages">{{BASEPAGENAME}}</categorytree>
</div>
----
{{Special:PrefixIndex/{{BASEPAGENAME}}/}}


Once you found the buggy part of your script, you can add echo statements before each command of which you are unsure, so that you will see exactly where and why things don't work. In the example <tt>commented-script1.sh</tt> script, it could be done like this, still assuming that the displaying of users gives us problems:
=== Dokumentation ===
<!--
; Man-Page
# [https://manpages.debian.org/stable/procps/pgrep.1.de.html prep(1)]


; Info-Pages
-->


{| style="border-spacing:0;width:17cm;"
=== Links ===
|- style="border:none;padding:0.049cm;"
==== Projekt ====
|| echo "debug message: now attempting to start w command"; w
==== Weblinks ====
|-
|}
In more advanced scripts, the echo can be inserted to display the content of variables at different stages in the script, so that flaws can be detected:


[[Kategorie:Bash/Scripting]]


{| style="border-spacing:0;width:17cm;"
</noinclude>
|- style="border:none;padding:0.049cm;"
|| echo "Variable VARNAME is now set to $VARNAME."
|-
|}

Aktuelle Version vom 11. Oktober 2025, 13:24 Uhr

Bash/Debug - Beschreibung

Beschreibung

Gesamtes Skript

Debugging für das gesamte Skript

Wenn die Dinge nicht nach Plan laufen, müssen Sie herausfinden, was genau die Ursache für das Scheitern des Skripts ist

  • Die Bash bietet umfangreiche Debugging-Funktionen
  • Die gebräuchlichste ist, die Subshell mit der Option -x zu starten, wodurch das gesamte Skript im Debug-Modus ausgeführt wird
  • Die Spuren der einzelnen Befehle und ihrer Argumente werden auf der Standardausgabe ausgegeben, nachdem die Befehle expandiert wurden, aber bevor sie ausgeführt werden

Dies ist das kommentierte-script1.sh Skript, das im Debug-Modus ausgeführt wird

  • Beachten Sie auch hier, dass die hinzugefügten Kommentare in der Ausgabe des Skripts nicht sichtbar sind

Es gibt jetzt einen vollwertigen Debugger für die Bash, der bei SourceForge verfügbar ist

  • Diese Debugging-Funktionen sind in den meisten modernen Versionen der Bash ab 3.x verfügbar

Teile eines Skripts

Debugging für Teile des Skripts

Mit dem set der Bash können Sie die Teile des Skripts, von denen Sie sicher sind, dass sie fehlerfrei sind, im normalen Modus ausführen und nur für problematische Bereiche Debugging-Informationen anzeigen

  • Angenommen, wir sind uns nicht sicher, was der w-Befehl im Beispiel kommentiertes-skript1.sh bewirken wird, dann könnten wir ihn wie folgt in das Skript einschließen:

Die Ausgabe sieht dann wie folgt aus:

Sie können den Debugging-Modus innerhalb desselben Skripts beliebig oft ein- und ausschalten

Die folgende Tabelle gibt einen Überblick über weitere nützliche Bash-Optionen:

Tabelle 2-1. Übersicht der eingestellten Debugging-Optionen

Kurze Schreibweise Lange Schreibweise Ergebnis
set -f set -o noglob Deaktiviert die Generierung von Dateinamen mit Metazeichen (Globbing)
set -v set -o verbose Druckt Shell-Eingabezeilen aus, wenn sie gelesen werden
set -x set -o xtrace Druckt Befehlsspuren vor der Ausführung des Befehls

Der Bindestrich wird verwendet, um eine Shell-Option zu aktivieren und ein Plus, um sie zu deaktivieren

  • Lassen Sie sich davon nicht verwirren!

Im folgenden Beispiel werden diese Optionen in der Befehlszeile demonstriert:

Alternativ können diese Modi auch im Skript selbst angegeben werden, indem die gewünschten Optionen in der ersten Zeile der Shell-Deklaration hinzugefügt werden

  • Optionen können kombiniert werden, wie es bei UNIX-Befehlen üblich ist:

#!/bin/bash -xv

Wenn Sie den fehlerhaften Teil Ihres Skripts gefunden haben, können Sie echo-Anweisungen vor jedem Befehl einfügen, bei dem Sie unsicher sind, so dass Sie genau sehen, wo und warum etwas nicht funktioniert

  • Im Beispielskript commented-script1.sh könnte man das so machen, wobei man immer noch davon ausgeht, dass die Anzeige der Benutzer Probleme macht:

In fortgeschritteneren Skripten kann das echo eingefügt werden, um den Inhalt von Variablen in verschiedenen Phasen des Skripts anzuzeigen, so dass Fehler aufgedeckt werden können:

Installation

Aufruf

Optionen

Unix GNU Parameter Beschreibung

Parameter

Umgebungsvariablen

Exit-Status

Wert Beschreibung
0 Erfolg
>0 Fehler

Anwendung

Problembehebung

Konfiguration

Dateien

Datei Beschreibung


Anhang

Siehe auch



Dokumentation

Links

Projekt

Weblinks