awk 和 bash 脚本

发布于 2024-08-12 21:09:04 字数 183 浏览 3 评论 0原文

我有一个 tgz 文件,其中包含版本文件 version.txt。该文件只有一行“version=1.0.0”。此 tgz 文件存在于两个不同的目录中并且具有相同的名称。

我的要求是我需要使用 bash 脚本和 awk 来确定这两个 tgz 文件中哪一个是最新版本。即具有1.0.0 的tgz 文件的版本比具有版本1.0.1 的文件低。

I have a tgz file which contains a version file, version.txt. This file has only one line "version=1.0.0". This tgz file is present in two different directories and has the same name.

My requirement is that I need to use bash script and awk to determine which of these two tgz files is of the latest version. i.e. tgz file having 1.0.0 is of a lower version than the file having version 1.0.1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杆小烟枪 2024-08-19 21:09:04

使用 tar -xzvf /mnt/cf/bundle.tgz -O version.txt 提取版本。

棘手的部分是如何处理版本。如果您的版本总是三位数,那么我建议将每个部分乘以 1000。这将为您提供一个可以轻松比较的大数字。即1.0.0==1000000000; 1.0.1 = 1000000001。另一个选项是使用 %04d 格式化每个部分:0001.0000.0000 和 0001.0000.0001。可以比较这些字符串。

Use tar -xzvf /mnt/cf/bundle.tgz -O version.txt to extract the version.

The tricky part is how to handle the version. If your version has always three digits, then I suggest to multiply each part with 1000. That will give you a large number which you can easily compare. I.e. 1.0.0 == 1000000000; 1.0.1 = 1000000001. Another option is to format each part with %04d: 0001.0000.0000 and 0001.0000.0001. These strings can be compared.

忆梦 2024-08-19 21:09:04

对于否决和负面评论表示歉意,但我总是对家庭作业类型的问题持怀疑态度。为了弥补,我已经替你做了。你不需要 awk,这一切都可以在 bash 中完成。

只需将 F1 和 F2 设置为正确的文件名即可。我将把它作为练习,让您接受它们作为命令行参数:)

#!/usr/bin/env bash
F1=f1.tgz
F2=f2.tgz
VERSION_FILE=version.txt
version1=`tar -xzf $F1 -O $VERSION_FILE|sed -e 's/.*version=//'`
version2=`tar -xzf $F2 -O $VERSION_FILE|sed -e 's/.*version=//'`

if [ "$version1" == "$version2" ]; then
    echo "$F1 and $F2 contain the same version: $version1, $version2"
    exit 0;
fi

(   # start a subshell because we're changing IFS

    # Assume F1 is the latest file unless we find otherwise below
    latest_file=$F1
    latest_version=$version1

    # set the Internal Field Separator to a dot so we can
    # split the version strings into arrays
    IFS=.

    # make arrays of the version strings
    v1parts=( $version1 )
    v2parts=( $version2 )

    # loop over $v1parts, comparing to $v2parts
    # NOTE: this assumes the version strings have the same
    # number of components. If wont work for 1.1 and 1.1.1,
    # You'd have to have 1.1.0 and 1.1.1
    # You could always add extra '0' components to the shorter array, but
    # life's too short...
    for ((i = 0 ; i < ${#v1parts[@]} ; i++ )); do
        # NOTE: ${#v1parts[@]} is the length of the v1parts array
        if [ "${v1parts[i]}" -lt "${v2parts[i]}" ]; then
            latest_file=$F2
            latest_version=$version2
            break;
        fi
    done
    echo "$latest_file is newer, version $latest_version"
)

Apologies for the downvote and the negative comment, but I'm always suspicious of homework type questions. To make amends, I've done it for you. You don't need awk, it can all be done in bash.

Just set F1 and F2 to the correct filenames. I'll leave it as an exercise for you to accept them as command line arguments :)

#!/usr/bin/env bash
F1=f1.tgz
F2=f2.tgz
VERSION_FILE=version.txt
version1=`tar -xzf $F1 -O $VERSION_FILE|sed -e 's/.*version=//'`
version2=`tar -xzf $F2 -O $VERSION_FILE|sed -e 's/.*version=//'`

if [ "$version1" == "$version2" ]; then
    echo "$F1 and $F2 contain the same version: $version1, $version2"
    exit 0;
fi

(   # start a subshell because we're changing IFS

    # Assume F1 is the latest file unless we find otherwise below
    latest_file=$F1
    latest_version=$version1

    # set the Internal Field Separator to a dot so we can
    # split the version strings into arrays
    IFS=.

    # make arrays of the version strings
    v1parts=( $version1 )
    v2parts=( $version2 )

    # loop over $v1parts, comparing to $v2parts
    # NOTE: this assumes the version strings have the same
    # number of components. If wont work for 1.1 and 1.1.1,
    # You'd have to have 1.1.0 and 1.1.1
    # You could always add extra '0' components to the shorter array, but
    # life's too short...
    for ((i = 0 ; i < ${#v1parts[@]} ; i++ )); do
        # NOTE: ${#v1parts[@]} is the length of the v1parts array
        if [ "${v1parts[i]}" -lt "${v2parts[i]}" ]; then
            latest_file=$F2
            latest_version=$version2
            break;
        fi
    done
    echo "$latest_file is newer, version $latest_version"
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文