简体   繁体   English

使用shell脚本检查“ diff”命令的输出

[英]Checking the output of “diff” command using shell script

I am comparing a file on my linux machine with another file on a remote machine (both AWS Instances) using the following command: 我正在使用以下命令将linux计算机上的文件与远程计算机上的另一个文件(两个AWS实例)进行比较:

diff -s main <(ssh -i /home/ubuntu/sai_key.pem ubuntu@w.x.y.z 'cat /home/ubuntu/c1')

I want to write a shell script to do nothing when both files are same and update the file in the remote machine when the file in the linux machine (host) changes. 我想编写一个shell脚本,当两个文件相同时什么也不做,并且当linux机器(主机)中的文件更改时更新远程机器中的文件。

I want the shell script to check the remote file every 30 seconds. 我希望shell脚本每30秒检查一次远程文件。

I am running the shell script on my host machine only and not the remote one. 我只在主机上运行shell脚本,而不在远程主机上运行。

Could you please help me with that? 你能帮我吗?

First of all, I would recommend using cmp rather than diff (I believe it is more efficient), but this solution should work either way. 首先,我建议使用cmp而不是diff (我相信它会更有效),但是该解决方案应该以任何一种方式起作用。

All you need to do is write a bash script with an if statement in it. 您需要做的就是编写一个带有if语句的bash脚本。 If the cmp or diff command returns nothing, you don't need to take any action. 如果cmpdiff命令什么也不返回,则无需执行任何操作。 In the other case, you just need to scp your current main file over to the remote host. 在其他情况下,你只需要scp您目前main在文件到远程主机。

If you decide to use cmp , that if statement just needs to look like: 如果决定使用cmp ,则if语句仅需要如下所示:

if cmp -s main <(ssh -i /home/ubuntu/sai_key.pem ubuntu@w.x.y.z 'cat /home/ubuntu/c1')
then
    echo "Match!"
else
    echo "No match!"
    scp ...
fi

If you really are dead set on using diff , comment below and I can write something up really quick that does the equivalent. 如果您真的diff使用diff ,请在下面评论,我可以很快地写出等效的东西。

Checking the remote file (running this bash script) every 30 seconds may be slightly overkill, but it's really up to you. 每隔30秒检查一次远程文件(运行此bash脚本)可能会有些过头,但这实际上取决于您。 To achieve a periodic check (this only works for time intervals above 1 minute), you could use a cron scheduler. 要实现定期检查(这仅适用于1分钟以上的时间间隔),可以使用cron计划程序。 I recommend using Crontab Guru to create cron schedules and understand how they work. 我建议使用Crontab Guru创建cron计划并了解它们如何工作。 For your purpose, you would just need to add one line to your crontab (in your terminal run crontab -e to edit your crontab) as follows: 出于您的目的,您只需要向crontab中添加一行(在终端中运行crontab -e来编辑crontab),如下所示:

* * * * * /absolute/path/to/shell/script.sh

Make sure you chmod the script with the right permissions as well! 确保您还以正确的权限对脚本进行chmod

There is no need for bash, diff, cmd, cron, ... Python can do everything with a little help from ssh: 不需要bash,diff,cmd,cron等。Python可以在ssh的帮助下完成所有工作:

import subprocess
import time

key_pair = 'AWS_Linux_key_pair.pem'
remote_name = 'ec2-user@ec2-3-16-214-98.us-east-2.compute.amazonaws.com'
file_name = 'fibonacci.py'
cat_string = "cat " + file_name

while True:
    command = 'ssh -i ' + key_pair + ' ' + remote_name + " '" + cat_string + "'"
    remote_lines = subprocess.getoutput(command)
    local_lines = subprocess.getoutput(cat_string)

    if remote_lines != local_lines:
        print("update")
        command = 'scp -i ' + key_pair + ' ' + file_name + ' ' + remote_name + ':'
        subprocess.getoutput(command)
    else:
        print("the same")
    time.sleep(30)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM