【发布时间】:2014-05-20 20:42:21
【问题描述】:
我今天写了一个脚本。这样做的目的是自动将新的虚拟主机添加到 Apache。该脚本应使用以下命令执行:
./new_vhost.pl --add --domain google.com --client google.
但是,它并不能很好地工作,而是会引发错误
在连接 (.) 或字符串中使用未初始化的值 $domain
你们以前有过这样的经历吗?我在想原因是'。在 google.com 虽然我不太确定。我仍在 Google 上寻找答案,但遗憾的是没有什么能帮我解决这个问题。
代码:
#!/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. *****#
【问题讨论】:
-
这个脚本对我来说运行良好,除了你从不打电话给
sub add_vhost。我怀疑这不是您正在运行的脚本。 -
嗨@M42!我第一次运行脚本时,它不起作用。我刚刚意识到我忘记声明一个新的未定义变量
add并且我错误地将add参数指向add_vhost子程序,它立即执行而没有得到我传递给$domain和$client的信息.我很高兴它现在运行良好。也感谢您的时间! :)
标签: string perl initialization concatenation