• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

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?
0
itcdr
Asked:
itcdr
  • 4
  • 3
1 Solution
 
Adam314Commented:
Enclose the command in single quotes, not double quotes
0
 
ozoCommented:
ssh root@10.0.0.1 "perl -e '"'$var="a";print $var'"'"
0
 
ozoCommented:
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\";' "
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
itcdrAuthor Commented:
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?
0
 
ozoCommented:
I put the $ and the " in ''
and put the ' in ""
0
 
itcdrAuthor Commented:
Your method does work, but I still understand the why it works. Can you explain?
0
 
ozoCommented:
"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
0
 
itcdrAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now