Link to home
Start Free TrialLog in
Avatar of itcdr
itcdrFlag for United States of America

asked on

execute perl one-liner over ssh (problem with quotes)

I'm trying to execute some perl code remotely and I can't seem to figure out what to do with all the quotes. Here is an example:


# ssh root@10.0.0.1 " perl -e '$var=\"a\"; print $var;' "
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.

# ssh root@10.0.0.1 " perl -e '$var="a"; print $var;' "
(nothing happened. It just returned)

# ssh root@10.0.0.1 " perl -e 'print \"a\";' "
a (it works without the variable. why?)

# ssh root@10.0.0.1 "./test_script"
a (it works if I place the perl code in a file first and then execute)

[root@10.0.0.1]# perl -e '$var="a"; print $var;'
a (it works if I execute the code directly on the machine.)



So the code works if I execute on the machine directly or place it in a file first, but the quotes seem to mess up when trying to execute it inside the ssh command. Any ideas?
Avatar of Adam314
Adam314

Enclose the command in single quotes, not double quotes
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to see what is happening
echo " perl -e '$var=\"a\"; print $var;' "
echo " perl -e '$var="a"; print $var;' "
echo "perl -e '"'$var="a";print $var'"'"
echo " perl -e 'print \"a\";' "
Avatar of itcdr

ASKER

Good job!

ssh root@10.0.0.1 "perl -e '"'$var="a";print $var'"'"

does work, but I'm not sure why. I see the only thing you changed was to surround the perl command with '"' (singel-double-single quote) instead of ' (single quote). Can you explain why you did that?
I put the $ and the " in ''
and put the ' in ""
Avatar of itcdr

ASKER

Your method does work, but I still understand the why it works. Can you explain?
"perl -e '" quotes the '
'$var="a";print $var' quotes the $ and the "
"'" quotes the '
This is more of a shell question than a Perl question
Avatar of itcdr

ASKER

Thanks