我在同一子目錄中有兩個版本的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)
這裏的ouptut
[[email protected] Desktop]$ ./latestDIRversion.sh
tcpdump-4.3.0
了tcpdump-4.1.1和tcpdump的-4.3.0目錄是在桌面目錄。
這是有點工作,但它說'tcpdump-4.3.0:'。 這裏是我的代碼, #/斌/慶典 #功能,以獲取最新版本的目錄 功能getLatestDirVer { \t latestDIR = $(!(LS -v $ 1 * |尾-n 1)) \t stringLen ='EXPR長度$ latestDIR' \t $ stringLen = $($ stringLen - 2) \t $ latestDIR ='EXPR SUBSTR $ latestDIR 1 $ stringLen' \t回聲$ latestDIR } #主功能 出來= $ (getLatestDirVer tcpdump) echo $ out 回聲「完成」 但它的意思是 ./latestDIRversion.sh:第6行:ls -v tcpdump * | tail -n 1:表達式中的語法錯誤(錯誤標記爲「tcpdump * | tail -n 1」) – lukegjpotter
在編輯視圖中,該註釋看起來更好。 – lukegjpotter
@lukegjpotter:請參閱我對答案所做的修改。每次鍵入'getLatestDirVer'時,都應該導航到最新的目錄。希望這會對你有用。 – Steve