brand@cad.Berkeley.EDU (Graham Brand) (12/06/90)
I have written a batch file which accepts parameters from the command line using the: if %1==param1 syntax. It works fine except that if I don't pass any parameters, I get the above syntax error message. Is there any way to suppress this? In addition, is there any way of passing a string, the first character of which is a dash (-), as in -f? It doesn't seem to work with the above test. Cheers, -Graham
sab@engr.uark.edu (Steven A. Breuer) (12/06/90)
In article <9467@pasteur.Berkeley.EDU>, brand@cad.Berkeley.EDU (Graham Brand) writes: > I have written a batch file which accepts parameters from the command > line using the: > if %1==param1 > syntax. It works fine except that if I don't pass any parameters, I > get the above syntax error message. Is there any way to suppress this? > > In addition, is there any way of passing a string, the first character > of which is a dash (-), as in -f? It doesn't seem to work with the > above test. > Batch files have trouble with blank parameters (none). try the following "if" statements. It is a good idea to surround the parameters with some character - this should take of errors happening because of no parameters. This should also allow you to use the dash (-) as part of a parameter. if "%1"=="" goto NOPARMS if "%1"=="param1" goto PARAM1 Steve Breuer sab@engr.uark.edu
kdq@demott.com (Kevin D. Quitt) (12/06/90)
In article <9467@pasteur.Berkeley.EDU> brand@cad.Berkeley.EDU (Graham Brand) writes: >I have written a batch file which accepts parameters from the command >line using the: > if %1==param1 >syntax. It works fine except that if I don't pass any parameters, I >get the above syntax error message. Is there any way to suppress this? try: if (%1)==(param1) or: if $%1==$param1 etc. >In addition, is there any way of passing a string, the first character >of which is a dash (-), as in -f? It doesn't seem to work with the >above test. Works just fine as above. -- _ Kevin D. Quitt demott!kdq kdq@demott.com DeMott Electronics Co. 14707 Keswick St. Van Nuys, CA 91405-1266 VOICE (818) 988-4975 FAX (818) 997-1190 MODEM (818) 997-4496 PEP last
ts@uwasa.fi (Timo Salmi) (12/06/90)
In article <9467@pasteur.Berkeley.EDU> brand@cad.Berkeley.EDU.UUCP (Graham Brand) writes: >I have written a batch file which accepts parameters from the command >line using the: > if %1==param1 >syntax. It works fine except that if I don't pass any parameters, I : Simple. Wrong format. Use if "%1"=="param1" ... If you want to learn batch programming take a look at some batch packages. I'll naturally recomend to my own, /pc/ts/tsbat22.arc available by anonymous ftp from uwasa.fi archives. Contains a wealth of batch lore and tricks. ................................................................... Prof. Timo Salmi (Moderating at anon. ftp site 128.214.12.3) School of Business Studies, University of Vaasa, SF-65101, Finland Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun
derek@sun4dts.dts.ine.philips.nl (derek) (12/06/90)
sab@engr.uark.edu (Steven A. Breuer) writes: >In article <9467@pasteur.Berkeley.EDU>, brand@cad.Berkeley.EDU (Graham Brand) writes: >> I have written a batch file which accepts parameters from the command >> line using the: >> if %1==param1 >> syntax. It works fine except that if I don't pass any parameters, I >> get the above syntax error message. Is there any way to suppress this? >> >> In addition, is there any way of passing a string, the first character >> of which is a dash (-), as in -f? It doesn't seem to work with the >> above test. >> > Batch files have trouble with blank parameters (none). try the > following "if" statements. It is a good idea to surround the parameters > with some character - this should take of errors happening because > of no parameters. This should also allow you to use the dash (-) as > part of a parameter. > if "%1"=="" goto NOPARMS > if "%1"=="param1" goto PARAM1 > Steve Breuer > sab@engr.uark.edu You can also use the following trick that I often use: if "%1"=="" goto NOPARMS goto %1 rem here handle incorrect parameters. The "goto %1" branches to the label passed as parameter 1. This is especially useful in a batch file calling itself. (Think about it!) Best Regards, Derek Carr DEREK@DTS.INE.PHILIPS.NL Philips I&E TQV-5 Eindhoven, The Netherlands Standard Disclaimers apply.
peterh@hemel.bull.co.uk (Peter Holditch) (12/07/90)
brand@cad.Berkeley.EDU (Graham Brand) writes: >I have written a batch file which accepts parameters from the command >line using the: > if %1==param1 >syntax. It works fine except that if I don't pass any parameters, I >get the above syntax error message. Is there any way to suppress this? >In addition, is there any way of passing a string, the first character >of which is a dash (-), as in -f? It doesn't seem to work with the >above test. >Cheers, >-Graham To supress the syntax error, use if "%1"=="param1" or if %1.==param1. as for passing strings starting with -, I have no problem on dos 3.20 Peter Holditch -- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # From: Peter.Holditch@hemel.bull.co.uk # # Tel: 0442-232222x4826 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
darcy@druid.uucp (D'Arcy J.M. Cain) (12/08/90)
In article <9467@pasteur.Berkeley.EDU> Graham Brand writes: > if %1==param1 >syntax. It works fine except that if I don't pass any parameters, I >get the above syntax error message. Is there any way to suppress this? The problem is that with no parameter it evaluates to if ==param1 which is invalid. To get around this simply do the test as follows: if %1x==param1x The extra character on both sides prevents the syntax error without affecting the comparison. >In addition, is there any way of passing a string, the first character >of which is a dash (-), as in -f? It doesn't seem to work with the >above test. Not sure what you mean. The following works for me: if %1==-f echo -f Or with the above change: if x%1==x-f echo -f Can you give an example of the problem. -- D'Arcy J.M. Cain (darcy@druid) | D'Arcy Cain Consulting | There's no government West Hill, Ontario, Canada | like no government! +1 416 281 6094 |
U3369429@ucsvc.ucs.unimelb.edu.au (Michael Bednarek) (12/10/90)
In article <1990Dec8.150147.1225@druid.uucp>, darcy@druid.uucp (D'Arcy J.M. Cain) writes: > In article <9467@pasteur.Berkeley.EDU> Graham Brand writes: >>In addition, is there any way of passing a string, the first character >>of which is a dash (-), as in -f? It doesn't seem to work with the >>above test. > Not sure what you mean. The following works for me: > if %1==-f echo -f BUT, why does it not work with "="? Example: File TEST.BAT: Echo %1 %2 Command line: TEST e=mc2 Result: e mc2 Who is eating the Equal Signs? And what can be done about it? -- Michael Bednarek, Big River Ski Lodge Caravan Park, Seelands Grafton Base Hospital, Grafton 2460, AUS, Phone: +61 66 44 9324 // u3369429@{ucsvc.dn.mu.oz.au | murdu.oz.au} | mb@munnari.oz.au \X/ "POST NO BILLS." PSI%23343000301::U3369429
silver@xrtll.uucp (Hi Ho Silver) (12/10/90)
In article <9467@pasteur.Berkeley.EDU> brand@cad.Berkeley.EDU.UUCP (Graham Brand) writes:
$I have written a batch file which accepts parameters from the command
$line using the:
$ if %1==param1
$syntax. It works fine except that if I don't pass any parameters, I
$get the above syntax error message. Is there any way to suppress this?
Yup. The reason, incidentally, that the above doesn't work is that it
expands to
if ==param1
which is incorrect. So all you need to do is add a constant character to
both sides of the expression; for example,
if _%1==_param1
(pick your favourite character, as long as it has no special meaning to
the shell).
--
__ __ _ | ...!nexus.yorku.edu!xrtll!silver | always
(__ | | | | |_ |_) >----------------------------------< searching
__) | |_ \/ |__ | \ | if you don't like my posts, type | for
_____________________/ find / -print|xargs cat|compress | SNTF
felton@eng3.UUCP (Ed Felton) (12/11/90)
Graham Brand writes: }I have written a batch file which accepts parameters from the command }line using the: } if %1==param1 }syntax. It works fine except that if I don't pass any parameters, I }get the above syntax error message. Is there any way to suppress this? ok, here's a simple trick. try: if .%1==.param1 The dot will be there no matter what, as it is put there by you. The error you are getting is when the command.com command line interpreter sees the following: if ==param1 which is so obviously an error that humans can see the problem. the fix suggested above by the way, works the same as the echo. trick to put out blank lines. By the way... Wouldn't it be nice to have a real set of tools for this, instead of DOS's broken stuff? Hey Microsoft! listen to your market! }In addition, is there any way of passing a string, the first character }of which is a dash (-), as in -f? It doesn't seem to work with the }above test.
userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) (12/11/90)
In article <635@sun4dts.dts.ine.philips.nl>, derek@sun4dts.dts.ine.philips.nl (derek) writes: >sab@engr.uark.edu (Steven A. Breuer) writes: > >>In article <9467@pasteur.Berkeley.EDU>, brand@cad.Berkeley.EDU (Graham Brand) writes: >>> I have written a batch file which accepts parameters from the command >>> line using the: >>> if %1==param1 >>> syntax. It works fine except that if I don't pass any parameters, I >>> get the above syntax error message. Is there any way to suppress this? <<<deletions>>> > >You can also use the following trick that I often use: > > if "%1"=="" goto NOPARMS > > goto %1 > > rem here handle incorrect parameters. > >The "goto %1" branches to the label passed as parameter 1. This is especially >useful in a batch file calling itself. (Think about it!) > Still redundant. Try: goto _%1 rem handle incorrect parameters :_ rem process the NOPARMS case here :_asm rem process an argument of 'asm', 'Asm', 'ASM' or etc. Using a variable as a label makes argument processing case insensitive, in addition to reducing the number of IF clauses used. In addition, prefixing label names with '_' adds two more benefits: 1) no need to handle the NOPARMS case separately, 2) makes it possible to go only to those labels designated for this purpose. I always get an error when no label is found to match the parameter, aborting the batch file before it can handle the incorrect parameters. This is acceptable if these label parameters are generated by the batch file itself, but if used to process (random) input from users you want to protect from this kind of thing, try: if exist "argcheck"_%1 goto _%1 echo invalid argument: "%1" -------------------+------------------------------------------- Al Dunbar | Edmonton, Alberta | "this mind left intentionally blank" CANADA | - Manuel Writer -------------------+-------------------------------------------