【问题标题】:Executing Shell command in Makefile rule在 Makefile 规则中执行 Shell 命令
【发布时间】:2016-07-23 09:28:33
【问题描述】:

我正在尝试从 Makefile 规则部分运行 shell 命令。我正在使用 basename 命令获取文件名,但无法通过 $$file 提供正确的文件名。任何人请帮助。

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

【问题讨论】:

标签: bash unix makefile


【解决方案1】:

虽然您可以通过这种方式到达那里,但几乎不需要 basename(1)。

  • 在 shell 中,${file##*/} 模式删除目录,${file%.*} 删除扩展名。作为内置程序,它们比调用可执行文件更快。

  • 在 GNU make 中,你有 $(notdir $(basename $(file)))。 BSD make 也有类似的。

我尽量避免 make 中的循环。为了完成你正在做的事情,我可能会做类似的事情

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

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

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

【讨论】:

    【解决方案2】:

    我可以通过一些小改动让它工作:

    all:
        for file in `ls *.txt`; \
           do \
           VIP=`basename $$file .txt` ;\
           echo "filename with ext. is $$file"; \
           echo "filename is $$VIP";\
        done   
    
    1. VIP= 行的变化
    2. 最后一个回显使用$$VIP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 2016-06-30
      • 2012-09-20
      相关资源
      最近更新 更多