Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a string in make?

I need to take a parameter in my Makefile that consists of a host identifier in the form

host[:port]

where the colon and port are optional. So all of the following are valid:

foo.example.com
ssl.example.com:443
localhost:5000

etc.

I want to split the string on the optional colon and assign the values to variables, so that HOST contains foo.example.com, ssl.example.com, localhost, etc., and PORT contains 80 (the default port), 443, and 500 respectively.

like image 347
Joe Shaw Avatar asked Dec 16 '11 21:12

Joe Shaw


People also ask

How do you split a string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you split a string in a text file?

You can use String. split() method (in your case it's str. split("\\s+"); ).


1 Answers

# Retrieves a host part of the given string (without port).
# Param:
#   1. String to parse in form 'host[:port]'.
host = $(firstword $(subst :, ,$1))

# Returns a port (if any).
# If there is no port part in the string, returns the second argument
# (if specified).
# Param:
#   1. String to parse in form 'host[:port]'.
#   2. (optional) Fallback value.
port = $(or $(word 2,$(subst :, ,$1)),$(value 2))

Usage:

$(call host,foo.example.com) # foo.example.com
$(call port,foo.example.com,80) # 80

$(call host,ssl.example.com:443) # ssl.example.com
$(call port,ssl.example.com:443,80) # 443
like image 120
Eldar Abusalimov Avatar answered Oct 19 '22 01:10

Eldar Abusalimov