0

I wanted to write the commands as a conditional statement for if i.e., when the command executes without any error it should print success and failure if it fails. I'm even trying to prevent from printing the long console messages of the commands. Below is the code that I'm using, which is working perfectly without any conditional statements like printing success or failures messages.

$(BASENAME).pdf: $(BASENAME).ps
    ps2pdf $(BASENAME).ps $(BASENAME).pdf  

$(BASENAME).ps: $(BASENAME).dvi
    dvips -Ppdf $(BASENAME).dvi -o $(BASENAME).ps

I tried changing above as

$(BASENAME).pdf: $(BASENAME).ps
    @out=ps2pdf $(BASENAME).ps $(BASENAME).pdf > /dev/null 2>&1 && echo "    5. ps2pdf successful" || echo "    5. ps2pdf failed"

$(BASENAME).ps: $(BASENAME).dvi

    dvips -Ppdf $(BASENAME).dvi -o $(BASENAME).ps  > /dev/null 2>&1\
    if [ $? -eq 0 ];\
            then \
            echo "    4. dvips successful";\
    else \
            echo "    4. dvips failed";\
            exit 2;\
    fi

which is failing my execution and providing me error while running the code like below error

dvips -Ppdf basename_04.dvi -o basename_04.ps \
if [ basename_04.dvi -eq 0 ];\
        then \
        echo "    4. dvips successful";\
else \
        echo "    4. dvips failed";\
        exit 2;\
fi
/bin/sh: -c: line 2: syntax error near unexpected token `then'
/bin/sh: -c: line 2: `      then \'
make: *** [basename_04.ps] Error 1

Even after updating with semi-colon, I'm getting below error.

dvips -Ppdf nkukunur_04.dvi -o nkukunur_04.ps  > /dev/null 2>&1 ;\
      if [ nkukunur_04.dvi -eq 0 ]; \
              then \
              echo "    4. dvips successful"; \
      else \
              echo "    4. dvips failed"; \
              exit 2; \
      fi
/bin/sh: line 1: [: nkukunur_04.dvi: integer expression expected
    4. dvips failed

Please help me in fixing the issue.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

0

So, I don't know what the best practices are for using conditional directives vs using plain Bash conditionals like you are doing. But to fix your code, remove the backslash after the very first line:

dvips -Ppdf $(BASENAME).dvi -o $(BASENAME).ps <<<< removed backslash
if [ $? -eq 0 ];\
        then \
        echo "    4. dvips successful";\
else \
        echo "    4. dvips failed";\
        exit 2;\
fi

Otherwise all lines are executed as a single bash statement: the if up until the semicolon are passed as arguments to dvips and then begins a new statement, thus the unexpected token error.

jcarpenter2
  • 5,312
  • 4
  • 22
  • 49
0

You are missing a semicolon — and a double-dollar:

$(BASENAME).ps: $(BASENAME).dvi
    dvips -Ppdf $(BASENAME).dvi -o $(BASENAME).ps; \
    if [ $$? -eq 0 ]; \
            then \
            echo "    4. dvips successful"; \
    else \
            echo "    4. dvips failed"; \
            exit 2; \
    fi

The semicolon missed was the one at the end of the first command line (after $(BASENAME).ps and before the backslash). Assume that make replaces backslash-newline with a blank (not a newline). That's why you need the semicolon after the test; it's why you need one before the if too.

The double-dollar is needed because $? is a Make macro, but you want the shell to see and interpret $?. So, you hide it from Make with $$, which is a macro invocation that expands to $.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • even after fixing the error, I'm getting an error which I updated in the question. –  Mar 04 '18 at 00:30
  • After seeing the updated question, I've updated the answer. – Jonathan Leffler Mar 04 '18 at 00:32
  • if it gives an error it is printing dvips failed, which is working perfectly now. but exit 2; is not breaking/exiting from execution of next rules. Could you please help me in that aspect. –  Mar 04 '18 at 00:59
  • Taken on its own, that fragment of a makefile causes make to detect that the command exited with status 2 and it stops further processing (and exits with status 2). Therefore, it is not clear what you are doing to allow it to continue. You might use `make -k`; you might have a `.IGNORE:` target in the `makefile`; there might be something else that allows it to continue. – Jonathan Leffler Mar 04 '18 at 03:10