代码之家  ›  专栏  ›  技术社区  ›  thclpr

检查复制的文件是否存在以及是否与源文件不同

  •  0
  • thclpr  · 技术社区  · 8 年前

    我有这个脚本,它目前在我们的x86_64服务器下工作,但是当我们尝试在PPC64服务器上运行时,它不工作:

    HOME=$(eval echo ~$(whoami))
    CACHE_DIR="$HOME/.jenkins/cache/archive"
    CACHE_PATH="$CACHE_DIR"
    
    if [ ! -d $CACHE_PATH ]
     then
       mkdir -p $CACHE_PATH
    fi
    
    if output=$(cmp --s requirements.txt $CACHE_PATH/requirements.txt)
    then
     echo "cached"
    else
     echo "build"
     cp requirements.txt $CACHE_PATH/requirements.txt
    fi
    

    从x86_64输出

    ubuntu@x86_agent:~$ bash tester.sh 
    build
    ubuntu@x86_agent:~$ bash tester.sh 
    cached
    

    从PPC64LE输出(目标文件夹始终为空)

    [xx@ppce64_agent /u/tpereira]$ bash testing.sh 
    cached
    [xx@ppce64_agent /u/tpereira]$ bash testing.sh 
    cached
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Socowi    8 年前

    我怀疑这是一个建筑问题。您是否有稍微不同的软件版本(bash、cmp等)以不同的方式处理脚本中的奇怪之处?

    怪事

    删除 HOME=$(eval echo ~$(whoami)) . HOME 是一个内置变量,应该由bash自动设置。将变量命名为小写,以避免此类名称冲突。 阿尔索 ~ 默认为当前用户的主目录。你可以只写 cacheDir=~/.jenkins/cache/archive

    有两个问题 if output=$(cmp --s requirements.txt $CACHE_PATH/requirements.txt) .

    • 这个 -s 选项应该只有一个破折号,而不是两个。
    • 当您指定不应该有任何输出时,为什么要存储命令的输出?( -S )是吗?

    简单地写 if cmp -s requirements.txt "$CACHE_PATH/requirements.txt" .

    更换

    您不必编写自己的脚本,而是可以使用现有的解决方案,用于Instace rsync .

        2
  •  0
  •   jeremysprofile Subin George    8 年前

    您获取的返回值错误。

    您将变量设置为 cmp (标准输出)。 凸轮轴位置 不显示任何内容,它使用返回值作为答案。此值将包含在 $? 如果 凸轮轴位置 最后一个命令运行了吗,或者您可以这样做:

    if cmp -s "$file1" "$file2"
    then
      echo "The files match"
    else
      echo "The files are different"
    fi
    

    这是因为它在问 凸轮轴位置 完成,没有错误。任何非零返回值都视为错误,并转到 else 声明。