Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如果两个文件不同,则执行Shellscript操作_Bash_Shell_Scp - Fatal编程技术网

Bash 如果两个文件不同,则执行Shellscript操作

Bash 如果两个文件不同,则执行Shellscript操作,bash,shell,scp,Bash,Shell,Scp,我正在使用以下命令将文件导入服务器: scp zumodo@shold:/test/test/test/server.py /test/test/test/test.py~/; 如果新导入的文件test.py~与已存在的test.py不同,我想重新启动服务器。如何使用shell脚本执行此操作?您可以区分这两个文件。返回码为零(0)表示没有差异。返回代码为1表示文件不同 if ! cmp test.py test.py~ >/dev/null 2>&1 then # re

我正在使用以下命令将文件导入服务器:

scp zumodo@shold:/test/test/test/server.py /test/test/test/test.py~/;
如果新导入的文件test.py~与已存在的test.py不同,我想重新启动服务器。如何使用shell脚本执行此操作?

您可以区分这两个文件。返回码为零(0)表示没有差异。返回代码为1表示文件不同

if ! cmp test.py test.py~ >/dev/null 2>&1
then
  # restart service
fi
分解如下:

  • cmp test.py test.py~
    如果test.py和test.py~相同,则返回true(0),否则返回false(1)。您可以在
    man cmp
    中看到这一点
  • 反转该结果,因此
    if
    语句转换为“if test.py和test.py~不同”
  • 重定向
    /dev/null 2>&1
    cmp
    的所有输出发送到,因此您只需获得真/假比较结果,而不会在控制台上产生任何不必要的噪音
    • 我会做类似的事情

      zumodo@shold$ cat /test/test/test/server.py | ssh zumodo@otherhost 'cat - > /test/test/test/test.py.new ; cmp /test/test/test/test.py /test/test/test/test.py.new || (mv /test/test/test/test.py.new /test/test/test/test.py ; echo issue restart command here)'
      

      我希望您的意思是“重新启动我的守护程序”而不是“重新启动我的服务器”。您可能不想要最后的“/;”在该命令上,因为它不是一个目录,并且您在同一行上没有其他命令。@peter您的意思是如果一个文件的加载与一个文件不同,请检查每个文件?在你对我下面答案的评论中,这正是我想要做的。同样,我是一个新手,你认为你能给我提供一个例子或一个例子的链接吗?@peter请看我的答案,例如/link。当
      cmp
      为此而构建时,为什么要使用diff?+1,但-q是非标准的。重定向到/dev/null将始终有效。对不起,有人能解释一下bash noob的第一行吗?if运算符的参数的程序是什么?为什么会有这两个重定向?它们是什么意思?您也可以使用
      cmp-s
      来抑制比较的终端输出。@PolyBug:是的,但这是Unix/Linux中的惯例。原因是只有一种类型的成功,但也可能有多种类型的失败,它们会返回不同的状态代码。例如,请参见
      man ls
      man grep
      的退出状态部分。即使文件不存在,这也将是正确的。