4

I have a bash script postCloneSetup.sh:

script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$script_path"

printf 'Updating submodules for project\r\n'
git submodule update --init --recursive

printf '\r\nInitializing git hooks\n'
./GitHooks-BackEnd/init-hooks

printf '\r\nInitializing Submodule1\r\n'
$(./Submodule1/postCloneSetup.sh)

printf '\r\nInitializing Submodule2\r\n'
$(./Submodule2/postCloneSetup.sh)

I trigger a bash script from Windows command prompt.

postCloneSetup.sh

It opens another window and then returns. The window it spawned stays open and logs output text.

enter image description here

I want to capture the output from the spawned window (the text written to the console) and return that to the Windows command prompt.

I would prefer to use something like

$(postCloneSetup.sh) // Linux for capturing output to current context

for the Windows command prompt.

I'd prefer not to modify postCloneSetup.sh. I know I could have it write out to a file with

exec &> postCloneSetupLog.log

but then I must wait and manually run

type postCloneSetupLog.log

to see the output in the console. This is not possible for integrating into a CI engine, which is my goal.

How can I capture the output from the spawned console in one command?

  • 1
    Please edit the question and include the contents of both scripts. – DavidPostill Jun 13 '17 at 19:37
  • @DavidPostill I added the contents of postCloneSetup.sh, but the other "script" is just running it from the Windows command prompt. In the CI engine that is what is happening. – Scotty H Jun 13 '17 at 19:46
  • The small window seem to be of 'git for windows' program. See command redirection:: en.wikipedia.org/wiki/Redirection_(computing) – Biswapriyo Jun 13 '17 at 20:00
  • Please explain exactly what you mean by "I want to capture the output from the spawned window and return that to the Windows command prompt." Do not respond in comments; edit your question to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Jun 16 '17 at 20:32
2

Short answer:

To use Git Bash, and execute

  • "%ProgramFiles%\Git\bin\bash.exe" script_name.sh to run a script
  • "%ProgramFiles%\Git\bin\bash.exe" -c "echo 'it works'" to run one-liner

A context:

I came across this question when looking for a solution for something similar. In our set up we use Git Bash on Windows (specifically at the moment "GNU bash, version 4.4.19(1)-release (x86_64-pc-msys)") as a part of development environment for Windows, we have some scripts that we want to test via GitLab CI exactly in this environment: to execute them under Git Bash, and see the output in CI job.

What worked for us is to execute "%ProgramFiles%\Git\bin\bash.exe" script_name.sh where "script_name.sh" is a script to be executed by bash (or if you need to execute just inline command "%ProgramFiles%\Git\bin\bash.exe" -c "echo 'it works'"). An output of this script is visible in CMD window, and is captured by CI job.

May be a similar result can be achieved with Windows Subsystem for Linux, but as Git Bash was our requirement, we didn't investigate how it could be done with WSL.

So, a solution/workaround that I would propose from my experience is to install "Git For Windows" https://gitforwindows.org/ (Git Bash is a part of it), and run bash scripts via Git Bash as described above.

Topic Starter mentioned that running a script from CI was exactly his goal (quote: but the other "script" is just running it from the Windows command prompt. In the CI engine that is what is happening, link), and that was our use case as well, so I'm adding an example of CI job configuration (GitLab CI in our case, ".gitlab-ci.yml"). It is important to get all escaping correct to make it work, and it took us a couple of errors to do it right:

test_on_windows:
  stage: test
  script:
    - "\"%ProgramFiles%/Git/bin/bash.exe\" script_name.sh"
    # or
    - "\"%ProgramFiles%/Git/bin/bash.exe\" -c \"echo 'it works'\""
  tags:
    - windows
0

It seems that your question has already been answered. https://stackoverflow.com/questions/2323292/windows-batch-assign-output-of-a-program-to-a-variable

I think you want that:

for /f %%i in ('application arg0 arg1') do set VAR=%%i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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