简体   繁体   English

awk和bash脚本

[英]awk and bash script

I have a tgz file which contains a version file, version.txt. 我有一个tgz文件,其中包含一个版本文件version.txt。 This file has only one line "version=1.0.0". 该文件只有一行“版本= 1.0.0”。 This tgz file is present in two different directories and has the same name. 此tgz文件存在于两个不同的目录中,并且具有相同的名称。

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. 我的要求是,我需要使用bash脚本和awk来确定这两个tgz文件中的哪个是最新版本。 ie tgz file having 1.0.0 is of a lower version than the file having version 1.0.1. 即,具有1.0.0版本的tgz文件的版本低于具有1.0.1版本的文件的版本。

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

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. 如果您的版本始终有三位数,那么我建议将每个部分乘以1000。这将为您提供一个很大的数字,您可以轻松地进行比较。 Ie 1.0.0 == 1000000000; 即1.0.0 == 1000000000; 1.0.1 = 1000000001. Another option is to format each part with %04d: 0001.0000.0000 and 0001.0000.0001. 1.0.1 =1000000001。另一种选择是用%04d格式化每个部分:0001.0000.0000和0001.0000.0001。 These strings can be compared. 这些字符串可以进行比较。

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. 您不需要awk,都可以用bash完成。

Just set F1 and F2 to the correct filenames. 只需将F1和F2设置为正确的文件名即可。 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"
)

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

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