运行2个rsync命令并将输出打印到日志文件

时间:2016-10-04 11:03:46

标签: bash if-statement boolean-expression error-code

我是脚本新手,想了解如何根据布尔逻辑打印出变量。

None

如果两个部分都成功,我希望整个#!/bin/bash # set variables WEBAPPS_YES="Successfully synced webapps folder" WEBAPPS_NO="Could not sync webapps folder" RSYNC_YES="Successfully synced rsync log file" RSYNC_NO="Could not sync rsync log file" # Command to rsync 'webapps' folder and write to a log file rsync -azvh ~/webapps -e ssh user@something.com:/home/directories >> /path/to/rsync.log 2>&1 # Command to rsync 'rsync.log' to a log file on backup server 'Larry' rsync -azvh --delete ~/rsync.log -e ssh user@something.com:/path/to/logs if [ $? -eq 0 ] then echo exit 0 else echo >&2 exit 1 fi ifthen部分在else打印出来。我知道我需要某种逻辑陈述,但无法弄明白。

1 个答案:

答案 0 :(得分:1)

您可以在运行每个rsync命令后检查结果,然后显示结果。我认为这样可行:

# Command to rsync 'webapps' folder and write to a log file
rsync -azvh ~/webapps -e ssh user@something.com:/home/directories >> /path/to/rsync.log 2>&1
RESULT1="$?"

# Command to rsync 'rsync.log' to a log file on backup server 'Larry'
rsync -azvh --delete ~/rsync.log -e ssh user@something.com:/path/to/logs
RESULT2="$?"

if [ "$RESULT1" != "0" ]; then
    echo "$WEBAPPS_NO"
else
    echo "$WEBAPPS_YES"
fi

if [ "$RESULT2" != "0" ]; then
    echo "$RSYNC_NO"
else
    echo "$RSYNC_YES"
fi
相关问题