Bash:Makefile中的for循环:文件意外结束 [英] Bash: for loop in Makefile: unexpected end of file

查看:83
本文介绍了Bash:Makefile中的for循环:文件意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Makefile,它将列出a.cpp,b.cpp和c.h文件包含的所有标头.但是,我得到了意外的EOF错误.相似的问题总是由行终止符引起的,例如对于EOL他们使用CRLF而不是LF.但是,我的文本编辑器设置为使用LF,我通过删除所有EOL并重新添加来重新检查它.不幸的是,错误仍然存​​在.这是代码:

I am writing a Makefile, which will list all headers included by a.cpp, b.cpp and c.h files. However, I got the error of unexpected EOF. Similar questions are always caused by the line terminator, like they used CRLF instead of LF for an EOL. However, my Text editor was set to using LF and I recheck this by delete all EOL and re-added. Unfortunately, the error still remains. Here are the codes:

#!/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

我收到此错误消息:

for file in "Bigram.cpp client.cpp Bigram.h"
/bin/sh: -c: line 1: syntax error: unexpected end of file"

在此先感谢您的帮助.

推荐答案

首先请注意,您必须转义要shell看到的 $ ,否则make会在调用shell之前将其展开.但是,您的主要问题是,make配方中的每个逻辑行都是单独的shell命令.因此,此规则:

First note you have to escape $ that you want the shell to see, otherwise make will expand them before calling the shell. However, your main problem is that every logical line in a make recipe is a separate shell command. So, this rule:

list-header:
        for file in a.cpp b.cpp b.h
        do
            echo "$file includes headers: "
            grep -E '^#include' $file | cut -f2
        done

将导致make调用shell命令:

will cause make to invoke the shell commands:

/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'

如果希望将所有字符都发送到相同的shell中,则需要使用反斜杠在换行符之间继续"逻辑行,并且必须添加分号来使该行生效,因为换行符不再用作命令定界符:

You need to use backslashes to "continue" a logical line across newlines if you want them all sent to the same shell, and you have to add semicolons to make that work since the newlines no longer serve as command delimiters:

list-header:
        for file in a.cpp b.cpp b.h; \
        do \
            echo "$$file includes headers: "; \
            grep -E '^#include' $$file | cut -f2; \
        done

这篇关于Bash:Makefile中的for循环:文件意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆