我今天写了剧本。这样做的目的是自动向Apache添加新的虚拟主机。应该使用以下命令执行脚本:
./new_vhost.pl --add --domain google.com --client google. 然而,它不能很好地工作,相反,它会给我带来错误
未初始化值$domain在级联(.)中的使用或串
你们以前有过这种经历吗?我在想原因是“。”不过,在google.com中,我对此并不十分肯定。我仍然在谷歌上寻找答案,但不幸的是,没有什么能帮我解决这个问题。
代码:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Cwd;
use POSIX "strftime";
my $dir = getcwd;
my $conf = 'test.conf';
my $domain = undef;
my $client = undef;
my $help = undef;
my $ltime = undef;
if (-e $conf) {
sub usage {
die "Usage: $0 [--add | --remove] [--domain google.com] [--client google].\n";
}
usage unless @ARGV > 3;
GetOptions (
'add' => \&add_vhost,
# 'remove' => \&rem_vhost,
'domain=s' => \$domain,
'client=s' => \$client,
'help' => \&usage
) || usage;
$domain = lc($domain);
$client = uc($client);
$ltime = localtime;
sub add_vhost {
open (CONF, ">>$conf") || die "ERROR: unable to open $conf.\n";
print CONF "#***** Start of configuration for $domain. *****#";
print CONF "\n# Domain: $domain\n";
print CONF "# Client: $client\n";
print CONF "# Date: $ltime\n";
print CONF "<VirtualHost *:1111>\n";
print CONF " ServerAdmin admin\@$domain\n";
print CONF " DocumentRoot /var/www/html/$domain\n";
print CONF " ServerName $domain\n";
print CONF " ServerAlias www.$domain\n";
print CONF " ErrorLog /var/log/httpd/$domain/$domain-error_log\n";
print CONF " CustomLog /var/log/httpd/$domain/$domain-access_log common\n";
print CONF "</VirtualHost>\n";
print CONF "#***** End of configuration for $domain. *****#\n";
close CONF;
if ($? != 0) {
die "Error: unable to create a new configuration.\n";
} else {
print "OK: new configuration for $domain has been added.\n";
}
}
} else {
die "ERROR: $conf not found.\n";
}错误:
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 37.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 38.
Use of uninitialized value $client in concatenation (.) or string at ./new_vhost.pl line 39.
Use of uninitialized value $ltime in concatenation (.) or string at ./new_vhost.pl line 40.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 42.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 43.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 44.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 45.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 46.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 46.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 47.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 47.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 49.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 54.
OK: new configuration for has been added.运行脚本后的test.conf。
#***** Start of configuration for . *****#
# Domain:
# Client:
# Date:
<VirtualHost *:1111>
ServerAdmin admin@
DocumentRoot /var/www/html/
ServerName
ServerAlias www.
ErrorLog /var/log/httpd//-error_log
CustomLog /var/log/httpd//-access_log common
</VirtualHost>
#***** End of configuration for . *****#但我以为会发生这样的事..。
#***** Start of configuration for google.com. *****#
# Domain: google.com
# Client: GOOGLE
# Date: Tue Apr 8 17:27:39 2014
<VirtualHost *:1111>
ServerAdmin admin@google.com
DocumentRoot /var/www/html/google.com
ServerName google.com
ServerAlias www.google.com
ErrorLog /var/log/httpd/google.com/google.com-error_log
CustomLog /var/log/httpd/google.com/google.com-access_log common
</VirtualHost>
#***** End of configuration for google.com. *****#发布于 2014-04-08 09:13:24
当GetOptions试图调用您的add_vhost时,可能不会初始化变量$domain和$client。
您可以将代码更改为
my $add = undef;
...
GetOptions (
'add' => \$add,
...
if ($add) {
add_vhost();
}
...顺便说一句,最好将add_vhost的定义从该条件语句中移出,并将这些信息(如$domain作为参数传递给add_vhost,而不是使用全局变量)。
发布于 2014-04-08 09:17:20
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Cwd;
use POSIX "strftime";
my $dir = getcwd;
my $conf = 'test.conf';
my $domain = undef;
my $client = undef;
my $help = undef;
my $ltime = undef;
my $to_add_vhost = undef;
if (-e $conf) {
sub usage {
die "Usage: $0 [--add | --remove] [--domain google.com] [--client google].\n";
}
usage unless @ARGV > 3;
GetOptions (
'add' => \$to_add_vhost,
# 'remove' => \&rem_vhost,
'domain=s' => \$domain,
'client=s' => \$client,
'help' => \&usage
) || usage;
$domain = lc($domain);
$client = uc($client);
$ltime = localtime;
&add_vhost if (defined $to_add_vhost);
sub add_vhost {
open (CONF, ">>$conf") || die "ERROR: unable to open $conf.\n";
print CONF "#***** Start of configuration for $domain. *****#";
print CONF "\n# Domain: $domain\n";
print CONF "# Client: $client\n";
print CONF "# Date: $ltime\n";
print CONF "<VirtualHost *:1111>\n";
print CONF " ServerAdmin admin\@$domain\n";
print CONF " DocumentRoot /var/www/html/$domain\n";
print CONF " ServerName $domain\n";
print CONF " ServerAlias www.$domain\n";
print CONF " ErrorLog /var/log/httpd/$domain/$domain-error_log\n";
print CONF " CustomLog /var/log/httpd/$domain/$domain-access_log common\n";
print CONF "</VirtualHost>\n";
print CONF "#***** End of configuration for $domain. *****#\n";
close CONF;
if ($? != 0) {
die "Error: unable to create a new configuration.\n";
} else {
print "OK: new configuration for $domain has been added.\n";
}
}
} else {
die "ERROR: $conf not found.\n";
}发布于 2014-04-09 01:46:47
我建议您进行一些样式更改,以清理您的代码。
die立即而不是在else之后。GetOptions的参数,以简化操作。@ARGV之前对GetOptions进行错误检查是不必要的。让GetOptions通过拥有所需的参数来处理这个问题。目前,在有效的调用方法--domain=value --client=value' because only 2 elements will be in@ARGV` ` is 4下,您将失败。这会将您的代码清理为以下内容:
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use Getopt::Long;
use Cwd;
use POSIX "strftime";
my $dir = getcwd;
my $conf = 'test.conf';
die "ERROR: $conf not found.\n" if ! -e $conf;
sub usage {
die "Usage: $0 [--add | --remove] [--domain google.com] [--client google].\n";
}
GetOptions(
'add' => \my $add,
# 'remove' => \&rem_vhost,
'domain=s' => \my $domain,
'client=s' => \my $client,
'help' => \&usage
) or usage();
$domain = lc($domain);
$client = uc($client);
my $ltime = localtime;
if ($add) {
add_vhost();
}
sub add_vhost {
open my $fh, '>>', $conf;
print $fh <<"END_CONF";
#***** Start of configuration for $domain. *****#
# Domain: $domain
# Client: $client
# Date: $ltime
<VirtualHost *:1111>
ServerAdmin admin\@$domain
DocumentRoot /var/www/html/$domain
ServerName $domain
ServerAlias www.$domain
ErrorLog /var/log/httpd/$domain/${domain}-error_log
CustomLog /var/log/httpd/$domain/${domain}-access_log common
</VirtualHost>
#***** End of configuration for $domain. *****#
END_CONF
if ($? != 0) {
die "Error: unable to create a new configuration.\n";
} else {
print "OK: new configuration for $domain has been added.\n";
}
}https://stackoverflow.com/questions/22932183
复制相似问题