Solved

Getopt:STD in .bat file

Posted on 2010-08-26
6
744 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
ID: 33536393
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 52

Expert Comment

by:Bill Prew
ID: 33536438
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
ID: 33536637
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 33537066
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
ID: 33538695
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
ID: 33605464
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

20 Experts available now in Live!

Get 1:1 Help Now