Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a cmd.exe batch file in a sub shell

Tags:

windows

cmd

I have a batch file that I usually invoke like this:

 longjob.cmd >result.txt 2>&1

This works fine, but the script changes directory during its execution leaving my shell in that directory - which is a nuisance.

Is there a way to run the command within a sub-shell - while still allowing the output to be captured ?

I have tried

cmd longjob.cmd >result.txt 2>&1

which just sits waiting for an exit command.

Also I tried

start longjob.cmd >result.txt 2>&1

which does run the script, but in a new window and all output is sent to that window instead of the file.

like image 604
Martin Avatar asked Nov 18 '10 13:11

Martin


People also ask

How do I run a batch file in shell?

Batch files can be run by typing "start FILENAME. bat". Alternately, type "wine cmd" to run the Windows-Console in the Linux terminal. When in the native Linux shell, the batch files can be executed by typing "wine cmd.exe /c FILENAME.

How do I run an exe from a batch file?

To start an exe file from a batch file in Windows, you can use the start command. For example, the following command would start Notepad in most versions of Windows. The start command can be used for other exe files by replacing the file path with the path to the exe file.

Is cmd.exe a shell?

Windows Command Prompt (also known as the command line, cmd.exe or simply cmd) is a command shell based on the MS-DOS operating system from the 1980s that enables a user to interact directly with the operating system.


1 Answers

Try

CMD /C longjob.cmd >result.txt 2>&1

Not sure how it'll deal with the redirection, but CMD /C lets you tell CMD what to run and that it should exit when done. (CMD /K lets you tell it to run something but stick around when done.) It will re-use the existing console window if run within one.

like image 83
Leo Davidson Avatar answered Oct 23 '22 04:10

Leo Davidson