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
Solved

Getopt:STD in .bat file

Posted on 2010-08-26
6
761 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: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 53

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
network copying issue 4 67
Command prompt output to variable? 9 59
Windows Updates Batch Files (now with executables!) 32 59
Perl Versus AWK? 7 52
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

861 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