0

I'm pretty new to xargs

I have a command

kubectl taint nodes xx.xx.xx.xx dedicated=test:NoSchedule --overwrite=true

I have my command , how do i pass the positional values to xargs? they are not getting passed.

echo \$NODES \$key_and_value \$overwrite \$label  |
  xargs -I -l % sh -c 'kubectl taint nodes  \$0 % \$1 --overwrite=\$2'

basically i'm doing something like this, i'm getting those values from the jenkins ui The values are dynamic

echo \$NODES \$key_and_value \$overwrite  |
  xargs -I% bash -c 'kubectl --kubeconfig ./kube-configs/config \
  taint nodes % "\$0"  "\$1" --overwrite=% "\$2"'`

error :

16:06:42 Error: invalid argument "ip-xx-xx-xx-xx.ec2.internal" for "--overwrite" flag: strconv.ParseBool: parsing "ip-xx-xx-xx-xx.ec2.internal": invalid syntax
1
  • Can you edit your question to include an example of what you're trying to do? Given your example, I don't understand why you need xargs at all. Can't you just run kubectl tain $NODES $key_and_value --overwrite $label? – Andy Dalton Nov 7 '19 at 20:48
1

You must define your positional arguments outside the command string:

 echo a b c d  | xargs -I VALUES sh -c "echo \$4 \$3 \$2 \$1" command VALUES

This is not well described (in my case from man dash )

-c

Read commands from the command_string operand instead of from the standard input. Special parameter 0 will be set from the command_name operand and the positional parameters ($1, $2, etc.) set from the remaining argument operands.

But behaves like better explained in man bash:

-c

If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages.

Note that the command_string is set as first argument after the command string in quotes (=$0)

In your case, you used the -I option without argument which means that the values xargs reads are not just appended but need to be called via '{}'. So either use this:

 echo 1 2 3 4 | xargs sh -c "echo \$4 \$3 \$2 \$1" command

OR this

 echo 1 2 3 4 | xargs -I sh -c "echo \$4 \$3 \$2 \$1" command '{}'

Hint: use the -x option from sh and the -t option from xargs to better track odd behavior. They will print each command line before executing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.