Reputation: 41
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_
Upvotes: 4
Views: 24727
Reputation: 1775
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.
my bash file (state.sh) contained the following scripts:
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.
Upvotes: 0
Reputation: 41
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
Upvotes: 0
Reputation: 38205
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
Upvotes: 5