1

I have a makefile in which I want to do checkout on a file if this file isn't already checkedout:

VAR=$(shell cleartool ls $(HOME)/all_files.tgz | grep CHECKEDOUT)
build:
     @if ["$(VAR)" == ""]; then \
        cleartool co -unres -nc $(HOME)/all_files.tgz;\
     fi
     @ tar czf $(HOME)/all_files.tgz $(OUT)/*.log

I get the following error if all_files.tgz is checked out:

/bin/sh: [/home/ge/prj/all_files.tgz@@/main/10/CHECKEDOUT from /main/10             Rule: CHECKEDOUT: not found
georgiana_e
  • 1,809
  • 10
  • 35
  • 54

1 Answers1

2

When you use the brackets in the shell you MUST include whitespace around them. This is because [ is actually a separate command: if you use ["$(VAR)" that expands to the string you quote above ([/home/ge/prj/all_files.tgz@@/main/10/CHECKEDOUT from /main/10) and that is not a valid command name. Similarly for the final ]: must have whitespace around it.

Use:

VAR=$(shell cleartool ls $(HOME)/all_files.tgz | grep CHECKEDOUT)
build:
        @if [ "$(VAR)" == "" ]; then \
            cleartool co -unres -nc $(HOME)/all_files.tgz;\
        fi
        @ tar czf $(HOME)/all_files.tgz $(OUT)/*.log

This is a kind of odd rule though. Since VAR is just a shell function, and you're using it in the shell, why even bother to use $(shell ...)?

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • +1. I have been writing too much bash script, and not enough makefile lately. – VonC Oct 28 '13 at 16:14
  • Would `ifeq ($(strip $(VAR)),) xxx endif` work too in a Makefile to test if the var is empty? – VonC Oct 28 '13 at 16:15
  • Yes, that will work too, if it's OK that the check run up-front, at the time the makefile is parsed. The example provided by georgiana defers the check so it doesn't happen until much later, when the `build` target recipe is going to be run. That could be an important difference, or not (we can't tell since we don't have complete information). – MadScientist Oct 28 '13 at 17:03
  • Thanks for your help (worked with a single '=': @if [ "$(VAR)" = "" ];). The variable VAR is defered when the build targetis called. – georgiana_e Oct 29 '13 at 10:41
  • Right, missed that... `==` is an extension supported by some shells, like `bash`, but not all. The POSIX standard operator for equality in the shell test program is `=`. – MadScientist Oct 29 '13 at 12:08