目前正在尋找更好的方法來做到這一點......
例如,我有一個檔案,其中包含一個帶有分支名稱或版本號的字串。我想知道檔案是否在ref模式后不使用版本號。
示例檔案:
source = "git::https://URL?ref=main"
source = "git::https://URL?ref=0.1.0"
目前我正在這樣做:
# Search for string after pattern and turn it into an array.
version=($(grep -oP '(?<=ref=).*' file.txt | tr -d '".'))
# If array does not contain number pattern, catch it.
if [[ $version =~ ^[0-9] (\.[0-9] ){2,3}$ ]]
then
echo "All module sources contain versions, will continue"
else
echo "Some module sources DO NOT contain versions, you must use a version number in your module source"
fi
如何單獨使用 grep 或什至必須將字串匯出到陣列來做到這一點?
uj5u.com熱心網友回復:
if grep -E 'ref=[0-9].[0-9]{2,3}$'; then
echo has version
else
echo has no version
uj5u.com熱心網友回復:
我結束了
if [[ $(grep -oPr '(?<=ref=).*' | tr -d '"') =~ ^[0-9] (\.[0-9] ){2,3}$ ]]
then
echo "All module sources contain versions, will continue"
else
echo "Some module sources DO NOT contain versions, you must use a version number in your module source"
fi
uj5u.com熱心網友回復:
使用 GNUgrep和 PCRE 正則運算式,您可以使用
#!/bin/bash
rx='ref=(?![0-9] (\.[0-9] ){2,3}")[^"]*"'
if grep -Pq "$rx" file.txt; then
echo "Some module sources DO NOT contain versions, you must use a version number in your module source"
else
echo "All module sources contain versions, will continue"
fi
請參閱在線演示。正則運算式的意思
ref=- 一段ref=文字(?![0-9] (\.[0-9] ){2,3}")- 如果有一個或多個數字后跟兩個或三個點和一個或多個數字,則匹配失敗的負前瞻[^"]*"- 零個或多個字符",然后是一個"字符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/475842.html
上一篇:如何正確使用xargs與cp
下一篇:Bash如何決議多標志命令?
