Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file in Windows XP using Perl? 1

Status
Not open for further replies.

ResinCoreSolder

Technical User
Joined
Aug 26, 2007
Messages
3
Location
US
I have three web sites, run Windows XP and Apache server on my development computer. I have three versions of the "httpd.conf" config file stored unser unique names, and wish to build a batch file to delete the working "httpd.conf," copy "version 1 httpd.conf" and paste as "copy of version 1 httpd.conf," rename the copy to "httpd.conf," and start my server. I then go to localhost and find the first of three websites.

OK, so I dig up a good batch file that goes like this:

@echo off
cls
echo.
echo 1 First site
echo 2 Second site
echo 3 Third site
CHOICE /C:123 /N choose >nul
IF ERRORLEVEL == 3 goto THIRD
IF ERRORLEVEL == 2 goto SECOND
IF ERRORLEVEL == 1 GOTO FIRST

THIRD:
echo Selected Third site
pause
commands to delete, copy, paste and rename third version of config file
exit

SECOND:
and so on.

But for Windows not having a CHOICE command from what I could see on a microsoft page, I have no way to do this using the MS-DOS commands that I have been familiar with.

However, as I have Perl installed at C:\usr\bin\perl.exe, which means my Perl programs run on my Windows computer on the Apache server exactly as they do on my Linux Webhost without editing the shebang line, I should be able to run a Windows batch file of Perl commands.

I opened a Windows command window and entered \usr\bin\perl -e 'print"hello"'
and that is followed with:
Can't find string terminator "'" anywhere before EOF at -e line 1.

Another site spoke about backticks and I think that refers to the character left of the 1 key under the tilde. I replaced ' with ` and my message was the same, except now it can't find "`" anywhere.

Can someone please advise me how to begin a batch file in Perl and be able to run it on Windows XP? And what was I doing wrong on that perl -e experiment? I was in Settings and Documents\ ... \My Documents, and maybe I should try that experiment at C:\ or a path without spaces.....

Thanks.
Larry
 
I would suggest doing the whole thing in perl. It seems very easy and could be done with just a few commands.

Something like this (untested, unverified, no promises, just thinking out loud)

use File::Copy;

print "Which site";
$in = <STDIN>;
chomp $in;
if ($in == 1) {
unlink /path/to/httpd.conf || die "can't delete file: $!";
copy("/path/to/newhttpd.conf", "/path/to/httpd.conf") || die "can't copy file: $!\n";
`/path/to/apache/bin/appachectl restart site1`;
`start "\program files\internet explorer\iexplore"
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Have you heard of Virtual Hosts? You can easily host multiple completely different websites with one Apache httpd.conf

For instance:

Code:
NameVirtualHost *:80

<VirtualHost *:80>
   ServerName hostname.tld
   DocumentRoot C:/[URL unfurl="true"]www/hostname.tld/public_html[/URL]
</VirtualHost>

# and do this for each host name

If the only hostname you have is 127.0.0.1 (or localhost), you can create additional aliases to it in your Windows hosts file.

Edit C:/Windows/System32/drivers/etc/hosts (a text file) in Notepad and copy the example shown, which is along the lines of:

Code:
127.0.0.1     localhost

And copy that line and make up new host names, i.e.

Code:
127.0.0.1     localhost
127.0.0.1     site1
127.0.0.1     site2
127.0.0.1     site3

And then in your one httpd.conf, just make VirtualHosts for each hostname.

Code:
<VirtualHost *:80>
   ServerName site1
   DocumentRoot C:/[URL unfurl="true"]www/site1/public_html[/URL]
</VirtualHost>

(you'd naturally change the DocumentRoot to whereever this site is being served from)

Then, just run Apache once, go to or or and access your three different sites at the same time.

If you have actual domain names, it's even easier than this because you don't have to modify your hosts file.

I've successfully hosted many domain names from my PC (on both Windows XP and Linux) using VirtualHosts. Even now, I host cuvou.com, kirsle.net, project-fearless.com, upsilon.cuvou.com, and a handful of others, and each site has their very own document root, their own webmaster e-mail addresses, and everything. It's almost as though they all were their very own dedicated Apache servers.

Code:
<VirtualHost *:80>
   ServerName site1.com
   ServerAlias [URL unfurl="true"]www.site1.com[/URL] *.site1.com
   ServerAdmin webmaster@site1.com
   DocumentRoot /home/site1/public_html
   ErrorDocument 404 /home/site1/public_html/404.shtml
   ErrorDocument 500 /home/site1/public_html/500.shtml
   # just to show how each virtual host can be
   # totally customized
</VirtualHost>

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Thank you all for the input.
Prex1, I found that syntax does indeed work on the command line or from a batch file. At least for one-line commands.

I am still unsure how to begin writing scripts designed for output on my screen instead of to a browser, and running them in Windows.

But Kirsle bumped me in the direction of virtual hosts. I have never taken the time to learn that part of it, as I could just switch files and get my results. Tonight I am happy to report that I have put two virtual hosts up on the same port with two made-up domain names, and got them working. I guess I need to set up three hosts to do it right, as I am trying to display three web sites for development work.

Need to do more work to get the virtual hosts fully capable yet.

Back to the original question, using that batch file, I could have run the server with my choice of different httpd config files, each with a different name, IF I could figure out how to make a menu work in XP's limited batch file capabilities.

Thanks for your guidance, once again.
-Larry
 
Latest update on this question's progress:
I have three different configuration files which permit the main Apache server to display one Web site each at 127.0.0.1:8080. All three are set up to permit shtml, php and define their respective cgi-bin. I can also serve an index.cgi as the start page.

I am still having problems getting a set of VirtualHost containers to serve up that full capability at through (which are mapped to my Hosts file at 127.0.0.1), which problem I will address at an Apache forum.

I have made the basic service of html pages work without any issues.

Meanwhile, I did a search for the choice command that revealed that MS-DOS' choice.com will burn processors while waiting for input, but that choice.exe will work. I have downloaded a choice.exe from a Microsoft site and am in business with my original plan to write a batch file to start Apache with my choice of config files on my Womdows XP computer.

I have successfully run one-line perl code that will display to the screen, using perl -e syntax provided earlier in this thread. I need to spend some more time studying the ways to make a perl script run on the Windows command line with output going to the screen instead of to a Web environment. To do that I will have to read the manual some more and then post a new question.

I thank you all for your input on this thread.

Kind regards,
Larry
 
Your apache questions are probably best directed to the apache forum, but you mention wanting to get into interactive command-line perl scripts.

At the very simplest level, you have access to your arguments via the @ARGV array and to your environment via the %ENV hash. You can prompt the operator and retrieve responses with code like
Code:
print 'Enter code: ';
chomp( my $response = <STDIN> );

For more professional results, investigate Getopt::Long ( for argument switch processing and Term::Readline ( for prompting and user input processing.

I found that within a few months of starting to use perl, I almost completely stopped using a lot of other tools, including c, sh, sed and awk. Perl has, for me, proved to be the best tool in the box by a long chalk.

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top