0

I have a shell command spurious ports which returns the following data...

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  -

If I want to get the port number for the "dynamo" service then the following shell script works...

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

This retuns 32791

But if I try and run this from inside a Makefile, I instead get the entire spurious ports output (all on one line?):

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

I've also tried moving the commands into a shell script file:

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

And inside the Makefile used:

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

But this doesn't work either.

Integralist
  • 5,899
  • 5
  • 25
  • 42
  • 1
    In addition you forgot to escape the `$` in the awk command; in the makefile version you need to use `awk '{print $$3}'`. But, the whole thing of using `eval` inside a recipe is very odd. – MadScientist May 18 '15 at 12:52

1 Answers1

0

This seems quotes issue. When you variable with multiple lines you need to put that variable in double quotes to retain the /n at end of line.

for E.g.

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

 but echo "$VAR" will print 

 1
 2
 3
 4

I Think this will solve your problem. For more details read about quotes in shell.

Amit Kumar
  • 313
  • 1
  • 4
  • 14