0

I was using this question: Assign a makefile variable value to a bash command result? to try and solve my problem, but it doesn't seem to work.

I have a one-liner to get the current port of a connected Arduino, that I need in my Makefile as a parameter:

[bf@localhost GameCode]$ arduino-cli board list | grep arduino:megaavr:nona4809 | awk '{ print $1; }'
/dev/ttyACM0

As you see this works fine. But now I want to use that in my Makefile:

BOARD=arduino:megaavr:nona4809
PORT=$(shell arduino-cli board list | grep $$BOARD | awk '{ print $$1; }')
OUTPUT=mycode.hex

program: $(OUTPUT)
     eeprom-program -f $(OUTPUT) -p $(PORT)

But, $(PORT) seems to be completely empty. I know I can fix this with a shell script, but as this is only a single line, I don't want to clutter my directory with small scripts.

How can I get that command in my Makefile?

5
  • $$1 is the makefile variable $1, which doesn't exist, just as $$BOARD is the makefile variable $BOARD. Commented Apr 25, 2022 at 15:43
  • @Barmar I forgot the shell in front, and $$1 should then be an escaped $. After adding shell it still doesn't seem to work though. Commented Apr 25, 2022 at 15:44
  • @Barmar, okay. Question still stands, how could I escape that one then? Commented Apr 25, 2022 at 15:45
  • grep $$BBOARD should be grep $(BBOARD). Makefile variables need $() around them. You're escaping the $, so it's trying to grep a shell variable named BBOARD. Commented Apr 25, 2022 at 15:49
  • @Barmar yep, got it now. All that escaping and not-escaping confused a little. Commented Apr 25, 2022 at 15:50

1 Answer 1

1

Here ...

BOARD=arduino:megaavr:nona4809
PORT=$(shell arduino-cli board list | grep $$BOARD | awk '{ print $$1; }')

... you correctly escape $1 as $$1 to pass the former through to the shell. But you do not want to pass $BOARD through to the shell as such. Rather, you want make to expand that one:

BOARD=arduino:megaavr:nona4809
PORT=$(shell arduino-cli board list | grep $(BOARD) | awk '{ print $$1; }')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.