以下简单的版本控制脚本用于查找给定文件的最新版本号,递增它,使用新创建的文件(例如编辑器)运行给定的命令,然后将其保存到稳定。因为它很简单,所以它不检查任何东西,因为脚本将根据需要进行修改。例如,如果结果不稳定,用户可以省略最后一个参数。
然而,当前功能的一个主要问题是如何实现以下内容:如果点后面的最后一节有两位数字,则Inc.直到99;如果只有1,则Inc.直到9,然后移动到前一节。版本可以具有任意正整数个节数。
1.2.3.44 -> 1.2.3.45
1.2.3.9 -> 1.2.4.0
1.2.3 -> 1.2.4
9 -> 10
剩下的问题是,它不会等待选项卡式wine编辑器关闭文件;其目标是检测选项卡何时关闭。另外,您能解释一下如何最好地确保我的变量名不会覆盖现有的变量名吗?
您还可以提供其他改进。
#!/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.
更新:以下内容适用于我的pc,如果发现未解决问题的改进或解决方案,我会更新它:
#!/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.
我很欣赏已经提供的各种解决方案,因为它们有助于获得视角和进行比较。
https://stackoverflow.com/questions/8653126
复制相似问题