f



"for" inside "for"

dear community,

there is no need to mention that i'm far from beeing a sh-professional. 
in a male file i am trying to do two things to all files in defined 
directories.

< === snip =============================================== >
@for FILE in $(TEMPLATES); do \
	@for DIR_NAME in $(DIRS); do \
         	FILE_BASENAME=`basename $$FILE`; \
         	DIR_NAME=`dirname $$FILE`; \
		mkdir -p $(DIRNAME); \	cat $$FILE \
		| perl -pe "s%\@INSTALL_URL\@%$(INSTALL_URL)%g" \
                 > $(INSTALL_PATH)/$$FILE_BASENAME; \
	done;
done;
< === snip =============================================== >

the script comes back with:

< === snip =============================================== >
/bin/sh: -c: line 1: syntax error near unexpected token `do'
/bin/sh: -c: line 1: `for FILE in index news WedOct15 ; do  @for 
DIR_NAME in /srv/www/htdocs/mhf.at /srv/www/htdocs/mhf.at/contact
[...]
< === snip =============================================== >

could anyone give me a hint of where i am going wrong, please?
most cordial thanks in advance!

best of regards,
-- 
*---== michael h. fischer ==--------------------------------*
[ http://mhf.at ][ wap.mhf.at ][ sms@mhf.at ][ icq:61578912 ]
*-----------------------------------------------------------*
the box said: for winxp or better!      so i installed linux!

0
Michael
11/2/2003 2:37:04 PM
comp.unix.shell 15484 articles. 2 followers. Post Follow

4 Replies
647 Views

Similar Articles

[PageSpeed] 29

2003/11/02, 15:37(+01), Michael H. Fischer:
> dear community,
>
> there is no need to mention that i'm far from beeing a sh-professional. 
> in a male file i am trying to do two things to all files in defined 
> directories.
>
>< === snip =============================================== >
> @for FILE in $(TEMPLATES); do \
> 	@for DIR_NAME in $(DIRS); do \
>          	FILE_BASENAME=`basename $$FILE`; \
>          	DIR_NAME=`dirname $$FILE`; \
> 		mkdir -p $(DIRNAME); \	cat $$FILE \
> 		| perl -pe "s%\@INSTALL_URL\@%$(INSTALL_URL)%g" \
>                  > $(INSTALL_PATH)/$$FILE_BASENAME; \
> 	done;
> done;

@ is a make(1) thing, it prevents echoing of the command, and is
placed in front of the command, so putting one inside a command
will lead to a sh error.

\ at eol is also a make thing, it makes one logical line out of
2 physical Makefile lines. Make runs one command per logical
line, so your last "done" would be run as a separate command (sh
syntax error).

Moreover, you need a leading <Tab> at each line.

You use the DIR_NAME variable for two different purposes.
You're confusing a $(DIRNAME) make variable with the $DIR_NAME
shell variable. You create a directory $(DIRNAME) but don't use
it, that looks suspicious to me.

You're going to recreate each dirname for each template.


Maybe this is what you want:
 
target: dependencies
	@for DIR_NAME in $(DIRS); do \
	  mkdir -p "$$DIR_NAME"; \
	  for FILE in $(TEMPLATES); do \
	    FILE_BASENAME=`basename $$FILE`; \
	    perl -pe 's|\@INSTALL_URL\@|$(INSTALL_URL)|g' \
	      "$$FILE" > "$$DIR_NAME/$$FILE_BASENAME"; \
	  done; \
	done

(untested)

Beware that $(DIRS) $(TEMPLATES) $(INSTALL_URL) are expanded by
make before running sh. $$ is expanded to $ by make also.

-- 
St�phane                      ["Stephane.Chazelas" at "free.fr"]
0
Stephane
11/2/2003 4:31:23 PM
Michael H. Fischer wrote:

> dear community,
> 
> there is no need to mention that i'm far from beeing a sh-professional.
> in a male file i am trying to do two things to all files in defined
> directories.
> 
> < === snip =============================================== >
> @for FILE in $(TEMPLATES); do \
> @for DIR_NAME in $(DIRS); do \
>          FILE_BASENAME=`basename $$FILE`; \
>          DIR_NAME=`dirname $$FILE`; \
> mkdir -p $(DIRNAME); \        cat $$FILE \
> | perl -pe "s%\@INSTALL_URL\@%$(INSTALL_URL)%g" \
>                  > $(INSTALL_PATH)/$$FILE_BASENAME; \
> done;
> done;
> < === snip =============================================== >

Remove the "@" sign from the second "@for" (line 2). Add a backslash to the
end of the first "done; line.

"@" to make says "do not echo this line when you run it. All of the above
will be one logical line (because of the backslashes) so you do not want to
tell make anything special about the second for. The shell of course does
not understand a command "@for" as being special, so it is would look for
that as a command if it didn't get a syntax error because you have the
second "do" which is a reserved word....
0
Icarus
11/2/2003 4:35:07 PM
"Michael H. Fischer" <mhf@mhf.at> wrote in message news:<1067782501.375453@newsmaster-03.atnet.at>...
> dear community,
> 
> there is no need to mention that i'm far from beeing a sh-professional. 
> in a male file i am trying to do two things to all files in defined 
> directories.
> 
> < === snip =============================================== >
> @for FILE in $(TEMPLATES); do \
> 	@for DIR_NAME in $(DIRS); do \
>          	FILE_BASENAME=`basename $$FILE`; \
>          	DIR_NAME=`dirname $$FILE`; \
> 		mkdir -p $(DIRNAME); \	cat $$FILE \
> 		| perl -pe "s%\@INSTALL_URL\@%$(INSTALL_URL)%g" \
>                  > $(INSTALL_PATH)/$$FILE_BASENAME; \
> 	done;
> done;
> < === snip =============================================== >
> 
> the script comes back with:
> 
> < === snip =============================================== >
> /bin/sh: -c: line 1: syntax error near unexpected token `do'
> /bin/sh: -c: line 1: `for FILE in index news WedOct15 ; do  @for 
> DIR_NAME in /srv/www/htdocs/mhf.at /srv/www/htdocs/mhf.at/contact
> [...]
> < === snip =============================================== >
> 
> could anyone give me a hint of where i am going wrong, please?
> most cordial thanks in advance!
> 
> best of regards,

**

Try the following:
 < === snip =============================================== >
 for FILE in $(TEMPLATES)
 do 
   for DIR_NAME in $(DIRS)
   do 
     FILE_BASENAME=`basename $$FILE`; \
     DIR_NAME=`dirname $$FILE`; \
     mkdir -p $(DIRNAME); \	cat $$FILE \
     | perl -pe "s%\@INSTALL_URL\@%$(INSTALL_URL)%g" \
                  $(INSTALL_PATH)/$$FILE_BASENAME; \
   done
done
> < === snip =============================================== >

I have had trouble with for and do on the same line before.
Walt R.
0
wmreinemer
11/2/2003 9:32:02 PM
thank you very much for your answers!

the script doesn't work yet, but i did learn a lot and i
should think that i get it to work soon, thanks to your
hints.

most cordial thanks and
best of regards,
-- 
*---== michael h. fischer ==--------------------------------*
[ http://mhf.at ][ wap.mhf.at ][ sms@mhf.at ][ icq:61578912 ]
*-----------------------------------------------------------*
the box said: for winxp or better!      so i installed linux!

0
Michael
11/2/2003 10:52:08 PM
Reply: