从makefile中的shell执行结果中删除子串

时间:2016-11-25 14:47:15

标签: shell awk sed makefile

在我的Makefile中,我试图提取虚拟盒桥接口IP,我设法让它存储命令的输出并回显它,但它包含不需要的字符:"值:"我只想要IP,我试过在VBoxManageCommand之后添加一个awk管道,但似乎无法让它工作。有什么想法吗?

$(eval VB_IP := "$(shell VBoxManage guestproperty get machinename "/VirtualBox/GuestInfo/Net/2/V4/IP")")

@echo $(VB_IP) ### This gives: Value: 10.224.199.19

docker-machine ssh oasis "sed '/--label provider=virtualbox/a --insecure_registry $(VB_IP):5000' /var/lib/boot2docker/profile"  ###This makes use of the extracted ip

3 个答案:

答案 0 :(得分:1)

在命令中添加管道应该有效:

test:
    $(eval VB_IP := $(shell echo Value: 10.224.199.19 | cut -f2 -d:))
    echo $(VB_IP)

或者你可以使用$(subst FROM,TO,TEXT)而不是炮轰:

    $(eval VB_IP := $(subst Value:,,$(shell echo Value: 10.224.199.19 )))

答案 1 :(得分:1)

在找到适合您的解决方案之前,我们可能需要尝试一些实验。请注意,内部和外部规则的工作方式不同。试试这个,不要在任何规则内,并告诉我们结果:

VB_IP := $(word 2, $(shell VBoxManage guestproperty get machinename "/VirtualBox/GuestInfo/Net/2/V4/IP"))
$(info the first result is $(VB_IP))

您也可以在规则中尝试此操作,并告诉我们结果:

somerule:
    VBoxManage guestproperty get machinename "/VirtualBox/GuestInfo/Net/2/V4/IP" | cut -f2 -d:

答案 2 :(得分:0)

感谢您的帮助,这最终为我们工作了:

# Update the docker-machine daemon with the insecure_registry from the brigded interface on virtualbox
$(eval VB_IP_ := "$(shell VBoxManage guestproperty get machinename "/VirtualBox/GuestInfo/Net/2/V4/IP")")
$(eval VB_IP := $(shell echo $(VB_IP_) | sed 's/[^ ]* //' ))

@echo Adding private docker repo: registry: $(VB_IP)
docker-machine ssh oasis "sed -i '' '/--label provider=virtualbox/a --insecure_registry $(VB_IP):5000' /var/lib/boot2docker/profile"