I'm trying to construct some docker command from an arbitrary environment json array, so I did some parsing with jq and then tried to pass it to xargs. However that seems to be getting parsed as one single flag?
echo '{"a": "b", "c": "d"}' | jq -r '. | to_entries | map([.key,.value] | join("=")) | join(" -e ")' | xargs -I '{}' docker run -e {} busybox:latest env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=830b80ada4a8
a=b -e c=d
HOME=/root
Yet when I print out xargs with -t
it works fine for me?
echo '{"a": "b", "c": "d"}' | jq -r '. | to_entries | map([.key,.value] | join("=")) | join(" -e ")' | xargs -t -I '{}' docker run -e {} busybox:latest env
# prints out
docker run -e a=b -e c=d busybox:latest env
# which works if I copy paste?
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=854ec1dc608e
a=b
c=d
HOME=/root
CodePudding user response:
I can't reproduce that -t
behavior with Linux's xargs
, anyway below CLI should deterministically work, assuming that the K=V
env array is "clean" enough (i.e. without special characters).
Details:
- use
jq
to build a newline- separated k=v list - use
sed
to prefix it with-e
- use
xargs bash -c '.... ${@} ...' --
construct, to let bash "expand" the whole passed array of arguments
echo '{"a": "b", "c": "d"}' |\
jq -r '. | to_entries | map([.key,.value] | join("=")) | join("\n")' |\
sed 's/^/-e /' |\
xargs bash -c 'docker run "${@}" busybox:latest env' --