我目前正在寫一個外殼腳本,其讀取Vagrantfile和引導程序它(簡而言之;))殼牌腳本 - 從文件提取AWK塊分割成陣列
但我擊中下面片的壁的代碼:
TEST=()
while read result; do
TEST+=(`echo ${result}`)
done <<< `awk '/config.vm.define[ \s]\"[a-z]*\"[ \s]do[ \s]\|[a-zA-Z_]*\|/, /end/ { print }' Vagrantfile`
echo "${TEST[1]}"
當我通過Vagrantfile與兩臺機器此AWK圖案在正則表達式定義它(config.vm.define)被發現。
輸出
config.vm.define "web" do |web|
web.vm.box = "CentOs"
web.vm.box_url = "http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box"
web.vm.hostname = 'dev.local'
web.vm.network :forwarded_port, guest: 90, host: 9090
web.vm.network :private_network, ip: "22.22.22.11"
web.vm.provision :puppet do |puppet|
puppet.manifests_path = "puppet/manifests"
puppet.manifest_file = "web.pp"
puppet.module_path = "puppet/modules"
puppet.options = ["--verbose", "--hiera_config /vagrant/hiera.yaml", "--parser future"]
end
config.vm.define "db" do |db_mysql|
db_mysql.vm.box = "CentOs"
db_mysql.vm.box_url = "http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box"
db_mysql.vm.hostname = 'db.mysql.local'
db_mysql.vm.network :private_network, ip: "22.22.22.22"
db_mysql.vm.network :forwarded_port, guest: 3306, host: 3306
db_mysql.vm.provision :puppet do |puppet|
puppet.manifests_path = "puppet/manifests"
puppet.manifest_file = "db.pp"
puppet.module_path = "puppet/modules"
puppet.options = ["--verbose", "--hiera_config /vagrant/hiera.yaml", "--parser future"]
end
但我似乎無法將它們傳遞到一個數組很好。我想要的是TEST數組包含兩個帶有機器config.vm.define塊的索引作爲它們的對應值。
E.g.
TEST[0] = 'config.vm.define "web" do |web|
.... [REST OF THE BLOCK CONTENT] ...
end'
TEST[1] = 'config.vm.define "db" do |db_mysql|
.... [REST OF THE BLOCK CONTENT] ...
end'
輸出echo "${TEST[1]}"
是nothing
。如上圖所示,echo "${TEST[0]}"
返回整個塊。
我玩IFS/RS/FS,但我似乎無法得到我想要的輸出。
awk命令本身的確切輸出是什麼?在你的例子中,echo「$ {TEST [1]}」的結果是什麼? – grebneke
每個'config.vm.define'塊中是否有換行符? –
@grebneke更新了輸出問題 是的,有新行 – cocheese