如何檢索變量,即給定nginx.conf虛擬主機中server_name
屬性中的第一個域?Bash從nginx.conf解析server_name
我的虛擬主機存儲爲從nginx.conf主文件中分離出來的文件,雖然我可以grep並顯示每個文件,但我期望做的是隻抓取它找到的第一個域並回顯它(最終,我「要是把它變成一個變量,這樣我可以在其他地方使用它)
所以......如果我grep "server_name" /each/vhost/conf
我呈現:
[email protected]:/home# grep "server_name" /home/site-configs/getyou
server_name getyou.onl;
server_name www.getyou.onl getme.onl www.getme.onl;
[email protected]:/home# grep "server_name" /home/site-configs/kevinpirnie
server_name kevinpirnie.com www.kevinpirnie.com;
[email protected]:/home# grep "server_name" /home/site-configs/airsweepinc
server_name airsweepinc.com www.airsweepinc.com;
[email protected]:/home# grep "server_name" /home/site-configs/themedepot
server_name theme-depot.net www.theme-depot.net;
什麼我找上面給出的例子是:
[email protected]:/home# grep "server_name" /home/site-configs/getyou
getyou.onl
[email protected]:/home# grep "server_name" /home/site-configs/kevinpirnie
kevinpirnie.com
[email protected]:/home# grep "server_name" /home/site-configs/airsweepinc
airsweepinc.com
[email protected]:/home# grep "server_name" /home/site-configs/themedepot
theme-depot.net
完美。這工作 – Kevin
希望你注意到'-P是高度實驗性'警告在GNU grep手冊頁,所以實際上並沒有依賴它的任何重要或便攜式... –
@EdMorton,是否已知有(有)很多/嚴重的錯誤? (很難搜索'-P')我可以從十多年前找到對grep的'-P'的引用,並且很難相信PCRE庫會被設計爲一個簡單的正則表達式太長。 – ilkkachu