Reputation: 46904
In my script, sometimes I need a script to be passed to SSH, and sometimes let the user interact.
I tried this trick:
if [ "" != "$SSH_SCRIPT" ] ; then
...
else
SSH_SCRIPT="&0";
fi
And then:
ssh $SSH_IGNORE_STDIN $SSH_TTY $noKeyCheck -i $AWS_KEY $USER_@$HOST_ "$COMMAND_SSH" <$SSH_SCRIPT;
Which I assumed would work because cat <&0
works.
But when I use a variable after <
, it works differently - it complains that file &0
was not found. Looks like <&n
is a syntax construct. Reproducible this way:
$ FOO='&0';
$ cat <$FOO
bash: &0: No such file or directory
Is there a way to parametrize redirection by file descriptor number?
Enclosing the whole command to bash -c "..."
isn't probably a good idea because it makes the whole thing more complicated.
Uploading the command to the remote server and running it there is also a bit overkill.
So far I have resorted to:
if [ "" == "$SSH_SCRIPT" ] ; then
ssh $SSH_IGNORE_STDIN $SSH_TTY $noKeyCheck -i $AWS_KEY $USER_@$HOST_ "$COMMAND_SSH";
else
ssh $SSH_IGNORE_STDIN $SSH_TTY $noKeyCheck -i $AWS_KEY $USER_@$HOST_ "$COMMAND_SSH" <$SSH_SCRIPT;
fi
Upvotes: 0
Views: 127
Reputation: 532408
bash
will recognize /dev/stdin
as the argument to a redirection whether or not your file system exposes such a file.
if [ -z "$SSH_SCRIPT" ]; then
SSH_SCRIPT=/dev/stdin
fi
ssh ... < "$SSH_SCRIPT"
Upvotes: 2