lit
lit

Reputation: 16266

Running a .bat script under Cygwin bash

I would like to use an existing DOS/Windows .bat script under a Cygwin bash shell. The .bat script creates a number of variables which need to exist after the .bat script ends.

This works, but the variables are not retained.

$ ./.phs_project_setup.bat .

It appears that this does not extend to sourcing a .bat script so that the variables it creates still exist in the environment.

$ . ./.phs_project_setup.bat .
-bash: @ECHO: command not found
-bash: SET: command not found
-bash: $'\r': command not found
-bash: REM: command not found

Any ideas on overcoming this obstacle?

Upvotes: 0

Views: 2007

Answers (4)

lit
lit

Reputation: 16266

What I have done is written the environment to a file, then iterated over the file using 'cygpath -u' on each value. Perhaps I have missed some, but it appears that cygpath will only change something that actually looks like a path. It does not change Oracle connect string for example; "user/pass@DB". I added 'export ' to the beginning of each line so that it can be sourced into a bash shell. It is not one step yet, but better.

Upvotes: 1

AlG
AlG

Reputation: 15167

Sounds like you need to run the batch file and then start cygwin. If so, call the batch file from whatever file you use (cygwin.bat for example) to start cygwin. Then the variables should be available.

Alternatively, I've also moved the required bits into the proper unix configuration files to achieve the same results.

Upvotes: 0

Barak Itkin
Barak Itkin

Reputation: 4877

I would suggest trying to run the batch script using the batch interpreter (aka the COMSPEC environment variable, which is simply CMD) and then echoing out the environment it has set up as presented in this question: How I can use PowerShell with the Visual Studio Command Prompt?

You can then try and set up the environment in a similar fashion. Note that you may have a problem with the direction of slashes, drive names and other stuff like that

Upvotes: 0

Marc B
Marc B

Reputation: 360872

Remember that Unix systems are generally case sensitive. cygwin's bash can run windows executables directly, but it's STILL case senstive. SET is not a valid bash command, while set is.

You can force it to source the file and try and run it, but it'll only be able to run shell built-in commands which have a 1:1 name correspondence to cmd commands. So set works, but @echo won't, because @ means nothing to bash. Same goes for rem.

Upvotes: 0

Related Questions