views:

71

answers:

4

I am just beginning to learn command line scripts, and I have an assignment for school in which the first part is to create a batch file that accepts two integers as parameters. The integers will be subsequently manipulated throughout the question, and I am not looking for any help with that. I have googled this many different ways, and cannot seem to come up with an answer. How do I begin this?

I know this is very basic to probably everyone that reads this, but I am asking you to cut me some slack, we all have to start somewhere.

+3  A: 

On windows, it is %1 %2

http://commandwindows.com/batch.htm

example:

@echo off

echo %1 %2

set /a v = %1
set /a v2 = %1 + 1
set /a v3 = %1 * 2

echo %v% %v2% %v3%
動靜能量
Yes, very sorry, it is Windows 7 that we are doing this in. I guess my confusion comes from the sample we were given in our lecture, in that a variable was assigned within the script, and we named the file 'param.bat'. We then ran the file as param.bat4 (or whatever number), and the 4 was assigned as the variable. I couldn't figure out how the hell I was supposed to get two integers in that way. Just confused, I guess.
mrwienerdog
So to get the user to assign the value of the paramater, I would just
mrwienerdog
Damn it, I keep pressing return, so I have a few posts, sorry. How would I get the user to input the parameter?
mrwienerdog
+3  A: 

Assuming this is Windows, you can reference the parameters in your script as %1 and %2.

Justin Ethier
Wow, am I impressed. In other instances when I have searched the internet for tech help (in reference to school related material), some of the responders were very rude when answering questions (essentially calling the poster stupid). You guys are great! Thank you so much for your help, now I can finish off my question without any problem.
mrwienerdog
You're welcome, glad to help!
Justin Ethier
+3  A: 

Assuming MS-DOS, you can use %1, %2, etc. for input parameters.

@ECHO OFF

SET /a INT1=%1
SET /a INT2=%2

SET /a ANSWER=INT1*INT2

ECHO %ANSWER%

PAUSE

You could then call this as:

mybatchfile.bat 2 4
LittleBobbyTables
A: 

If the requirement of your assignment doesn't specifically say you definitely must use batch (cmd.exe), you can use vbscript. It has a tad better error handling, and can do floating point maths etc, better than what cmd.exe can offer.

On Error Resume Next
num1= CInt(WScript.Arguments(0))
num2= CInt(WScript.Arguments(1))
total=num1 + num2
If Err.Number <> 0 Then
    WScript.Echo "Description: " &  Err.Description
    Err.Clear
Else
    WScript.Echo "Added: " & total
End If

example

C:\test>cscript //nologo assigment.vbs 1 2
Added: 3

C:\test>cscript //nologo assignment.vbs 1 test
Description: Type mismatch
ghostdog74
It does specify batch. Although, what you have written looks really cool, and I'm looking forward to learning that!
mrwienerdog