我正在编写一个Makefile,它将列出a.cpp,b.cpp和ch文件包含的所有头文件.但是,我得到了意外EOF的错误.类似的问题总是由行终止符引起,就像他们在EOL中使用CRLF而不是LF.但是,我的文本编辑器设置为使用LF,我通过删除所有EOL重新检查并重新添加.不幸的是,错误仍然存在.以下是代码:
#!/bin/bash
list-header:
for file in a.cpp b.cpp b.h
do
echo "$file includes headers: "
grep -E '^#include' $file | cut -f2
done
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
for file in "Bigram.cpp client.cpp Bigram.h"
/bin/sh: -c: line 1: syntax error: unexpected end of file"
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
Mad*_*ist 17
首先要注意你必须要转义$你希望shell看到,否则make会在调用shell之前展开它们.但是,您的主要问题是make配方中的每个逻辑行都是一个单独的shell命令.所以,这条规则:
list-header:
for file in a.cpp b.cpp b.h
do
echo "$file includes headers: "
grep -E '^#include' $file | cut -f2
done
Run Code Online (Sandbox Code Playgroud)
将导致make调用shell命令:
/bin/sh -c 'for file in a.cpp b.cpp b.h'
/bin/sh -c 'do'
/bin/sh -c 'echo "ile includes headers: "'
/bin/sh -c 'grep -E '^#include' ile | cut -f2'
/bin/sh -c 'done'
Run Code Online (Sandbox Code Playgroud)
如果要将它们全部发送到同一个shell,则需要使用反斜杠"继续"跨换行的逻辑行,并且必须添加分号才能使其工作,因为换行符不再用作命令分隔符:
list-header:
for file in a.cpp b.cpp b.h; \
do \
echo "$$file includes headers: "; \
grep -E '^#include' $$file | cut -f2; \
done
Run Code Online (Sandbox Code Playgroud)