leo
leo

Reputation: 1185

Linux a long shell command with pipe operation

This is my command.

ssh username@hostname "cd /usr ; echo \"password\" | sudo -S tar cpf - . --ignore-failed-read" | tar xpf - -C /usr

The problem is that the latter tar command needs sudo, so if I change my code like ssh username@hostname "cd /usr ; echo \"password\" | sudo -S tar cpf - . --ignore-failed-read" | sudo tar xpf - -C /usr。But I want to run it automatically, this case is not what I want.

If I change command to

ssh username@hostname "cd /usr ; echo \"password\" | sudo -S tar cpf - . --ignore-failed-read" | echo "password" | sudo -S tar xpf - -C /usr。In this case I can not get the former tar data flow。

So how can I simultaneously get the former tar data flow and give sudo to my latter tar command?

PS: I don't want to create actual tar file。I want it compress and uncompress on the fly.

Anyone can help me? Thank you very much~!

Upvotes: 0

Views: 143

Answers (1)

William Pursell
William Pursell

Reputation: 212198

Pipe the data to a fifo, read the password from stdin:

trap 'rm -f /tmp/fifo' 0
mkfifo /tmp/fifo
ssh username@hostname 'cd /usr ; echo "password" |
     sudo -S tar cpf - . --ignore-failed-read' > /tmp/fifo & 
echo "${password?}" | sudo -S sh -c 'tar xpf - -C /usr < /tmp/fifo'

Upvotes: 1

Related Questions