简体   繁体   中英

How to replace a character in a variable in tcsh script while using inside a makefile function

I have a makefile with the below content:

mytarget:
    @$(call my_func, echo \"hello world\")

define my_func
    $(eval var := $(1))

    if [[ "$(var)" != "" ]]; then \
        $(var); \
    fi;
endef 

while executing with make command, this prints "hello world" (enclosed with the double quotes) All I want is to get rid of the " from the echo "hello world" before the conditional if check.

I tried with below statement but of no use:

var2 = $(echo $var | tr '"' '' then use $(var2) in the if condition instead of $(var) It is not working. Any help will be very much appreciated.

Thanks

First, you should never add @ to your makefile recipes until they are working 100%. Otherwise you're trying to debug your makefiles blindfolded.

Your entire makefile script seems like it could just be written as:

mytarget:
        $(call my_func, echo "hello world")

my_func = $1

and it would work exactly the same way: if my_func is called with an empty argument then $1 expands to the empty string, which is what you're trying to test anyway. Presumably you have some reason for wanting a more complicated configuration but, as I mentioned in my comment, it seems like an XY problem.

I'll give you a short answer: make doesn't care about quoting or about backslashes. Any quotes or backslashes you use are passed verbatim to the shell. So if you pass echo \"foo\" to the shell, it will print the quotes. That's exactly as expected.

If you remove the @ , as I mention above, things will be a lot more clear to you.

So, why are you adding backslashes? It appears you're adding them because otherwise the condition if [ "$1" != "" ] will expand to if [ "echo "hello world"" != "" ] which is a syntax error.

One simple solution is to use single quotes instead of double quotes:

mytarget:
        $(call my_func, echo "hello world")

define my_func
    if [ '$1' != '' ]; then \
        $1; \
    fi;
endef 

Of course, this will fail if you call my_func with a value containing singe-quotes.

You have two solutions:

First is to use a make conditional, not a shell conditional, since make doesn't care about quotes etc. That gives you:

mytarget:
        $(call my_func, echo "hello world")

my_func = $(if $1,$1)

but that's the same as my first suggestion, just using $1 by itself.

If you really, really have to do this in the shell then you have to quote things for the shell. Make's subst function can do that for you:

mytarget:
        $(call my_func, echo "hello world")

define my_func
    if [ '$(subst ','\'',$1)' != '' ]; then \
        $1; \
    fi;
endef

which replaces all instances of ' with '\'' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM