2012-10-28 63 views
2

我有一个印象,我可以在GNU makefile中调用bash函数,但似乎是错误的。下面是一个简单的测试,我有这个函数:无法在makefile中调用bash函数

>type lsc 
lsc is a function 
lsc() 
{ 
    ls --color=auto --color=tty 
} 

这里是我的Makefile:

>cat Makefile 
all: 
    lsc 

这是我得到的运行make:

>make 
lsc 
make: lsc: Command not found 
make: *** [all] Error 127 

我的印象错误?或者是否有任何env设置问题?我可以在命令行运行“lsc”。

+0

另一条信息:当我尝试重现此,我将命令添加'型lsc'的规则,并给出正确的答案 - 但命令'lsc'仍然失败LS。 – Beta

回答

1

你用“export -f”导出了你的函数吗?

bash是你Makefile的shell,还是sh?

+0

是的,我试过“出口-f”,没有帮助。不确定你的第二个问题是什么意思,我的shell是bash,我运行make。 –

+0

您是否设置了SHELL变量?看到这里:http://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html#Choosing-the-Shell“如果这个变量没有在你的makefile中设置,程序/ bin/sh用作壳。“ – Sebastian

3

您不能在Makefile中调用bash函数或别名,只能调用二进制文件和脚本。但是你可以做什么,在呼唤一个交互式bash和指示它调用你的函数或别名:

all: 
    bash -i -c lsc 

如果lsc.bashrc定义,例如。

3

使用$*在bash脚本:

functions.sh

_my_function() { 
    echo $1 
} 

# Allows to call a function based on arguments passed to the script 
$* 

的Makefile

test: 
    ./functions.sh _my_function "hello!" 

运行例如:

$ make test 
./functions.sh _my_function "hello!" 
hello! 
0

您可以导入所有shell脚本函数从shell文件,如果使用这个从问题How do I write the 'cd' command in a makefile?

.ONESHELL: my_target 

my_target: dependency 
    . ./shell_script.sh 
    my_imported_shell_function "String Parameter" 

如果你愿意,你也可以甚至不使用.ONESHELL的事情,做这一切在一个线只需使用一个冒号;之后进口的shell脚本:

my_target: dependency 
    . ./shell_script.sh; my_imported_shell_function "String Parameter"