【问题标题】:Pass arguments in shell-script from a makefile and get back the results?从makefile传递shell脚本中的参数并取回结果?
【发布时间】:2012-11-05 09:17:59
【问题描述】:

我有一个makefile,我想从中调用一个shell脚本,它会做一些事情并将结果返回给makefile。

详细说明:-

从我的 make-file 中,我调用 shell 脚本如下:-

source = $(PWD)
target = $(ROOT)
SCRIPT:= /home/........./temp.sh
FUNCTION:=$(shell $(SCRIPT $source $target))`

Shell 脚本“temp.sh”:-

source=$1
target=$2

echo There are $# arguments to $0: $*
common_part=$source # for now
result="" # for now

while [[ "${target#$common_part}" == "${target}" ]]; do
    common_part="$(dirname $common_part)"

    if [[ -z $result ]]; then
        result=".."
    else
        result="../$result"
    fi
done

if [[ $common_part == "/" ]]; then
    result="$result/"
fi

forward_part="${target#$common_part}"

if [[ -n $result ]] && [[ -n $forward_part ]]; then
    result="$result$forward_part"
elif [[ -n $forward_part ]]; then
    result="${forward_part:1}"
fi

echo "Result=$result"
  • 我没有看到 Shell-Script 的“echo statements”,可能是什么原因?
  • 如何将结果返回到 makefile?
  • 这是从 make-file 调用脚本的正确方法吗?

我是这方面的新手。

【问题讨论】:

    标签: shell makefile gnu-make


    【解决方案1】:

    您的调用语法错误;你想要的

    FUNCTION:=$(shell $(SCRIPT) $(source) $(target))
    

    内插的 Makefile 变量需要在其名称周围加上括号,除非它们是单字母。

    【讨论】:

    • 感谢您的回答。当我运行上面的命令时,我收到以下错误: temp.sh:找不到命令 temp.sh 存在于我的当前目录中。我也尝试将 scipt 的值设置为 ./temp.sh 但仍然遇到相同的错误。我还缺少什么吗?请纠正我的错误。
    • 无法从这种情况下判断。也许发布一个包含更具体细节的单独问题。
    • 在 makefile 中,每个脚本都在 subshel​​l 下运行。如您所见,在运行脚本之前使用了“shell”。所以我建议对你的工作目录执行“cd”,然后运行脚本。例如。 $(shell cd $(WORKING_DIR); ./$(SCRIPT_name) $(source) $(target))
    • 如果你想在执行脚本之前知道你在哪个目录然后执行 pwd;在“cd”命令之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2017-01-22
    • 2016-03-20
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多