Doing arithmetic and passing it to the next command

Posted by neurolysis on Super User See other posts from Super User or by neurolysis
Published on 2010-12-23T16:21:39Z Indexed on 2010/12/23 16:55 UTC
Read the original article Hit count: 275

Filed under:
|
|

I know how to do this in /bin/sh, but I'm struggling a bit in Windows.

I know you can do arithmetic on 32-bit signed integers with

SET /a 2+2
4

But how do I pass this to the next command? For example, the process I want to perform is as follows.

Consumer editions of Windows have no native automated sleep function (I believe?) -- the best way to perform a sleep is to use PING in association with the -n switch to get that many seconds, minus one, of sleep. The following command is effective for a silent sleep:

PING localhost -n 3 > NUL

But I want to alias this into a sleep command. I'd like to have it elegant so that you enter the actual number of seconds you want to sleep after the command, right now I can do

DOSKEY SLEEP=PING 127.0.0.1 -n $1 > NUL

Which works, but it's always 1 second less than your input, so if you wanted to sleep for one second you would have to use the command SLEEP 2. That's not exactly ideal.

Is there some way for me to pass the arithmetic of $1+1 and pass it on to the next command in Windows? I assume there is some way of using STDOUT...

© Super User or respective owner

Related posts about Windows

Related posts about cmd