
Ich habe ein Skript, das rsyncs durchführt und so endet
rsync -azvh /dir -e ssh [email protected]:/
RESULT="$?"
# check result of rsync db's
if [ "$RESULT" != "0" ]; then
echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups"
else
echo "SUCCESSFULL rsync of backups"
fi
Ich wurde gerade gebeten, es in eine API einzubinden, aber die API besagt, dass 0=fail
und 1=success
. Wie kann ich den Exit-Code ändern, um dies widerzuspiegeln? Muss ich ihm eine Variable zuweisen?
Antwort1
exit 1
wird mit einem Fehlercode von 1 beendet und exit 0
wird mit einem Fehlercode von 0 beendet.
Zum Beispiel:
rsync -azvh /dir -e ssh [email protected]:/
RESULT="$?"
# check result of rsync db's
if [ "$RESULT" != "0" ]; then
echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups"
exit 0
else
echo "SUCCESSFULL rsync of backups"
exit 1
fi
Antwort2
Überarbeitete Antwort, Bericht
false
(oder(exit 1)
) undtrue
nach Bedarf gemäß nicht standardmäßiger API:rsync -azvh /dir -e ssh [email protected]:/ RESULT="$?" # check result of rsync db's if [ "$RESULT" = "0" ]; then echo "SUCCESSFULL rsync of backups" false else echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups" true fi
Wenn die gesamte Textausgabe nicht wirklich benötigt wird, könnte der Code erheblich reduziert werden:
! rsync -azvh /dir -e ssh [email protected]:/
rsync
... alles was nötig ist, ist , ein logisches NICHT voranzustellen!
und das Gegenteil von dem zurückzugeben, wasrsync
zurückgegeben wird.
Antwort3
ganz einfach per Bash:
test $((`echo "${PIPESTATUS[@]}" | tr ' ' '+'`)) -eq 0
echo $?