我今天写了一个剧本。这样做的目的是自动将新的虚拟主机添加到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. *****#
答案 0 :(得分:1)
当GetOptions
试图呼叫您的add_vhost
时,变量$domain
和$client
可能无法初始化。
您可以将代码更改为
my $add = undef;
...
GetOptions (
'add' => \$add,
...
if ($add) {
add_vhost();
}
...
顺便说一句,您最好将add_vhost
的定义移出该条件语句,并将$domain
之类的信息作为参数传递给add_vhost
,而不是使用全局变量。< / p>
答案 1 :(得分:0)
#!/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";
}
答案 2 :(得分:-1)
我建议更改一些样式以清理代码。
die
而不是else
之后。 GetOptions
的参数,以简化操作。@ARGV
之前,您在GetOptions
上的错误检查是不必要的。让GetOptions
通过获取所需参数来处理它。实际上,您目前在有效的调用方法--domain=value --client=value' because only 2 elements will be in
@ ARGV`与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";
}
}