我在一個大而重要的 SH 檔案中有以下變數,我需要從變數中洗掉一些資料并只保留部分文本。
我得到帶有指向內部 git repo 的鏈接的“repoTest”,我需要變數“nameAppTest”來僅接收最后一個“/”之后的常量資料。
例子:
我得到: repoTest="ssh://[email protected]/code/ecsb/name-repo.git"
我嘗試進行拆分: nameAppTest=$(echo "$repoTest"|cut -d'/' -f5|sed -e 's/.git//g')
我得到的回應: echo "$nameAppTest"(ecsb)。
我期望收到的: name-repo
我試過這樣但失敗了:nameAppTest=$(echo "$repoTest"|cut -d'/' -f5|sed -e 's/.git//g')
uj5u.com熱心網友回復:
這是一個絕妙的技巧:
nameAppTest=$(basename "$repoTest" .git)
用于basename僅獲取 URL 的最后一個組成部分,并在一個步驟中全部洗掉擴展名。
您也可以使用sh引數擴展分兩步完成,無需任何外部程式:
# Remove everything up to and including the last /
temp="${repoTest##*/}"
# Remove the trailing .git
nameAppTest="${temp%.git}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/479246.html
