Makefile: read input variable and set several variables

齐天大圣

I have a makefile where I want to read file name from input and then make other names based on it`s name. I have tried the following code

mlext = .ml
testext = test.ml
nativeext = test.native

test: 
    @read -p "Enter file name: " file; \
    echo $$file$(mlext); \
    echo $$file$(testext); \
    echo $$file$(nativeext)

for example:

If i type: foo

then I want to get foo.ml, footest.ml, footest.native

however, I can only get foo.ml. For the rest two i only get .ml and .native

How can i fix this?

Virgile

First, let us see what is the exact recipe given to the shell by removing the @ in your Makefile:

read -p "Enter file name: " file; \
echo $file.ml; \
echo $filetest.ml; \
echo $filetest.native;

The issue is thus that the content of $(testext) gets appended to $$file, creating the shell variable $filetest, which (very probably) does not exist, resulting in an empty string in the end. This does not occur with $(mlext), as the initial dot cannot be part of a variable name.

To overcome this, use $${file} instead of $$file in your Makefile rule.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Makefile set variable in target

From Dev

How to set environment variables in makefile?

From Dev

Makefile -- error out if variable not set

From Dev

Check if environmental variable is set in makefile

From Dev

Using "read" to set variables

From Dev

How to set env variables in Makefile to run tests?

From Dev

How to set multiple variables in target makefile

From Dev

Makefile - Set multiple variables on a single stage

From Dev

Makefile: set the environment variables output by a command

From Dev

Makefile for loop - variables never get set

From Dev

How to "unpack" a vector variable into several variables?

From Dev

Calculate proportion of several binary variables by another variable

From Dev

makefile read reused variable inside recipe

From Dev

How to set input files with a Makefile? (C)

From Dev

Optimal way to set and read variables

From Java

How to set child process' environment variable in Makefile

From Dev

Is it possible to set environment variable through a makefile?

From Dev

How to conditionally set Makefile variable to something if it is empty?

From Dev

Rule to set a variable in Makefile not working as expected

From Dev

How to conditionally set Makefile variable to something if it is empty?

From Dev

Variable input read from stdin

From Dev

Set Input Value to Javascript Variable

From Dev

Disable input fields if a variable is not set

From Dev

Set variable to input value (jQuery)

From Dev

Makefile variables using include directive and targe specific variable

From Dev

Create several dummy variables from one string variable

From Dev

R - How to get the order of several variables in another variable

From Dev

How to define a variable that takes spaces of several other variables?

From Dev

How to define a variable that takes spaces of several other variables?

Related Related

  1. 1

    Makefile set variable in target

  2. 2

    How to set environment variables in makefile?

  3. 3

    Makefile -- error out if variable not set

  4. 4

    Check if environmental variable is set in makefile

  5. 5

    Using "read" to set variables

  6. 6

    How to set env variables in Makefile to run tests?

  7. 7

    How to set multiple variables in target makefile

  8. 8

    Makefile - Set multiple variables on a single stage

  9. 9

    Makefile: set the environment variables output by a command

  10. 10

    Makefile for loop - variables never get set

  11. 11

    How to "unpack" a vector variable into several variables?

  12. 12

    Calculate proportion of several binary variables by another variable

  13. 13

    makefile read reused variable inside recipe

  14. 14

    How to set input files with a Makefile? (C)

  15. 15

    Optimal way to set and read variables

  16. 16

    How to set child process' environment variable in Makefile

  17. 17

    Is it possible to set environment variable through a makefile?

  18. 18

    How to conditionally set Makefile variable to something if it is empty?

  19. 19

    Rule to set a variable in Makefile not working as expected

  20. 20

    How to conditionally set Makefile variable to something if it is empty?

  21. 21

    Variable input read from stdin

  22. 22

    Set Input Value to Javascript Variable

  23. 23

    Disable input fields if a variable is not set

  24. 24

    Set variable to input value (jQuery)

  25. 25

    Makefile variables using include directive and targe specific variable

  26. 26

    Create several dummy variables from one string variable

  27. 27

    R - How to get the order of several variables in another variable

  28. 28

    How to define a variable that takes spaces of several other variables?

  29. 29

    How to define a variable that takes spaces of several other variables?

HotTag

Archive