如何将stdin上的输入发送到Makefile中定义的python脚本?

如何将stdin上的输入发送到Makefile中定义的python脚本?,python,linux,bash,variables,makefile,Python,Linux,Bash,Variables,Makefile,给出这个问题的答案,哪个有效 define NEWLINE endef define PYTHON_SCRIPT_CODE import sys print("hi") endef SDK_PATH := $(shell echo \ '$(subst $(NEWLINE),@NEWLINE@,${PYTHON_SCRIPT_CODE})' | \ sed 's/@NEWLINE@/\n/g' | python -) default: @echo 'SDK Pa

给出这个问题的答案,哪个有效

define NEWLINE


endef

define PYTHON_SCRIPT_CODE
import sys
print("hi")
endef

SDK_PATH := $(shell echo \
    '$(subst $(NEWLINE),@NEWLINE@,${PYTHON_SCRIPT_CODE})' | \
    sed 's/@NEWLINE@/\n/g' | python -)

default:
    @echo 'SDK Path Detected: $(SDK_PATH)'
如何通过管道将数据传输到python std中?例如:

define PYTHON_SCRIPT_CODE
import sys
print("hi")
print(sys.stdin.read())
endef

# pseudocode
SDK_PATH := $(shell bash --version | PYTHON_SCRIPT_CODE)
default:
    @echo 'SDK Path Detected: $(SDK_PATH)'
将输出:

SDK Path Detected: hi
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
结果:(无新株系)


相关问题:


  • 新示例,无需将内容管道化到Python脚本中:

    #!/usr/bin/make -f
    ECHOCMD:=/bin/echo -e
    SHELL := /bin/bash
    
    define NEWLINE
    
    
    endef
    
    VERSION := $(shell bash --version)
    
    # With this, you cannot use single quotes inside your python code
    define PYTHON_VERSION_CODE
    import re, sys;
    program_version = """${VERSION}"""
    match = re.search("Copyright[^\d]+(\d+)", program_version);
    
    if match:
        if int( match.group(1) ) >= 2018:
            sys.stdout.write("1")
        else:
            sys.stdout.write( match.group(1) )
    else:
        sys.stdout.write("0")
    endef
    
    # Due to this, you cannot use single quotes inside your python code
    PYTHON_SCRIPT_RESULTS := $(shell echo \
        '$(subst $(NEWLINE),@NEWLINE@,${PYTHON_VERSION_CODE})' | \
        sed 's/@NEWLINE@/\n/g' | python -)
    
    all:
        printf 'Results: %s\n' "${PYTHON_SCRIPT_RESULTS}"
    
    结果:


  • 对我来说,避免所有命令行引用问题的最简单方法是使用GNUmake的
    $(file)
    函数将代码写入文件。您甚至可以在
    定义
    d变量中使用
    #
    作为Python注释:

    VERSION := $(shell bash --version)
    
    # With this, you can use any form of quotes inside your python code
    define PYTHON_VERSION_CODE :=
    # -*- coding: iso-8859-15 -*-
    import re, sys;
    program_version = "${VERSION}"
    match = re.search("Copyright[^\d]+(\d+)", program_version);
    
    if match:
        if int( match.group(1) ) >= 2018:
            sys.stdout.write("1")
        else:
            sys.stdout.write( "match:" + match.group(1) )
    else:
        sys.stdout.write("0")
    endef
    
    
    $(file > test.py,$(PYTHON_VERSION_CODE))
    
    PYTHON_SCRIPT_RESULTS := $(shell python test.py)
    
    .PHONY: all
    all:
        @echo $(PYTHON_SCRIPT_RESULTS)
    

    这个解决方案可以局限于GNUmake吗?或者你需要一个“可移植”的版本吗?你能想到的任何解决方案都是非常受欢迎的。
    VERSION := $(shell bash --version)
    
    # With this, you can use any form of quotes inside your python code
    define PYTHON_VERSION_CODE :=
    # -*- coding: iso-8859-15 -*-
    import re, sys;
    program_version = "${VERSION}"
    match = re.search("Copyright[^\d]+(\d+)", program_version);
    
    if match:
        if int( match.group(1) ) >= 2018:
            sys.stdout.write("1")
        else:
            sys.stdout.write( "match:" + match.group(1) )
    else:
        sys.stdout.write("0")
    endef
    
    
    $(file > test.py,$(PYTHON_VERSION_CODE))
    
    PYTHON_SCRIPT_RESULTS := $(shell python test.py)
    
    .PHONY: all
    all:
        @echo $(PYTHON_SCRIPT_RESULTS)