I wrote a script today. The purpose of this is to automatically add new virtual host to Apache. The script should be executed using this command:
./new_vhost.pl --add --domain google.com --client google.
However, it doesn't work well instead it throws me errors
Use of uninitialized value $domain in concatenation (.) or string
Have you guys experienced this before? I was thinking the cause was the '.' in google.com though I am not really sure about it. I'm still searching for answers on Google but unfortunately nothing could help me fix this.
Code:
#!/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";
}
Error:
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.
The test.conf after running the script.
#***** 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 . *****#
But I was expecting something like this...
#***** 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. *****#