1

I am sorry if that is an unnecessary question but I already searched the internet and unfortunately the command is quite complex for my relations.

I am trying to pipe a password into a sudo command in order to execute a command with root rights:

 echo 'PASSWORD' | sudo -S [...]

This works for me.

But as soon as I need the root rights for a command which includes an own pipeline I run into troubles:

 echo 'PASSWORD' | sudo -S printf '%s' "OTHER_PASSWORD" | cryptsetup --batch-mode --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 3000 luksFormat /dev/sd* -

Therefore I try to use a subshell command:

echo 'PASSWORD' | sudo -S bash -c 'printf '%s' "OTHER_PASSWORD" | cryptsetup --batch-mode --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 3000 luksFormat /dev/sd* -'

Unfortunately the inverted commas cannot be used since they are in conflict with them in the printf command.

To solve this problem I tryed several brackets. This one worked:

echo 'PASSWORD' | sudo -S bash -c '(printf '%s' "OTHER_PASSWORD" | cryptsetup --batch-mode --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 3000 luksFormat /dev/sd* -)'

But if I want to use a previously created variable I even get with exporting them into trouble:

 export storage="/dev/sda"
 echo 'PASSWORD' | sudo -S bash -c '(printf '%s' "OTHER_PASSWORD" | cryptsetup --batch-mode --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 3000 luksFormat $storage -)'

 ERROR: Device - doesn't exist or access denied.

Is there an easy way to solve this issue?

closed as off-topic by Anthony Geoghegan, Jeff Schaller, Satō Katsura, countermode, GAD3R Feb 26 '17 at 22:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Anthony Geoghegan, Jeff Schaller, Satō Katsura, countermode, GAD3R
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Variables aren't expanded in single quotes. – choroba Feb 26 '17 at 12:20
  • Oh that's great! Such an easy mistake. Thanks for thequick respond! – linuxguy Feb 26 '17 at 12:23
  • @choroba You should post that as an answer so it can be accepted. Otherwise, this question would be best closed as a typographical error. – Anthony Geoghegan Feb 26 '17 at 17:46
1

Variables aren't expanded in single quotes.

sudo -S bash -c '(printf '%s' "OTHER_PASSWORD" | ... luksFormat $storage -)'
                                                                ~~~~~~~~

Note that the quotes around %s in fact mean that the string is unquoted (as the syntax highlighting here shows us), but fortunately it doesn't matter, as %s has no special meaning.

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