我正在尋找BASH正則表達式來從下面的命令中拉出'db'粗體。然而,參數的順序並不能保證。出於某種原因,我無法完全工作。正則表達式來匹配行尾
我至今
regex="--db (.*)($| --)"
[[ [email protected] =~ $regex ]]
DB_NAMES="${BASH_REMATCH[1]}"
# These are example lines
somecommand --db myDB --conf /var/home # should get "myDB"
somecommand --db myDB anotherDB manymoreDB --conf /home # should get "myDB anotherDB manymoreDB"
somecommand --db myDB # should get "myDB"
somecommand --db myDB anotherDB # should get "myDB anotherDB"
對正則表達式的任何建議?
我不認爲bash支持非貪婪的匹配。你可以嘗試'([^ - ] *)'而不是'(。*)',或者使用awk或sed – Fabricator
爲什麼不使用'getopt'或'getopts'來解析命令行參數? – nanoix9