我在同一个子目录中有两个版本的 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 目录位于 Desktop 目录中。
最佳答案
这是使用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/