2016-02-29 117 views
0

对于学校项目,我尝试使用makefile。首先,我创建Makefile删除文件

install: main.c 
    gcc -asve-temps main.c 
    @if test ! -d bin/; then mkdir bin; else : fi 
    mv a.out $(shell pwd)/bin/ 
    chmod 555 ./bin/a.out 

的文件现在我要清除的项目:

clear: 
@if test -d *.[osia]; then rm *.[osia] else : ; fi 
@if test -d a.out then rm a.out; else: ; fi 

执行make install精品工程。运行make clear产生错误代码:

/bin/sh: 1: test: main.i: unexpected operator 

并不会删除请求的文件。我想通过使用上面给出的模式运行make clear目标来删除所有*.o *.s *.i and *.a文件,以避免错误cannot remove ... : no such file or directory

回答

2

test期望单个参数;当你传递一个glob时,它会得到一堆。像find事情会在这种情况下工作:

find . -maxdepth 1 -name '*.[osia]' -delete 

或者说,为什么检查文件是否存在呢?

rm -f *.[osia] 

夫妇的其他注意事项:如果你没有一个else子句中的if语句,不包括它。阅读test命令;如果您正在查找文件,您肯定不想使用-d。而且,您可以使用变量$PWD代替运行子shell来获取它。