Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
modul:m122:learningunits:lu08:module [2024/12/02 09:18] msutermodul:m122:learningunits:lu08:module [2024/12/16 08:51] (aktuell) – [Beispiel] msuter
Zeile 12: Zeile 12:
 import subprocess import subprocess
  
-Einen Bash-Befehl ausführen +bash_command = 'cat /var/log/error.log | grep SEVERE' 
-result = subprocess.run(['ls''-l']capture_output=True, text=True)+Den Bash-Befehl ausführen 
 +try: 
 +    result = subprocess.run( 
 +        bash_command, 
 +        shell=True # Use a shell to interpret the command 
 +        check=True,  # Raise an error if the command fails 
 +        text=True,  # Capture output as a string 
 +        stdout=subprocess.PIPE,  # Redirect standard output 
 +        stderr=subprocess.PIPE  # Redirect standard error 
 +    ) 
 +    return result 
 +except subprocess.CalledProcessError as e: 
 +    print(f'Error executing the command: {e.cmd}'
 +    print(f'Error message: {e.stderr}'
 +    sys.exit(1) 
 +except Exception as ex: 
 +    print(f'An unexpected error occurred: {ex}'
 +    sys.exit(1)
  
 # Ausgabe anzeigen # Ausgabe anzeigen
Zeile 29: Zeile 46:
 <code python> <code python>
 import os import os
 +import sys
  
-# Einen Bash-Befehl ausführen +def run_bash_command(command): 
-os.system('echo "Hallo Bash von Python!"')+    """ 
 +    Führt einen Bash-Befehl aus und behandelt Fehler. 
 + 
 +    :param command: Der auszuführende Bash-Befehl als String 
 +    """ 
 +    try: 
 +        print(f'Running command: {command}'
 +        # os.system gibt den Exit-Status des Befehls zurück 
 +        exit_status = os.system(command) 
 + 
 +        if exit_status != 0: 
 +            print(f'Fehler: Der Befehl '{command}' schlug fehl mit Exit-Status {exit_status}'
 +            sys.exit(exit_status) 
 +        else: 
 +            print('Befehl erfolgreich ausgeführt!'
 + 
 +    except Exception as e: 
 +        print(f'Ein unerwarteter Fehler ist aufgetreten: {e}'
 +        sys.exit(1) 
 +         
 +bash_command = 'ls -l /nonexistent_directory' 
 +run_bash_command(bash_command)
 </code> </code>
  
Zeile 44: 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"Running command: {command}")
 +        # 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'Fehler: Der Befehl '{command}' schlug fehl mit Fehler: {e.stderr.decode().strip()}')
 +        sys.exit(e.exit_code)
 +
 +    except Exception as e:
 +        print(f'Ein unerwarteter Fehler ist aufgetreten: {e}')
 +        sys.exit(1)
 +
 +# Beispielbefehl
 +bash_command = 'ls -l /nonexistent_directory'
 +run_bash_command(bash_command)
  
-# Bash-Befehl ausführen 
-print(sh.ls("-l")) 
 </code> </code>
  
  • modul/m122/learningunits/lu08/module.1733127530.txt.gz
  • Zuletzt geändert: 2024/12/02 09:18
  • von msuter