从makefile逐行解释python程序

时间:2014-07-24 14:24:35

标签: python bash makefile

我需要逐行解释一个python程序。我在python中使用-c选项,并且像这样使用makefile。

all:   
python -c  
"print 'aa'  
   print 'bb'"

当我用make运行它时

python -c "print 'aa'
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [all] Error 2

当我拿出相同的python线并从bash运行时,它工作正常。可能是什么问题?

3 个答案:

答案 0 :(得分:1)

make规则的每一行都在不同的shell实例中执行。您需要转义换行符(使用\)或将其全部放在一行。

同样,给定的makefile片段应该给你一个关于-c的意外参数的错误。您的错误表明您的代码段实际上是:

all:   
python -c "print 'aa'  
   print 'bb'"

这不会改变任何事情。

答案 1 :(得分:1)

如果你的Makefile确实是

all:   
python -c  
"print 'aa'  
   print 'bb'"

我希望看到更多错误。使用该makefile,make将首先尝试运行python -c,这会产生如下错误:Argument expected for the -c option。然后它将中止,甚至不尝试运行shell命令"print 'aa'。你需要续行和分号。

all:   
        python -c   \
        "print 'aa';   \
        print 'bb'"

分号是必要的,因为make会剥离所有换行符并将字符串python -c "print 'aa'; print bb'"传递给shell(无论SHELL设置为什么)。

答案 2 :(得分:0)

看看这个问题。我认为你的问题是你的程序跨越多行,但你的makefile并没有这样解释它。添加斜杠应该清除它。

Multiline bash commands in makefile

相关问题