Sunday, 11 September 2016

bash - /bin/sh: 1: Bad substitution Makefile





I have written a script to find all running docker containers with a certain name and it works when I directly type it into my terminal but as soon as I put it a Makefile it throws an error




/bin/sh: 1: Bad substitution




This is the script in makefile:



remote: FORCE
docker ps -q --filter name=$$(tmp=$${PWD##*/} && printf "%s_workspace" "$${tmp//./}")



To clarify what that chunk after name= is doing, it's trying to get the current folder name and remove all .'s and append it to my container name which is workspace.


Answer



The substitution operator you are using isn't supported by /bin/sh. You need to tell make to use bash instead:



SHELL := /bin/bash


If you want to keep your recipe POSIX-compatible, use tr instead:




remote: FORCE
docker ps -q --filter name=$$(printf '%s_workspace' "$${PWD##*/}" | tr -d .)


If you are using GNU make, you might want to use



remote: FORCE
docker ps -q --filter name=$(subst .,,$(notdir $(PWD)))_workspace



instead to let make to all the string processing itself.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...