Linux

如何用正則表達式找到文件夾的真實路徑

  • February 27, 2019

presto 是分佈式 SQL 查詢引擎,

當我們安裝 presto 時,它應該創建以下主文件夾

/application/DB/presto/presto-server-0.216

所以我們可以說

為了捕捉我們可以做的家庭 presto(它是我的 bash 腳本的一部分

PRESTO_HOME=` find  /application/DB/presto/ -maxdepth 1 | grep  presto-server `
echo $PRESTO_HOME
/application/DB/presto//presto-server-0.216

但是…因為某些使用者可以重命名**/application/DB/presto/**下的文件夾以安裝新版本

然後在**/application/DB/presto**下,我們可以得到以下文件夾:

/application/DB/presto/presto-server-HOLD_0.211
/application/DB/presto/_presto-server-0.212
/application/DB/presto/backup_presto-server-0.216.new
/application/DB/presto/presto-server-0.210.old
/application/DB/presto/presto-server-0.212.orig

所以關於那個

為了找到真正的路徑,正確的語法是什麼

/application/DB/presto/presto-server-0.216

筆記:

presto 文件夾結構始終與

presto-伺服器-

$$ any float number or integer number $$ 有效的例子:

/application/DB/presto/presto-server-0.736
/application/DB/presto/presto-server-0.936
/application/DB/presto/presto-server-1
/application/DB/presto/presto-server-1.9
/application/DB/presto/presto-server-12

無效的例子

/application/DB/presto/presto-server-0.736-43.previos
/application/DB/presto/presto-server-0.936_old
/application/DB/presto/orig_presto-server-1
/application/DB/presto/presto-server-1.9.bck
/application/DB/presto/presto-server-PREVIOS
/application/DB/presto/presto-server-
/application/DB/presto/presto-server-0.936_old
/application/DB/presto/orig_presto-server-1.other
/application/DB/presto/presto_server-1.9.bck
/application/DB/presto/OLD_presto-server-12-83

根據您使用的外殼,您可以使用萬用字元來完成。

重擊

#! /bin/bash -
shopt -s nullglob extglob
dirs=(/application/DB/presto/presto-server-+([[:digit:]])*(.+([[:digit:]])))

printf '%s\n' "Found ${#dirs[@]} matching directories:" "${dirs[@]}"

zsh

#! /bin/zsh -
set -o extendedglob
dirs=(/application/DB/presto/presto-server-<->(.<->)#(N))

printf '%s\n' "Found $#dirs matching directories:" $dirs

ksh93

#! /bin/ksh93
dirs=(~(N)/application/DB/presto/presto-server-+([[:digit:]])*(.+([[:digit:]])))

printf '%s\n' "Found ${#dirs[@]} matching directories:" "${dirs[@]}"

總之,presto-server-後面是 1 個或多個數字的序列,後跟 0 個或多個.<digits>.

引用自:https://unix.stackexchange.com/questions/503388