Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung | |||
| modul:m122:learningunits:lu08:module [2024/12/16 08:49] – [Beispiel] msuter | modul:m122:learningunits:lu08:module [2024/12/16 08:51] (aktuell) – [Beispiel] msuter | ||
|---|---|---|---|
| Zeile 83: | Zeile 83: | ||
| <code python> | <code python> | ||
| import sh | import sh | ||
| + | import sys | ||
| + | |||
| + | def run_bash_command(command): | ||
| + |     """ | ||
| + | Führt einen Bash-Befehl aus und behandelt Fehler. | ||
| + | |||
| + | :param command: Der auszuführende Bash-Befehl als String | ||
| + |     """ | ||
| + | try: | ||
| + |         print(f" | ||
| + | # sh führt den Befehl aus und fängt automatisch Fehler ab | ||
| + |         result = sh.Command(command.split()[0])(*command.split()[1: | ||
| + | print(result) | ||
| + | |||
| + | except sh.ErrorReturnCode as e: | ||
| + |         print(f' | ||
| + | sys.exit(e.exit_code) | ||
| + | |||
| + | except Exception as e: | ||
| + |         print(f' | ||
| + | sys.exit(1) | ||
| + | |||
| + | # Beispielbefehl | ||
| + | bash_command = 'ls -l / | ||
| + | run_bash_command(bash_command) | ||
| - | # Bash-Befehl ausführen | ||
| - | print(sh.ls(" | ||
| </ | </ | ||