-1

I am having an issue with my Makefile:

  • At the begining of the Makefile, a file is created.
  • I wanna retrieve the content of that created file by using the "cat command".
  • I need to use the $(shell cat) execution because I have to retrive it inside another command.

It's like if the sub-shell does not reallize of the creation of that file and does not find it. If you take a look into the output message order... its like first of all, it is executing the $(shell cat) command, because the first line is saying: "cat: test.txt: The file or directory does not exist".

Why it seems to be executing the commands unordered... executing first the $(shell cat)...

It could be shown in the following silly example:

Take a look into the following Makefile with a test routine:

test:
    rm -rf test.txt
    echo "Hello World" > test.txt
    echo "$(shell cat test.txt)"

Executing make test the Output is as follows:

cat: test.txt: The file or directory does not exist.
rm -rf test.txt
echo "Hello World" > test.txt
echo ""

If you execute "make test" twice, you can realize that the second execution of the Makefile is echoing the text "Hello World"... because it is executing $(shell cat test.txt) at the begining and the file exists from the frist "make test" execution...

Any suggestion about what is happening and how I have to proceed to accomplish my goal?

Many thanks in advance!

user2607702
  • 41
  • 1
  • 6
  • ...yes, that's what happens when you tell `make` to run the subprocess instead of having the shell it's invoking do it. Is there a reason you *want* to use `shell cat` here, instead of deferring the subshell to be run by the subprocess, not by make? – Charles Duffy Apr 13 '20 at 17:05
  • The reason why I am using the ```shell cat``` is because I have a command execution that needs to take the value of the file. For instance: ```curl -X POST -u ${userPass} -H ${headers} ${url} -d '{ "key": "'"$(shell cat pubkey.txt) ...}``` – user2607702 Apr 13 '20 at 17:09
  • I mean. I dont know how to perform a ```cat``` within another command. The only way of achieving it that I found on the internet, is by executin it with a ```shell cat``` execution. – user2607702 Apr 13 '20 at 17:11
  • Ahh. It's probably better not to use `cat` at all if you're generating JSON; better to use something like `jq`, that knows how to modify your file's contents to be correctly encoded even if it has characters that need to be escaped to be valid. `curl ... -d "$(jq --rawfile pubkey pubkey.txt '{"key": $pubkey}')"`, f/e, but for use in a makefile, the `$`s need to be doubled up. – Charles Duffy Apr 13 '20 at 17:21

1 Answers1

0

Double up your $s to escape them, so they're still seen by the shell in literal form (and thus executed by the shell, not by make prior to the shell's invocation):

test:
    rm -rf test.txt
    echo "Hello World" > test.txt
    echo "$$(cat test.txt)"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441