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.

Alright, so I was playing around with changing MTU size and wanted to make a batch file to automatically lower it and then raise it later.
This is probably simple, but I just can't figure it out. Point is, is there a way to run a command, which would normally echo out "ok" but check to see if it does say ok? And if it doesn't say ok then, to end the rest of the file from running and exit out.
The command I'm using is netsh interface ipv4 set subinterface "Local Area Connection" mtu=386 store=persistent which, as I mentioned above prints out an OK. I just want to check if it did run correctly, and if not, then do __

share|improve this question
add comment

2 Answers

If netsh is successful it will return an errorlevel of 0 (as do most command-line utilities), essentially meaning "No error".

So you should be able to use the command-line if and errorlevel to accomplish the check in a batch file. Something like:

@echo off
netsh interface ipv4 set subinterface "Local Area Connection" mtu=386 store=persistent
if errorlevel 0 goto END

:ERRORHANDLER
echo Error Detected - do stuff to fix it.
goto END

:END
echo Done!
share|improve this answer
add comment

The simple answer is to pipe the output through 'grep' and test the output of 'grep':

netsh interface ipv4 set subinterface "Local Area Connection" mtu=386 store=persistent | grep OK || exit 1

Typically if there is an error, a program would also return a non-zero exit code (the same as 'grep' will do if it doesn't find the text you are searching for in the case above). I don't know if netsh will do that, but you can probably test by feeding it some illegal values (like 1000000000000). If it does, you can do something slightly simpler:

netsh interface ipv4 set subinterface "Local Area Connection" mtu=386 store=persistent || exit 1

share|improve this answer
    
Using grep, can you exit if the text is not found? –  Manuel Jun 12 '12 at 4:03
add comment

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.