0

I am currently working on a bigger project where i want to test executable file with few different codes as input.
I call it like this ./test < code1 and after command echo $?, it shows last returned value [0, 1, 2, ..]

I wanted to automate is, so i created call in makefile like this :

#makefile 
[...]
test : 
    ./test < code1 
    @echo $$?
    ./test < code2 
    @echo $$?
    [...]

[...]

So i can call make test.

When program returns 0 as success, everything works fine. But when program has to return something else than 0, it shows me this :

./test < code3
Makefile:19: recipe for target 'test' failed
make: *** [test[ Error 2

Weird thing is, when i try to call program with code which made it crash in command line like :

./test < code3; echo $?  

It works perfectly and shows me last exit status ( for exapmle 3 ).
I am confused now, because i thought it should work the same. Can someone help me out?
Thank you!

SevO
  • 303
  • 2
  • 11
  • Possible duplicate of [How can I use Bash syntax in Makefile targets?](https://stackoverflow.com/questions/589276/how-can-i-use-bash-syntax-in-makefile-targets) – Nic3500 Nov 30 '17 at 15:22
  • Possible duplicate of [\`2>/dev/null\` does not work inside a Makefile](https://stackoverflow.com/questions/47527075/2-dev-null-does-not-work-inside-a-makefile) – Vroomfondel Nov 30 '17 at 16:54
  • If the test program and the target are both named `test`, you would typically get `make: "test" is up to date` when trying `make test`. i guess your real `Makefile` looks somewhat different. – tripleee Dec 01 '17 at 05:25

1 Answers1

1

See this answer: https://stackoverflow.com/a/41452754/939557

You need to put the echo into the same logical line as your test invocation:

test : 
        ./test < code1; echo $$?
        ./test < code2; echo $$?
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Well, i wanted to know, why one specific command works in command line and doesn't work in Makefile..should have asked differently – SevO Nov 30 '17 at 19:24
  • I explained it, in the comment that I mentioned. Each logical line in a makefile recipe is invoked in a separate shell. It's not like a shell prompt where each command that you type at the prompt is run _in the same shell_. – MadScientist Nov 30 '17 at 19:39
  • If you want to really emulate how make works, from a shell prompt, you need to run `/bin/sh -c 'somecommand'` not just `somecommand`. – MadScientist Nov 30 '17 at 19:41
  • Maybe explain in the answer itself that each recipe line is run in a separate shell. This is a FAQ but I wasn't quickly able to find a canonical duplicate. – tripleee Dec 01 '17 at 05:26
  • I don't know what you mean. The answer I linked to says _exacctly_ that. – MadScientist Dec 01 '17 at 06:18