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?

3
  • 1
    Variables aren't expanded in single quotes. Commented Feb 26, 2017 at 12:20
  • Oh that's great! Such an easy mistake. Thanks for thequick respond! Commented Feb 26, 2017 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. Commented Feb 26, 2017 at 17:46

1 Answer 1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.