1
votes

He,
I want to read the contents of a file (which contains relative file paths) to a variable and prefix every line in the file with a path. Then copy all those files to a directory. Like this:

$(httpd_DIR)/my.tar: $(mypath)/html.txt
    rm -rf web
    mkdir -p web
    VV = $(addprefix $(httpd_DIR)/, $(shell cat $(mypath)/html.txt) )
    cp -R $$VV $(httpd_DIR)/web
    $(TAR) -C $(httpd_DIR) -cvf $(httpd_DIR)/web.tar web


The $(mypath)/html.txt file contains a list of relative file paths like this:
dir1/file1.html
dir2/file2.html
dir3/file3.html

For some reason I get the followin error:
/bin/bash: VV: command not found

I'm not trying to execute VV, so why is het giving me this error?
Note that if I uncomment the cp command, I still get the same error... I'm using GNU make on a linux PC.

1

1 Answers

2
votes

You have several problems here.

VV = $(addprefix $(httpd_DIR)/, $(shell cat $(mypath)/html.txt) )

You haven't told us what shell you're using, so I'll assume bash.

If you want to assign a variable in bash, you must beware whitespace: VV=foo is legal and will do what you expect, but if you type VV = foo, the shell will interpret the first word, "VV", as a command, and balk. If you type VV=foo bar, the shell will assign foo to VV, then balk at the command bar. You can use VV="foo bar" instead.

Then you run into another problem. Each command runs in its own subshell, so variables assigned in one command don't survive to the next:

VV=foo
echo $$VV # this will echo a blank

You must combine the commands, like this:

VV=foo ; echo $$VV # this will echo foo

or this:

VV=foo ; \
  echo $$VV # this will echo foo

(Note that there is only one TAB there, before the first line.)

In general, you should test these constructs with the simplest commands you can think of, before you plug in the real commands. That way it's much easier to catch these bugs.