如何增加在shell脚本的版本号? [英] How to increment version number in a shell script?

查看:263
本文介绍了如何增加在shell脚本的版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的简单版本控制脚本是为了找到给定文件的最后版本号,增加它与新创建的文件(例如,编辑)运行给定命令,之后将其保存到稳定。由于它的简单,它不检查任何事情,因为需要脚本将被修改。例如,如果结果是不稳定的,用户可以省略最后一个参数

不过,目前的功能的一个主要问题是如何实现如下:如果一个点之后的最后一节有两个数字,INC直到99;只有1,如果,那么公司直到9,然后移动到previous部分。该版本可能有部分的任意正整数。

  1.2.3.44  - > 1.2.3.45
1.2.3.9 - > 1.2.4.0
1.2.3 - > 1.2.4
9 - > 10

剩下的问题是,它并不等待酒标签编辑器来关闭该文件;的目标是当标签被关闭,以检测。另外,你能解释一下如何最好地确保我的变量名称不覆盖现有的?

您也可以提供其他方面的改进。

 #!/斌/庆典
#Tested上的bash 4.1.5
#ALL参数依次是:使用文件夹,文件模式CMD【稳定名称]
文件夹=$ 1
FILE_PATTERN =$ 2
CMD =$ 3
稳定=$ 4CD$文件夹
last_version = $(LS --format =单柱--almost-全部| \\
    grep的$ FILE_PATTERN| \\
    SED -nrS / ^ [^ 0-9] *(([0-9] + \\)* [0-9] +)* / \\ 1 / P。| \\
    排序-Vu | \\
    尾-n 1)
last_version_file = $(LS --format =单柱--almost-全部| \\
    grep的$ FILE_PATTERN| \\
    grep的$ last_version | \\
    尾-n 1)#tail -n 1只需要拿到1行,如果存在具有相同版本号的备份文件
NEW_VERSION = $(回声$ last_version | \\
    呆子-F。 '{$ NF + = 1} {$打印} 0RT'OFS =。 ORS =)无限期#increments最后一节
new_version_file = $(回声$ last_version_file| \\
    SED -rS / $ last_version / $ NEW_VERSION /)
CP$ last_version_file$ new_version_file
$ CMD,$ new_version_file与& \\
    等到用gedit #works,但不可与酒标签编辑器
[$稳定]]&放大器;&安培; \\
    CP$ new_version_file$稳定#True如果字符串的长度为非零。

更新:
在我的电脑下面的作品中,我将更新它,如果改进或解决尚未解决的问题被发现:

 #!/斌/庆典
INC。()
{
禁用了javascript -s extglob
    NUM = $ {last_version //./}
    让NUM ++    重新= $ {last_version //./)(}
    重新= $ {//重新[0-9] /}')'
    重新= $ {RE#*)}    数= $ {// last_version [0-9] /}
    数= $(WC -c<<< $计数)
    OUT =''
    为((ⅰ=计数-1;我大于0;我 - ));做
        出='\\\\'$ I $出
    DONE    SED -r S / $重/ $出/<<< $ NUM
}文件夹=$ 1
FILE_PATTERN =$ 2
CMD =$ 3
稳定=$ 4CD$文件夹
last_version = $(LS --format =单柱--almost-全部| \\
    grep的$ FILE_PATTERN| \\
    SED -nrS / ^ [^ 0-9] *(([0-9] + \\)* [0-9] +)* / \\ 1 / P。| \\
    排序-Vu | \\
    尾-n 1)# - 几乎所有的没有列出暗示。和..
last_version_file = $(LS --format =单柱--almost-全部| \\
    grep的$ FILE_PATTERN| \\
    grep的$ last_version | \\
    尾-n 1)#tail -n 1只需要拿到1行,如果存在具有相同版本号的备份文件
NEW_VERSION = $(INC)
new_version_file = $(回声$ last_version_file| \\
    SED -rS / $ last_version / $ NEW_VERSION /)
CP$ last_version_file$ new_version_file
$ CMD,$ new_version_file与&&放大器; \\
    等到用gedit #works但不是酒标签编辑
[$稳定]]&放大器;&安培; \\
    CP$ new_version_file$稳定#True如果字符串的长度为非零。

我AP preciate品种已经提供了,因为他们与获得的透视和绘图的比较有助于解决方案。


解决方案

  $呼应1.2.3.4 | awk的-F。 -v OFS =。 NF == 1 {打印++ $ NF}; NF> 1 {如果(长度($ NF + 1)>长度($ NF))$(NF-1)++; $ NF = sprintf的(%0 * D,长度($ NF),($ NF + 1)%(10 ^长度($ NF)));打印}'
1.2.3.5


  1.2.3.9 => 1.2.4.0
1.2.3.44 => 1.2.3.45
1.2.3.99 => 1.2.4.00
1.2.3.999 => 1.2.4.000
1.2.9 = GT; 1.3.0
999 => 1000

更新:

 #!的/ usr / bin中/ GAWK -f开始{
    v [1] =1.2.3.4
    V [2] =1.2.3.44
    v [3] =1.2.3.99
    v [4] =1.2.3
    v [5] =9
    v [6] =9.9.9.9
    v [7] =99.99.99.99
    v [8] =99.0.99.99
    v [9] =    对(在vⅰ)
        的printf(#%D:%s =>%S \\ n,I,V [I],INC(V [I]))| 排序|列-t
}功能INC(S,A,LEN1,LEN2,LEN3,头,尾)
{
    拆分(S,A,)。    LEN1 =长度(a)
    如果(LEN1 == 0)
        返回-1
    否则如果(LEN1 == 1)
        返回小号+ 1    LEN2 =长度([LEN1])
    LEN3 =长度([LEN1] +1)    头=联接(一,1,len1-1)
    尾= sprintf的(%0 * D,LEN2,(一[LEN1] +1)%(10 ^ LEN2))    如果(LEN2 == LEN3)
        返回头。尾巴
    其他
        返回公司(头)。尾巴
}功能加入(一个,X,Y,S)
{
    对于(I = X; I< Y;我++)
        S =是个[我]。
    回个[Y]
}


  $搭配chmod + X inc.awk
$ ./inc.awk
#1:1.2.3.4 => 1.2.3.5
#2:1.2.3.44 => 1.2.3.45
#3:1.2.3.99 => 1.2.4.00
#4:1.2.3 => 1.2.4
#5:9 => 10
#6:9.9.9.9 => 10.0.0.0
#7:99.99.99.99 => 100.00.00.00
#8:99.0.99.99 => 99.1.00.00
#9:=> -1

The following simple version control script is meant to find the last version number of a given file, increment it, run a given command with the newly created file (e.g., editor), and after that save it to stable. Since it's simple, it doesn't check anything since the script would be modified as needed. For instance, if the result won't be stable, the user can omit the last argument.

However, one major concern of the current functionality is how to implement the following: if the last section after dot has two digits, inc untill 99; if only 1, then inc until 9, then move to the previous section. The versions may have any positive integer number of sections.

1.2.3.44 -> 1.2.3.45
1.2.3.9 -> 1.2.4.0
1.2.3 -> 1.2.4
9 -> 10

The remaining issue is that it doesn't wait for a tabbed wine editor to close the file; the goal is to detect when the tab is closed. Also, could you explain how best to make sure that my variable names don't overwrite existing ones?

You can also offer other improvements.

#!/bin/bash
#Tested on bash 4.1.5
#All arguments in order: "folder with file" "file pattern" cmd [stable name]
folder="$1"
file_pattern="$2"
cmd="$3"
stable="$4"

cd "$folder"
last_version=$(ls --format=single-column --almost-all | \
    grep "$file_pattern" | \
    sed -nr 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p' | \
    sort -Vu | \
    tail -n 1)
last_version_file=$(ls --format=single-column --almost-all | \
    grep "$file_pattern" | \
    grep $last_version | \
    tail -n 1) #tail -n 1 is only needed to get 1 line if there are backup files with the same version number
new_version=$(echo $last_version | \
    gawk -F"." '{$NF+=1}{print $0RT}' OFS="." ORS="") #increments last section indefinitely
new_version_file=$(echo "$last_version_file" | \
    sed -r "s/$last_version/$new_version/")
cp "$last_version_file" "$new_version_file"
"$cmd" "$new_version_file" & \
    wait #works with gedit but not with wine tabbed editor
[[ "$stable" ]] && \
    cp "$new_version_file" "$stable" #True if the length of string is non-zero.

Update: The following works on my pc, I will update it if improvements or solutions to unsolved problems are found:

#!/bin/bash
inc()
{
shopt -s extglob
    num=${last_version//./}
    let num++

    re=${last_version//./)(}
    re=${re//[0-9]/.}')'
    re=${re#*)}

    count=${last_version//[0-9]/}
    count=$(wc -c<<<$count)
    out=''
    for ((i=count-1;i>0;i--)) ; do
        out='.\\'$i$out
    done

    sed -r s/$re$/$out/ <<<$num
}

folder="$1"
file_pattern="$2"
cmd="$3"
stable="$4"

cd "$folder"
last_version=$(ls --format=single-column --almost-all | \
    grep "$file_pattern" | \
    sed -nr 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p' | \
    sort -Vu | \
    tail -n 1) #--almost-all do not list implied . and ..
last_version_file=$(ls --format=single-column --almost-all | \
    grep "$file_pattern" | \
    grep $last_version | \
    tail -n 1) #tail -n 1 is only needed to get 1 line if there are backup files with the same version number
new_version=$(inc)
new_version_file=$(echo "$last_version_file" | \
    sed -r "s/$last_version/$new_version/")
cp "$last_version_file" "$new_version_file"
"$cmd" "$new_version_file" && \
    wait #works with gedit but not tabbed wine editor
[[ "$stable" ]] && \
    cp "$new_version_file" "$stable" #True if the length of string is non-zero.

I appreciate the variety of solutions that have been offered, for they help with gaining a perspective and drawing a comparison.

解决方案

$ echo 1.2.3.4 | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}'
1.2.3.5


1.2.3.9  => 1.2.4.0
1.2.3.44 => 1.2.3.45
1.2.3.99 => 1.2.4.00
1.2.3.999=> 1.2.4.000
1.2.9    => 1.3.0
999      => 1000

UPDATE:

#!/usr/bin/gawk -f

BEGIN{
    v[1] = "1.2.3.4"
    v[2] = "1.2.3.44"
    v[3] = "1.2.3.99"
    v[4] = "1.2.3"
    v[5] = "9"
    v[6] = "9.9.9.9"
    v[7] = "99.99.99.99"
    v[8] = "99.0.99.99"
    v[9] = ""

    for(i in v)
        printf("#%d: %s => %s\n", i, v[i], inc(v[i])) | "sort | column -t"
}

function inc(s,    a, len1, len2, len3, head, tail)
{
    split(s, a, ".")

    len1 = length(a)
    if(len1==0)
        return -1
    else if(len1==1)
        return s+1

    len2 = length(a[len1])
    len3 = length(a[len1]+1)

    head = join(a, 1, len1-1)
    tail = sprintf("%0*d", len2, (a[len1]+1)%(10^len2))

    if(len2==len3)
        return head "." tail
    else
        return inc(head) "." tail
}

function join(a, x, y,    s)
{
    for(i=x; i<y; i++)
        s = s a[i] "."
    return s a[y]
}


$ chmod +x inc.awk
$ ./inc.awk
#1:  1.2.3.4      =>  1.2.3.5
#2:  1.2.3.44     =>  1.2.3.45
#3:  1.2.3.99     =>  1.2.4.00
#4:  1.2.3        =>  1.2.4
#5:  9            =>  10
#6:  9.9.9.9      =>  10.0.0.0
#7:  99.99.99.99  =>  100.00.00.00
#8:  99.0.99.99   =>  99.1.00.00
#9:  =>           -1

这篇关于如何增加在shell脚本的版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆