1

I'm making a makefile and I'm working on a target with no dependencies that checks to see if a file named README exists in the current directory and if it does, read it using less or else exit quitely: give no errors whatsoever--the command to run the target should print nothing to console if this is the case.

I've tried a couple different ways but nothing really seems to work (PAGER is simply a var equal to less):

read :
   ifneq ("$(wildcard README)","")
      -@$(PAGER) README
   endif

And also with this code

read :
   -@for file in *;\
   do \
       if [ ${file} == "README" ]; then\
           $(PAGER) "README" ;\
       fi;\
   done

With the first chunk I keep getting an error akin to /bin/sh: Syntax error: word unexpected (expecting ")") and for the life of me I just don't get what it's saying. I certainly don't think there's a syntax error, perhaps I'm misusing make.

For the latter code I get an unexpected operator error for the ==.

I've also tried simpler things like these two single liner solutions but get similar errors:

-@test -a README && less README
-@[ -a README ] && less README

Any help would be greatly appreciated.

EDIT: I was digging further and saw something promising and explicitly setting my shell to /bin/bash (SHELL := /bin/bash) yet no dice.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Wuzseen
  • 687
  • 3
  • 14
  • 20
  • @Luboš Turek before removing tags, you should check their wiki about how it is used. It currently mentions the usage of `less` as a UNIX command. See http://stackoverflow.com/tags/less/info – fedorqui May 03 '17 at 08:17

2 Answers2

1

You should try to extract your shell code in separate *.sh files so that you can specify them as commands in your makefile (and make sure that the *.sh files are executable).

npclaudiu
  • 2,401
  • 1
  • 18
  • 19
  • that's a great idea. I'll give it a shot. – Wuzseen Apr 20 '12 at 06:23
  • Beta provided me with an answer to resolve it within the makefile, but this is pretty creative. It may be against the rules for the assignment, however. – Wuzseen Apr 20 '12 at 06:34
1
read:
        @if [ -e README ]; then less README; fi
Beta
  • 96,650
  • 16
  • 149
  • 150
  • This solves it. I'm sort of curious as to why what I did was throwing issues. It seems functionally identical to me, but the == operator is apparently not digested similarly. – Wuzseen Apr 20 '12 at 06:29
  • `[` is just the command `test`. And `test` uses the `=` operator for equality comparison, not `==`. – Michael Wild Apr 20 '12 at 06:37
  • @Wuzseen, your first solution works for me without error, but says there's nothing to be done for the target when README does not exist (there's a way to fix that). Your second solution works if you double the '$' (`$` => `$$`) because otherwise Make interprets `${` as nothing and it all falls apart. Your third and fourth don't work, and shouldn't. – Beta Apr 20 '12 at 06:48
  • @Wuzseen: Recipes are executed by your shell (probably /bin/sh) and it's the shell that's throwing the syntax error. – reinierpost Apr 20 '12 at 14:11