无法在makefile中调用bash函数

问题描述 投票:2回答:4

我有一个印象,我可以在GNU 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

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

makefile gnu-make
4个回答
4
投票

在BASH脚本中使用$*

functions.是

_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!

3
投票

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

all:
    bash -i -c lsc

例如,如果你的lsc中定义了.bashrc


1
投票

你用“export -f”导出了你的功能吗?

是bash你的Makefile的shell,还是sh?


0
投票

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

.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"
© www.soinside.com 2019 - 2024. All rights reserved.