Vivek Patel
Vivek Patel

Reputation: 53

Executing Shell command in Makefile rule

I am trying to run a shell command from Makefile rule section. I'm using basename command to get file name couldn't provide correct file name through $$file . anyone please, help.

for file in `ls *.fof`; \
   do \
   $(eval VIP = `basename $$file .fof`) \
   echo "filename with ext. is $$file"; \
   echo "filename is $(VIP)";\
done               

Upvotes: 1

Views: 3326

Answers (2)

James K. Lowden
James K. Lowden

Reputation: 7837

While you can get there that way, basename(1) is hardly ever needed.

  • In the shell, the pattern ${file##*/} removes the directory, and ${file%.*} removes the extension. As built-ins, they're faster than invoking an executable.

  • In GNU make, you have $(notdir $(basename $(file))). BSD make has similar.

I try to avoid loops in make. To accomplish what you're doing, I might do something like

.SUFFIXES = .fof .T
NAMES=$(wildcard *.fof)

all: $(subst .fof,.T,$(NAMES))

.fof.T: 
        echo "filename with ext. is $^"
        echo "filename is $(notdir $(basename($@)))"

Upvotes: 2

Lars Fischer
Lars Fischer

Reputation: 10129

I could make it work with small changes:

all:
    for file in `ls *.txt`; \
       do \
       VIP=`basename $$file .txt` ;\
       echo "filename with ext. is $$file"; \
       echo "filename is $$VIP";\
    done   
  1. changes in the VIP= line
  2. the last echo uses $$VIP

Upvotes: 0

Related Questions