1

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               
RishbhSharma
  • 259
  • 3
  • 10
Vivek Patel
  • 53
  • 1
  • 9
  • More generally perhaps see also https://stackoverflow.com/questions/10024279/how-to-use-shell-commands-in-makefile – tripleee Nov 16 '18 at 08:40

2 Answers2

2

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($@)))"
James K. Lowden
  • 7,574
  • 1
  • 16
  • 31
0

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
Lars Fischer
  • 9,135
  • 3
  • 26
  • 35