【问题标题】:run 2 rsync commands and print the output to a log file运行 2 个 rsync 命令并将输出打印到日志文件
【发布时间】:2017-02-12 12:30:36
【问题描述】:

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

#!/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

如果两个部分都成功与否,我希望将整个 ifthenelse 部分打印在 echo 中。我知道我需要某种逻辑语句,但无法弄清楚。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 这是个好主意!谢谢!是否可以根据上面的变量打印哪个失败了?所以我可以有 WEBAPPS_YES “但是” RSYNC_NO。那种东西?
    • 当然,RESULT1 和 RESULT2 将基本上保持“0”是命令成功,因此您可以检查并相应地显示结果。查看更新的答案。
    • 我会试一试,让你知道。感谢您的帮助
    • 请解释一下这个rsync -azvh ~/webapps -e ssh user@something.com:/home/directories >> /path/to/rsync.log 2>&1 RESULT1="$?" 命令。
    • @GeorgeUdosen,什么部分? rsync 标志记录在手册页中,其余的是标准 shell 语法。
    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多