Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Script for command calculate

2 views
Skip to first unread message

Marco Shaw

unread,
Oct 4, 2006, 10:52:46 AM10/4/06
to
> i'm trying to write a simple script for the console command that calculate
> two numbers,the script need to ask the user what mathematical option do
you
> want that follow by numbers for Example: press 1 on keyboard for +,press 2
> for -,press 3 for /,press 4 for *, and then do the math, how i do it?

You'll have to rethink your request. As far as I know, there is no way
VBScript or any other simple scripting can do such key mapping on Windows.

I don't know if the beta PowerShell has this functionality...

Marco


dreeschkind

unread,
Oct 4, 2006, 4:12:01 PM10/4/06
to
"Marco Shaw" wrote:

PowerShell supports math at the command line:
PS> 1+2
3
However, if you want to input the operator first and then do the math, you
have to write it as a script:

### do-math.ps1 ###
"Enter operator: press 1 for +, press 2 for -, press 3 for /, press 4 for *"
$op = ""
while ($op -eq "") {
$key =
$host.UI.rawui.ReadKey([System.Management.Automation.Host.ReadKeyOptions]"NoEcho,IncludeKeyDown,IncludeKeyUp")
if ($key.KeyDown) {
switch ($key.Character) {
([char]"1") {
$op = "+"
}
([char]"2") {
$op = "-"
}
([char]"3") {
$op = "/"
}
([char]"4") {
$op = "*"
}
([char]"q") {
return $true
}
}
}
}

#$input = read-host operator
#switch ($input) {
# 1 {$op = "+"}
# 2 {$op = "-"}
# 3 {$op = "/"}
# 4 {$op = "*"}
# }

$v1 = read-host value1
$v2 = read-host value2
invoke-expression "$v1 $op $v2"
### EOF ###

Sample PoSh session:

PS> .\do-math.ps1
Enter operator: press 1 for +, press 2 for -, press 3 for /, press 4 for *
value1: 5
value2: 5
25

More information on PowerShell RC2 and download link at:
http://blogs.msdn.com/powershell/archive/2006/09/26/Windows-PowerShell-RC2-Now-Available.aspx

PowerShell has a dedicated newsgroup. You can access it directly via
news://microsoft.public.windows.powershell from the server: news.microsoft.com
Or just use Microsoft's Discussion Groups website at:
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?&guid=&sloc=en-us&dg=microsoft.public.windows.powershell

There's also an interesting IRC channel #powershell on freenode.

--
greetings
dreeschkind

Bill Stewart

unread,
Oct 9, 2006, 12:56:27 PM10/9/06
to
Avi G wrote:

> i'm trying to write a simple script for the console command that calculate
> two numbers,the script need to ask the user what mathematical option do you
> want that follow by numbers for Example: press 1 on keyboard for +,press 2
> for -,press 3 for /,press 4 for *, and then do the math, how i do it?

You can have a VBScript or JScript script read in all its command-line
arguments, concatenate them to a string, pass the string to the Eval
function (VBScript) or eval method (JScript), and output the results.

Here's a JScript example:

var args, arg, expression, result;

args = WScript.Arguments;

if (args.length == 0) {
WScript.Echo("Specify an expression on the command line");
WScript.Quit(0);
}

expression = "";

args = new Enumerator(args);
for (; ! args.atEnd(); args.moveNext()) {
arg = args.item();
expression += expression == "" ? arg : " " + arg;
}

try {
result = eval(expression);
WScript.Echo(result);
}
catch(err) {
WScript.Echo("Can't evaulate that expression");
}

--
Bill Stewart

0 new messages