gpt4 book ai didi

bash - 如何比较目录名称并输入最高版本的目录

转载 作者:行者123 更新时间:2023-11-29 09:49:00 24 4
gpt4 key购买 nike

我在同一个子目录中有两个版本的 tcpdump。

tcpdump-4.1.1 和 tcpdump-4.3.0

如何编写一个返回最高版本的 bash 函数?

编辑:

我现在已经开始工作了。这是代码。

#!/bin/bash
# Function to get the latest version of the directory
function getLatestDirVer {
latestDIR=$(ls -v $1* | tail -n 1)
stringLen=`expr length "$latestDIR"`
stringLen=$(($stringLen-1))
latestDIR2=`expr substr $latestDIR 1 $stringLen`
echo $latestDIR2
}

# Main function
echo $(getLatestDirVer tcpdump)

这是输出

[luke@machine Desktop]$ ./latestDIRversion.sh 
tcpdump-4.3.0

tcpdump-4.1.1 和 tcpdump-4.3.0 目录位于桌面目录中。

最佳答案

这是使用 ls 的一种方法。您可以使用 -v 标志按文件名中的版本号从低到高排序:

ls -v tcpdump* | tail -n 1

编辑:

事实证明,我完全误读了你的问题。我以为您对文件名感兴趣,但实际上您对目录感兴趣。您可以将以下内容添加到您的 ~/.bashrc,我认为它对您有用:

getLatestDirVer () {
for i in $(find ./* -type d | sort --version-sort); do :;done
cd "$i"
}

关于bash - 如何比较目录名称并输入最高版本的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12857637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com