4

Is it possible to run a msys bash command in a batch script? Let say I have a.bat file in that I have my windows batch commands and in one line I will switch to msys bash to process msys specified commands. Like so:

[...] 
batch commands 
[...]
C:\mingw\msys\1.0\bin\sh -l
mount 'C:\mingw\local32' /local32
[...] 
bash commands 
[...]

Thanks for help!

jb_

3 Answers 3

5

Why don't you just put all your shell commands in a separate file and just invoke that script from within the batch file in one line ?

C:\mingw\msys\1.0\bin\sh your-msys-script.sh
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I try this before, but then I lost all paths from the msys environment. Maybe I found a solution with: C:\mingw\msys\1.0\bin\sh -l your-msys-script.sh Maybe it works so how I want.
0

I found now a better way: I use the mintty console, it works with other I think also. The mintty is under msys/1.0/bin, with a shortcut I point to mintty with the parameter: /bin/sh -l. Now I can send from a batch file parameter to that shortcut. For example: mintty.lnk script.sh

Comments

0

A .SH file is quiet similar to the batch file in the Windows OS and can be run in Windows if the Linux-based operating system of Windows has been activated. So, to run .SH file in Windows 10 using Windows Subsystem for Linux, if your Linux Subsystem is not yet installed do the following:

Go to Settings > Update & Security > For Developers. Check the Developer Mode radio button. And search for “Windows Features”, choose “Turn Windows features on or off”. Scroll to find WSL, check the box, and then install it. Once done, one has to reboot to finish installing the requested changes. Press Restart now. BASH will be available in the Command Prompt as well as your PowerShell.

Execute bash script files

Open Command Prompt and go to the folder that contains your .sh file. Type Bash script-filename.sh and hit the enter key. It will execute the script, and depending on the file, you should see an output. enter image description here

my bash file (state.sh) contained the following scripts: enter image description here

and as text:

echo "Current state:"
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")

if [ $LOCAL = $REMOTE ]; then
echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
echo "Need to pull"
elif [ $REMOTE = $BASE ]; then
echo "Need to push"
else
echo "Diverged"
fi

Run batch file containing bash script files

You can see in the following picture how can a bash file be started from a batch file:

@echo off
START  /wait ./state.sh
echo            . . . . . . . . . . . . . . . . . . . .
echo               'The program executed successfully!'
echo            . . . . . . . . . . . . . . . . . . . .

Now your batch file is ready for executing.

Hope this answer help those looking for a solution to the problem like this.

1 Comment

don't post text as images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.