?
Solved

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

Posted on 2006-05-19
8
Medium Priority
?
504 Views
Last Modified: 2012-06-27
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
Comment
Question by:itcdr
  • 4
  • 3
8 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 16720348
Enclose the command in single quotes, not double quotes
0
 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 16720412
ssh root@10.0.0.1 "perl -e '"'$var="a";print $var'"'"
0
 
LVL 85

Expert Comment

by:ozo
ID: 16720472
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:itcdr
ID: 16720524
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
 
LVL 85

Expert Comment

by:ozo
ID: 16720576
I put the $ and the " in ''
and put the ' in ""
0
 
LVL 1

Author Comment

by:itcdr
ID: 16720618
Your method does work, but I still understand the why it works. Can you explain?
0
 
LVL 85

Expert Comment

by:ozo
ID: 16720747
"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
 
LVL 1

Author Comment

by:itcdr
ID: 16720760
Thanks
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question