Shell – stdin ‘hop’ over process

io-redirectionshell

Is there a way that stdin can 'hop' over a process? For example, in the following command,

cat file | ssh host 'mkdir -p /some/directory && cat > /some/directory/file'

This will send the stdin from the first cat to mkdir and the second cat will recieve no stdin. I want the stdout from the first cat to hop over mkdir and only be sent to the second cat. I am aware that you can run something like:

cat file | ssh host 'cat > /tmp/file2 ; mkdir -p /some/directory && mv /tmp/file2 /some/directory/'

That only works when copying a file or

cat file | ssh host 'tee >(mkdir -p /some/directory) >/some/directory/file'

But that only works because the mkdir command does not use stdin. Is there a command that will execute a command that replicates this functionality? Something like:

cat file | ssh host 'stdinhop mkdir -p /some/directory | cat > /some/directory/file'

where stdinhop would not send its stdin to mkdir, but it redirect it to stdout so the second cat can read it?

Best Answer

You can redirect the first command's stdin from /dev/null:

anthony@Watt:~$ echo -e 'hello\nworld' | ssh localhost 'cat < /dev/null && cat -n'
     1  hello
     2  world

The lines are numbered, so the 2nd cat got them.

If not using ssh in there, you'd use a subshell: echo -e 'hello\nworld' | ( cat < /dev/null && cat -n )

Related Question