Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell calling - not taking variables literally 1

Status
Not open for further replies.

Graeme06

Technical User
Jun 6, 2006
60
Okay one more question (hopefully the last one).

Here's the command I'm using:
qx{mysql -h localhost -u pingbase -fGGfGGf pingbase -e "update servers set last_status = current_status where name = $server; update servers set current_status = 1 where name = $server; update servers set updated = null where name = $server;"};

Basically what this does is send a command to the Unix shell that opens MySQL and executes a few queries. THe problem is that in there I have the variable $server and I think instead of sending the value that $server represents, it is sending literally "$server". The problem is (I think) that its enclosed in qutation marks but I have to do this for the Unix command to be correct

Any idea how to send the value of $server instead of literally "$server"?

Thanks,
Graeme
 
I don't think your problem is what you think it is.
I think it's quite the contrary.
Try this:
Code:
qx{mysql -h localhost -u pingbase -fGGfGGf pingbase -e "update servers set last_status = current_status where name = '$server'; update servers set current_status = 1 where name = '$server'; update servers set updated = null where name = '$server';"};

BTW: Have you considered rewriting this to use the DBI module instead? It could be a wise idea, particularly if $server is read in from somewhere (avoid sql injection attacks).



Trojan.
 
oh so basically you need to put single quotations around the variables? I figured it was the other way around (putting quotes would make it taken literally)

Anyways I'll try that tomorrow. And The reason I'm using this command line call and not DBI module is that it apparntly isn't installed on the version of Perl that I have to use for this. The only other alternative I could think of was to call a PHP script from within Perl, but this seems like a waste. Plus security isn't a huge concern for this.

Thanks for your help.
 
Okay nevermind, that doesn't work at all, it doesn't give a MYSQL error anymore, but the database still doesn't change. I think its still sending literally "$server" instead of the variable represented by $server. Because if I replace $server with what its supposed to represent in that script, it works fine.
 
so would this work
Code:
qx{mysql -h localhost -u pingbase -fGGfGGf pingbase -e "update servers set last_status = current_status where name = '" . $server . "'; update servers set current_status = 1 where name = '" . $server . "'; update servers set updated = null where name = '" . $server . "';"};

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
thanks, but that does the exact same thing.
 
Instead of using $server, hard code what that value is supposed to be and test the code.

and/or try this:

Code:
my $msql = qq~mysql -h localhost -u pingbase -fGGfGGf pingbase -e "update servers set last_status = current_status where name = '$server'; update servers set current_status = 1 where name = '$server'; update servers set updated = null where name = '$server';"~;
print $msql; #so you see what the string is
qx{$msql};
 
I'd go with what Trojan suggested
The Perl DBI, updated 3 days ago, by the man himself Tim Bunce

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
okay im an idiot. I had the wrong variable name in there. But me posting this wasn't completely pointless, as what Trojan said helped me with another problem where MySQL required the single quotes around strings.

So thanks (and Paul, I don't have access to the DBI, if I did it would make things much better.)
 
You can install modules locally using the 'use lib' directive, and what version of perl are you on? AFAIK, the DBI should be part of the core perl distro since about 5.6 (no sources ;-))

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I only have access to version 5.005_03, and I don't think the DBI library is installed.

 
google "perl use lib", you'll be glad you did ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
i dont have access to the server, so i cant place anything in the lib directory. And for some reason people who do have access to the server aren't allowed to install any aditional libraries.
 
In all honesty, I'd start looking for another job, there are tools written and tested by the Open Source Community, and you're not allowed to use them!!! Probably the single biggest asset of perl is CPAN and you can't use it, but you are permitted to reinvent [insert deity here] knows how many wheels...

If it was just a host, I'd say move hosts. Not trying to put you on a downer on a Friday afternoon, just saying how *I* see it ...


Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top