Ich habe ein Shell-Skript mit vielen echo
drin. Ich möchte die Ausgabe in eine Logdatei umleiten. Ich weiß, es gibt den Befehlsaufruf cmd > logfile.txt
, oder es in der Datei echo 'xy' > logfile.txt
zu tun, aber ist es möglich, den Dateinamen einfach in das Skript zu setzen, das dann automatisch alle Echos in diese Datei schreibt?Redirect Echo-Ausgabe in Shell-Skript zu Logfile
10
A
Antwort
14
Sie diese Zeile oben auf Ihrem Skript hinzufügen:
#!/bin/bash
# redirect stdout/stderr to a file
exec &> logfile.txt
sonst nur StdOut Verwendung zu umleiten:
exec > logfile.txt
5
Sie leicht verschiedene Teile des Shell-Skript in eine Datei umleiten (oder mehrere Dateien) mit Sub-Shells:
{
command1
command2
command3
command4
} > file1
{
command5
command6
command7
command8
} > file2
8
Ich habe versucht, unter Verwendung Befehl zu verwalten. Dadurch wird die Ausgabe in die Protokolldatei geschrieben und auf der Konsole gedruckt.
#!/bin/bash
# Log Location on Server.
LOG_LOCATION=/home/user/scripts/logs
exec > >(tee -i $LOG_LOCATION/MylogFile.log)
exec 2>&1
echo "Log Location should be: [ $LOG_LOCATION ]"
0
LOG_LOCATION="/path/to/logs"
exec >> $LOG_LOCATION/mylogfile.log 2>&1
tun Sie * nur * 'echo's umleiten möchten? Gibt es andere Befehle, die die Ausgabe erzeugen? –
Keine andere Ausgabe außer Echos – wasp256