How to execute Unix shell script from Windows?

Microsoft Backticks

  • How do I get the ouput of a windows console program / script into a batch file variable? Unix user here, trying to translate a shell script into a batch file that will run in a Windows command shell environment. I need to get the output of a program or script into a variable. In a Unix shell it is easy : var=`program`, but I have not been able to figure out how to do the same in a batch file.

  • Answer:

    You can probably do this in an XP batch file. Read the help for the "for" statement, for /?The example there is: FOR /F "usebackq delims==" %i IN (`set`) DO @echo %iWhich prints the environment variable names without their values. You can fool with the delimiters to get different token parsing. There is a whole bunch of useful information hidden in help strings from the various CMD.EXE builtins, like set /? if /? call /?You'll have to think differently than bash, but it's pretty powerful.

Deepspace at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

Windows batch files kind of suck, but I think you may be able to output the program to a file with the >> [filename] operator, then bring it back into the script with << [filename], or use that to self-write a batch file that is then called. It's not really an answer but is one area where I'd look.

rolypolyman

You might consider just shipping your script with a Windows version of bash. It'll make your life a lot easier.

grouse

Sad to say, but Windows batch language is not much enhanced from its roots in MS-DOS, when it was an operating system that fit on a 360 kb floppy. If you want to do anything substantial, you'll have to use an external http://msdn.microsoft.com/library/?url=/library/en-us/dnanchor/html/scriptinga.asp or command shell. The above suggestion about using a windows implementation of the BASH shell is a good one. If you don't want Microsoft to be dissapointed with you, you can use http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsoriWindowsScriptHost.asp.

curtm

from a command prompt "ping yahoo.com > c:\ping.txt" works. It does not work from the Run box. You want to specify the directory path to the save location. I haven't taken the time to figure out how Windows selects what directory is current, but it's certainly not always the one I would expect.

theora55

The trick used by many people is setting an environment variable, and then using that. There is just no other variable available to use in a batch file. Here's an example from a batch file I have on a Win2000 machine to get the date. ------------------------------------------------------------- @echo off SET %%PATH = "c:\winnt\system32\" For /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do ( For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do ( set dow=%%i set %%a=%%j set %%b=%%k set %%c=%%l set hh=%%m set min=%%n set ss=%%o ) ) SET YY=%YY:~2,2% REM Let's see the result. For %%i in (dow dd mm yy hh min ss) do set %%i

stovenator

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.