Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

CX or System

by TCPDump (Initiate)
on Apr 04, 2012 at 18:26 UTC ( #963503=perlquestion: print w/replies, xml ) Need Help??
TCPDump has asked for the wisdom of the Perl Monks concerning the following question:

I am a fairly compete newbie to Perl, but am wanting to learn because it seems to give me more power than other things I am trying.

I have the following miniature program:

========================
# !/usr/bin/perl -w system ("clear"); my $dbpass=qx{ password_decode $(grep "name=\"db\"" /opt/SecureSphere/ +etc/bootstrap.xml|cut -d\" -f4) }; print "Password: $dbpass\n";

========================

What I am trying to do is run the system command:

password_decode $(grep "name=\"db\"" /opt/SecureSphere/etc/bootstrap.xml|cut -d\" -f4)

and then pass the output of this command to $dbpass

Then print $dbpass

Should be fairly simple.....but...I get the following:

sh: -c: line 0: unexpected EOF while looking for matching `"' sh: -c: line 1: syntax error: unexpected end of file System Password:

It clears the screen so line 3 is executing, and it seems to do line 7 correctly.....which seems to indicate the problem is in line 5

By itself the command runs without a problem and decodes the password involved. So there has to be something I am missing with the cx command.

Any help would be appreciated as I have a networking background, not a programming background, also if you have good references, let me know them.

Many, many thanks in advance

TCPDump

Replies are listed 'Best First'.
Re: CX or System
by Eliya (Vicar) on Apr 04, 2012 at 19:03 UTC

    qx acts like a double-quoted (interpolated) string, so you need to escape stuff, i.e.

    my $dbpass=qx{ password_decode \$(grep "name=\\"db\\"" /opt/SecureSphe +re/etc/bootstrap.xml|cut -d\\" -f4) };

    (untested)

    Alternatively, declare your command using a non-interpolating quoting mechanism, and then pass it to qx{}:

    my $cmd = q{ password_decode $(grep "name=\"db\"" /opt/SecureSphere/et +c/bootstrap.xml|cut -d\" -f4) }; my $dbpass = qx{ $cmd };

    Hint: replace qx{} with qq{} and print it, and you'll see what the shell sees with your version, e.g.

    password_decode 999 101 104 814 999 1035 1070grep "name="db"" /opt/Sec +ureSphere/etc/bootstrap.xml|cut -d" -f4)
Re: CX or System
by jeffa (Bishop) on Apr 04, 2012 at 21:13 UTC

    There are several CPAN modules available right now that will parse XML for you into a Perl data structure, such as XML::Simple. While your approach works for your needs, consider "doing it the right way" by parsing the XML file into a data structure and pull the proper piece of data out, all within the context of your code, not your code and several other forked processes (cut, grep, etc.).

    # the following is untested use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml = XMLin( '/opt/SecureSphere/etc/bootstrap.xml' ); my $db = $xml->{path}{to}{db}; print Dumper $db; my $pass = `password_decode $db`; print "$pass\n";


    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963503]
Approved by Happy-the-monk
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-07-22 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (455 votes). Check out past polls.

    Notices?