Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getopt:STD in .bat file

Posted on 2010-08-26
6
Medium Priority
?
807 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Expert Comment

by:Christopher McKay
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 57

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

661 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