Reputation: 652
I'm writing a bash script that is intended to execute some command, and depending on some flag, this command should be either executed locally or remotely. This command's output should be redirected to some file, and this file should be on the box that executes the command, that is, on the remote box if the command is executed remotely.
I'm trying things like
#!/bin/bash
REMOTE=1
function f
{
CMD="$@"
if [ "${REMOTE}" == "1" ]
then
ssh some_host "$CMD"
else
$CMD
fi
}
# This executes "echo huhu" remotely and redirects the output into "out" on the remote box.
REMOTE=1 f echo huhu \> out
# This executes "echo haha > out" remotely (without redirection).
REMOTE=0 f echo haha \> out
When I don't escape the >
sign, any output of f is redirected to "out"
on the local box, of course.
How could I avoid this behavior?
Upvotes: 2
Views: 544
Reputation: 58778
Don't use eval
; use arrays instead. And a solution for the SSH command.
Upvotes: 1
Reputation: 852
Write eval $CMD
instead of $CMD
. When $CMD
is expanded the interpretation of redirection has already happened and redirections operations will simply passed as ordinary arguments.
Upvotes: 1