2015-05-18 32 views
0

我有一個shell命令spurious ports返回以下數據...無法通過Makefile使用shell管道?

Service      Host       Port Browser link 
spurious-elasticache-docker 192.168.59.103    32794 - 
spurious-sqs     sqs.spurious.localhost  32789 http://sqs.spurious.localhost:32789 
spurious-s3     s3.spurious.localhost  32790 http://s3.spurious.localhost:32790 
spurious-elasticache   192.168.59.103    32793 - 
spurious-dynamo    dynamodb.spurious.localhost 32791 http://dynamodb.spurious.localhost:32791 
spurious-browser    browser.spurious.localhost 32795 http://browser.spurious.localhost:32795 
spurious-memcached   192.168.59.103    32792 - 

如果我想要得到的「發電機」的服務,然後下面的shell腳本的該端口號...

lines=`spurious ports`; echo $lines | grep "dynamo" | awk '{print $3}' 

這retuns 32791

但是,如果我嘗試從一個Makefile中運行這些,我反而得到整個spurious ports輸出(在一行上所有):

foobar: 
    $(eval port:=$(shell lines=`spurious ports`; echo $$lines | grep 'dynamo' | awk '{print $3}')) 
    @echo $(port) 

我也試着命令移動到一個shell腳本文件:

#!/bin/sh 
lines=`spurious ports`; echo $lines | grep "dynamo" | awk '{print $3}' 

而且Makefile裏使用:

PORT:=$(shell ./scripts/spurious-ports.sh) 
bing: 
    @echo $(PORT) 

但是,這也不行。

+0

此外,您忘記在awk命令中轉義'$';在makefile版本中,您需要使用'awk'{print $$ 3}''。但是,在配方中使用'eval'的事情非常奇怪。 – MadScientist

回答

0

這似乎是引號的問題。當你用多行變量時,你需要用雙引號將該變量保留在行尾。

例如,

VAR="1 
2 
3 
4" 
echo $VAR ## here it will print "1 2 3 4" 

but echo "$VAR" will print 

1 
2 
3 
4 

我認爲這會解決您的問題。有關更多詳細信息,請閱讀shell中的引號。

+0

謝謝!我會讀一讀關於它是如何工作的。 – Integralist