简体   繁体   English

无法使用命令行将电子邮件地址传递给Perl脚本

[英]Unable to pass email address to Perl script using command line

I am trying to pass two email addresses to MySQL to find their accounts. 我正在尝试将两个电子邮件地址传递给MySQL以查找其帐户。 I call my script like this: 我这样称呼我的脚本:

./message-2.pl  user1@s  'rech user2@s 22'

Here is my script: 这是我的脚本:

#!/usr/bin/perl

use warnings;
use strict;
use DBI;
use IO::Handle;

our $calerid = "'$ARGV[0]'";
our $message = "$ARGV[1]";
our $msg_arg1 = qx(echo $message | awk '{print \$1}');
our $msg_arg2 = qx(echo $message | awk '{print \$2}');
my $share_reciver =  "'$msg_arg2'";

my $database = "mya2billing";
my $host = "localhost";
my $port = "3306";
my $user = "root";
my $pass = "xxxxxx";
my $dsn = "DBI:mysql:database=$database;host=$host;port=$port";
my $sth;

# find card id
my $query1 = "SELECT  id_cc_card FROM cc_callerid WHERE cid = $calerid ;";
my $dbh = DBI->connect($dsn, $user, $pass) || die "Could not connect to database:$DBI::errstr";
our $card;
print " query=$query1\n";

if (!($sth = $dbh->prepare($query1))) {
    die ("Failed to prepare statement: " . DBI->errstr);
}                           
$sth->execute() or die $DBI::errstr;
my @result;  
while (@result = $sth->fetchrow_array) { # retrieve one row
    ($card) = @result;
    print "card id found: $card \n";
}   

if ($msg_arg1 =~ "rech" or $msg_arg1 =~ "Rech"  or $msg_arg1 =~ "bal" or $msg_arg1 =~ "Bal") {
    recharge();
}   

sub recharge {
    print " we are in rech section\n";
    our $msg_arg2 = qx(echo $message | awk '{print \$2}');
    our $share_reciver="$msg_arg2";


    my $query2 = "SELECT id_cc_card FROM cc_callerid WHERE cid = '$share_reciver' ";
    my $dbh = DBI->connect($dsn, $user, $pass);
    $sth = $dbh->prepare($query2);
    $sth->execute() or die $DBI::errstr;

    print " query=$query2\n";
    my $receiver_id;

    while (my @row = $sth->fetchrow_array) {
        ($receiver_id) = @row;
        print " we are in query section\n";
        print "receiver_cardid:$receiver_id \n";
    }

    exit 1;
}

Here is the output: 这是输出:

[root@laptop Desktop]# ./message-2.pl  user1@s  'rech user2@s 22'
query=SELECT  id_cc_card FROM cc_callerid WHERE cid = 'user1@s' ;
card id found: 10 
we are in rech section
query=SELECT id_cc_card FROM cc_callerid WHERE cid = 'user2@s
' 

There is no error. 没有错误。 query1 is working fine but query2 is not working. query1工作正常但query2无法正常工作。 I tried to use placeholders instead of $share_reciver but it is not working. 我试图使用占位符而不是$share_reciver但它不起作用。 I also tried with single quotes and without quotes. 我也试过单引号而没有引号。 If I use $calerid instead of $share_reciver then it works fine. 如果我使用$calerid而不是$share_reciver那么它可以正常工作。 Can anyone tell me what I am missing? 谁能告诉我我失踪了什么?

Use placeholders and bound variables for your sql queries. 为sql查询使用placeholders and bound variables Never just put raw variables in your sql string: 永远不要只是将原始变量放在sql字符串中:

my $sth = $dbh->prepare(q{SELECT id_cc_card FROM cc_callerid WHERE cid=?})
$sth->execute($calerid) or die $dbh->errstr;

You'll have to adjust your initialization of $calerid to not have single quotes around it, as using placeholders takes care of that: 您必须调整$calerid的初始化, $calerid周围没有单引号,因为使用占位符可以解决这个问题:

our $calerid = $ARGV[0];

Also, it should be sufficient to only connect to your database once. 此外,仅连接到您的数据库一次就足够了。 You don't have to do it for each query. 您不必为每个查询执行此操作。 Therefore, just put your connect statement at the beginning of the script. 因此,只需将connect语句放在脚本的开头即可。 You can even isolate all the constants the db connect relies on so that they aren't available later in your script: 您甚至可以隔离db connect依赖的所有常量,以便以后在脚本中不可用:

our $dbh = do {
    my $database = "mya2billing";
    my $host = "localhost";
    my $port = "3306";
    my $user = "root";
    my $pass = "xxxxxx";
    my $dsn = "DBI:mysql:database=$database;host=$host;port=$port";

    DBI->connect($dsn, $user, $pass) or die "DB connect failed: $DBI::errstr";
};

To reiterate, your second query should also use placeholders, like so: 重申一下,你的第二个查询也应该使用占位符,如下所示:

my $sth = $dbh->prepare('SELECT id_cc_card FROM cc_callerid WHERE cid=?');
$sth->execute($share_reciver) or die $dbh->errstr;

Finally, it doesn't appear you're validating the returned content from external resources qx{} . 最后,您似乎没有验证来自外部资源qx{}的返回内容。 At the very least, I suspect that returned content might contain return characters you aren't expecting. 至少,我怀疑返回的内容可能包含您不期望的返回字符。 Therefore just whip out the chomp : 因此,只需chomp

our $msg_arg2 = qx(echo $message | awk '{print \$2}');
chomp $msg_arg2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在多个文件上使用 perl 脚本的命令行 - Command line for using perl script on multiple files 提取电子邮件地址 Perl 脚本 - Extract Email Address Perl Script 如何使用perl将命令行参数与perl模块一起传递? - How to pass command line arguments along with perl modules using perl? 如何使用正则表达式在perl中解析电子邮件地址行? - How to parse a line for an email address in perl using regular expression? 无法从perl脚本向dir命令传递参数 - unable to pass a parameter to dir command from perl script 如何将强制和可选的命令行参数传递给 perl 脚本? - How to pass both mandatory and optional command line arguments to perl script? 如何将带有后向引用的替换正则表达式作为命令行参数传递给Perl脚本 - How to pass a replacing regex with a backreference as a command line argument to a Perl script 传递命令行参数以及来自STDIN的Perl脚本输入? - Pass command line arguments as well as input from STDIN for Perl script? 如何将替换正则表达式作为命令行参数传递给perl脚本 - How to pass a replacing regex as a command line argument to a perl script 一个perl命令行,用于打印用于运行脚本的perl版本 - A perl command line that prints the perl version it is using to run your script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM