Solved

Getopt:STD in .bat file

Posted on 2010-08-26
6
734 Views
Last Modified: 2012-05-10
I have a perl script that use getopt.  It works fine if it is save as *.pl, but it wouldn't work if I change the file type to *.bat.  How could I use Getopt function in a *.bat file?

code:
#!/usr/bin/perl
#perl script starts below here
use Getopt::Std;

getopt("s");

if ($opt_s ne ""){
   goto "STEP".$opt_s;
}
STEP1:
#print "";
print "Step1: Start\n";

STEP2:
print "Step2\n";

STEP3:
print "Step3\n";
0
Comment
Question by:asenna425
6 Comments
 
LVL 22

Expert Comment

by:Bartender_1
Comment Utility
This is because DOS commands don't easily accept user input. Are you sure you have to run this as a batch file? If so, see here about getting batch files to accept user input:
http://www.robvanderwoude.com/userinput.php

Hope this helps!

:o)

Bartender_1
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
It looks like you were running a perl script with either a /s1 or mabe a /s 1 parm passed in to it, it that right?  And you want to do something similar with a BAT file?  

If so, you might want to consider moving to a positional parm, and using that parms value as the switch.  So you could use a command like like:

foo.bat 2

and then the BAT file could do:

@echo off
if not "%~1" == "" goto STEP%~1
exit /b
:STEP1
echo Step 1
exit /b
:STEP2
echo Step 2
exit /b
:STEP3
echo Step 3
exit /b

~bp
0
 

Author Comment

by:asenna425
Comment Utility
Hi billprew,

thanks for the quick response.  I tried your suggestion but it failed to run if i implemented it with some header code...

@rem = 'Perl, ccperl will read this as an array of assignment & skip this block
@D:\Perl\bin\perl.exe -s "%~dp0%~nx0" %*
@SET resexe=%ERRORLEVEL%
@ECHO RESEXE=%resexe%
@GOTO:EOF
@rem ';

#!/usr/bin/perl
#perl script starts below here
use Win32::Process;
use Win32API::File 0.08 qw( :ALL );
use Fcntl ':flock';
use Errno qw (EWOULDBLOCK) ;

### your suggestion starts here ###
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
Comment Utility
You can use "%~f0" btw for the full path.  Does perl run a file other than a .pl extension?  Perhaps you could have it copy the file to a temp location, e.g.

@echo off
@copy /Y "%~f0" "%temp%\%~n0.pl"
@D:\Perl\bin\perl.exe -s "%temp%\%~n0.pl" %*
@SET resexe=%ERRORLEVEL%
@ECHO RESEXE=%resexe%
@GOTO:EOF
@rem ';


Steve
0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
Is this what you're after?  It works fine for me.  Adjust the c:\strawberry\perl\bin\perl.exe to the path of your perl (D:\Perl\bin\perl.exe).
@rem = 'trick to get perl to ignore this junk

@echo off

c:\strawberry\perl\bin\perl.exe %0 %1 %2 %3 %4 %5

SET resexe=%ERRORLEVEL%

ECHO RESEXE=%resexe%

@rem';



use Getopt::Std;



getopt("s");



if ($opt_s ne ""){

   goto "STEP".$opt_s;

}

STEP1:

#print "";

print "Step1: Start\n";



STEP2:

print "Step2\n";



STEP3:

print "Step3\n";

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
When you rename it to a .bat, how were you starting it?  If you didn't have the header code (that you now have), unless you specifically told perl to run it, windows would not have known it was a perl file, and wouldn't have executed it correctly at all (windows doesn't use the #! line).  The installation of perl associates *.pl files with perl, so windows knows how to execute them.  As perl doens't care about the file name or extension, I think the error is related to how windows is handling the file.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now