Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using awk to extract a specific text in a Makefile

Tags:

makefile

awk

I have a config file with this format

foo=bar
fie=boo
..
..

and there is a Makefile, I want to extract a line of config file that have a string 'disk_size' then extract value that is assigned to the variable

this is the line I've used in Makefile

fallocate -l $(shell awk -F= '/disk_size/ { print $2 }' $(conf)) $@ 

but I receive this error, (the whole line was extracted.)

fallocate -l disk_size=268435456 disk.img
fallocate: invalid length value specified

the awk command work in terminal but it doesn't work in Makefile, why?

tnx

like image 988
Arash Avatar asked Nov 29 '25 12:11

Arash


2 Answers

You probably just need to escape the $:

fallocate -l $(shell awk -F= '/disk_size/ { print $$2 }' $(conf)) $@ 

Make is trying to use the variable $2 rather than passing the string $2 to awk.

like image 119
William Pursell Avatar answered Dec 02 '25 04:12

William Pursell


If your config file is really this format:

foo = bar
disk_size = 1234

You can directly include it in the Makefile:

# Include configuration file
include $(conf)

target:
    fallocate -l $(disk_size)

You can also use the - operator to ignore error of the include command and assign default value if there is no config file.

# Include configuration file
-include $(conf)

# Set default size
disk_size ?= 5678

target:
    fallocate -l $(disk_size)
like image 26
jmlemetayer Avatar answered Dec 02 '25 04:12

jmlemetayer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!