Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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...

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Give this a try:

DOSKEY SLEEP==set /a t=$1+1 $G NULL $T PING 127.0.0.1 -n %t% $G NULL

The $T is a command separator equivalent to &.

Note that the > NUL redirects the output of DOSKEY rather than the macro. You can see that it's not included if you do DOSKEY /MACROS. To do the redirection that you want, append $G NUL to the end of the line as shown above.

Something like this will also work:

DOSKEY SLEEP=for /f %t in ('set /a $1+1') do @PING 127.0.0.1 -n %t $G NUL

Try both and see which one works better for you.

share|improve this answer
    
That's absolutely perfect. Wow, I didn't realise the Windows command line would be this bad. :D –  Matthieu Cartier Dec 23 '10 at 21:09
    
beautiful i must say –  bubu Dec 24 '10 at 1:50
    
+1. This is why real hackers love .bat files: There's nothing more hacky! –  phogg Dec 27 '10 at 19:17
  1. As far as I know cmd.exe does not offer readily available function for in-execution evaluation of function.

  2. If you just wanted to wait for 1 more second, just insert another ping -n 2 127.0.0.1 into it.

  3. You can consider installation of sleep command from the windows server administration tools.

  4. How about an installation of cygwin/powershell?

EDIT: correct my ping parameter stupidity

share|improve this answer
    
Not possible, this needs to be deployed to clients without cygwin/powershell and needs to be standalone as a batch file. Another ping 127.0.0.1 -n 1 as it executes and terminates nearly instantaneously. –  Matthieu Cartier Dec 23 '10 at 16:37
    
should have been another "ping -n 2 127.0.0.1" sorry my gross stupidity. should have taken the clue that ping -n [k] 127.0.0.1 will wait for k-1 seconds... –  bubu Dec 23 '10 at 16:38
    
Hm, I suppose that's a possibility, but not really an elegant one (not that this is going to be elegant anyway... Windows command line never is). –  Matthieu Cartier Dec 23 '10 at 19:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.