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?