Erstellen von Shell-Skripten: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
Zeile 42: Zeile 42:
  
 
* '''pgrep''' gibt manchmal nicht genug Informationen aus
 
* '''pgrep''' gibt manchmal nicht genug Informationen aus
 +
* '''pgrep -a sshd'''
 +
 +
<!----->
 +
453 sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups
 
* '''ps alx | grep ''<Programm>''''' verbirgt die Kopfzeilen
 
* '''ps alx | grep ''<Programm>''''' verbirgt die Kopfzeilen
 +
 +
<!----->
 +
4    0    453      1  20  0  10192  6644 -      Ss  ?          0:00 sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups
 +
0  1000  81102  81042  20  0  6588  2512 pipe_r S+  pts/3      0:00 grep --color sshd
 
* Ziel ist es ein Skript zu schreiben, das mehr Informationen ausgibt als '''pgrep'''
 
* Ziel ist es ein Skript zu schreiben, das mehr Informationen ausgibt als '''pgrep'''
 +
* '''ps alx | head -n 1'''
 +
 +
<!----->
 +
F  UID    PID    PPID PRI  NI    VSZ  RSS WCHAN  STAT TTY        TIME COMMAND
 +
;Gesamter Befehl: + '''ps alx | head -n 1 &amp;&amp; ps alx | grep sshd | grep -v grep'''
 +
 +
<!----->
 +
F  UID    PID    PPID PRI  NI    VSZ  RSS WCHAN  STAT TTY        TIME COMMAND
 +
4    0    453      1  20  0  10192  6644 -      Ss  ?          0:00 sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups
 +
* das letzte '''grep -v grep''' verbirgt den Programm selbst
  
 
<!----->
 
<!----->

Version vom 14. Februar 2023, 13:22 Uhr

Grundlagen

Logische Operatoren

  • Jedes Programm generiert beim Beenden ein exit code (oder return code)
  • Ein Wert von 0 bedeutet, dass das Programm erfolgreich ausgeführt wurde
  • Je nach Fehlerart gibt das Programm einen höheren Wert zurück
  • den exit code des letzten Befehls kann man mit echo $? abfragen

UND

  • Befehl 1 && Befehl 2
  • Befehl 2 wird ausgeführt, wenn Befehl 1 mit exit code 0 beendet
  • ls / && echo "hat funktioniert"
bin   etc         initrd.img.old  lib64       media  proc  sbin  tmp  vmlinuz
boot  home        lib             libx32      mnt    root  srv   usr  vmlinuz.old
dev   initrd.img  lib32           lost+found  opt    run   sys   var
hat funktioniert

ODER

  • Befehl 1 || Befehl 2
  • Befehl 2 wird ausgeführt, wenn Befehl 1 mit exit code >0 beendet
  • ls nichtexistentedatei || echo "hat nicht funktioniert"
ls: Zugriff auf 'nichtexistentedatei' nicht möglich: Datei oder Verzeichnis nicht gefunden
hat nicht funktioniert

Beispiele

pgreppier

  • pgrep gibt manchmal nicht genug Informationen aus
  • pgrep -a sshd
453 sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups
  • ps alx | grep <Programm> verbirgt die Kopfzeilen
4     0     453       1  20   0  10192  6644 -      Ss   ?          0:00 sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups
0  1000   81102   81042  20   0   6588  2512 pipe_r S+   pts/3      0:00 grep --color sshd
  • Ziel ist es ein Skript zu schreiben, das mehr Informationen ausgibt als pgrep
  • ps alx | head -n 1
F   UID     PID    PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
Gesamter Befehl
+ ps alx | head -n 1 && ps alx | grep sshd | grep -v grep
F   UID     PID    PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0     453       1  20   0  10192  6644 -      Ss   ?          0:00 sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups
  • das letzte grep -v grep verbirgt den Programm selbst

del

  • rm löscht Dateien, ohne sie dabei in einen Papierkorb zu verschieben