检查复制的文件是否存在以及它是否与源文件不同
Check if copied file exists and if it's different from source
我有这个脚本目前在我们的 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
您获取的 return 值错误。
您将变量设置为等于 cmp
的输出(标准输出)。 cmp
不显示任何内容,它使用 return 值作为答案。如果 cmp
是最后一个命令 运行,则此值将包含在 $?
中,或者您可以这样做:
if cmp -s "$file1" "$file2"
then
echo "The files match"
else
echo "The files are different"
fi
这是有效的,因为它询问 cmp
是否完成且没有错误。任何非零 return 值都被视为错误,并转到 else
语句。
我怀疑是架构问题。会不会是您的软件版本(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"
.
替换
您可以使用现有的解决方案,而不是编写自己的脚本,例如 rsync
。
我有这个脚本目前在我们的 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
您获取的 return 值错误。
您将变量设置为等于 cmp
的输出(标准输出)。 cmp
不显示任何内容,它使用 return 值作为答案。如果 cmp
是最后一个命令 运行,则此值将包含在 $?
中,或者您可以这样做:
if cmp -s "$file1" "$file2"
then
echo "The files match"
else
echo "The files are different"
fi
这是有效的,因为它询问 cmp
是否完成且没有错误。任何非零 return 值都被视为错误,并转到 else
语句。
我怀疑是架构问题。会不会是您的软件版本(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"
.
替换
您可以使用现有的解决方案,而不是编写自己的脚本,例如 rsync
。