0

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?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
lit
  • 14,456
  • 10
  • 65
  • 119
  • 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. – Marc B Jul 18 '14 at 18:36

4 Answers4

1

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.

lit
  • 14,456
  • 10
  • 65
  • 119
0

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • The script is the same file in both cases. It appears that bash is willing to kick of cmd.exe to run the script in the first case. However, in the case of using 'source' or '.' bash does not kick off cmd.exe. – lit Jul 19 '14 at 01:43
  • 1
    bash never invokes *any* other program when a script is sourced; it is run in the *current* shell. – Ignacio Vazquez-Abrams Jul 19 '14 at 02:23
0

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

Community
  • 1
  • 1
Barak Itkin
  • 4,872
  • 1
  • 22
  • 29
  • Yes, many of the variables are file paths and are not automatically converted when bash is started. That is the half of the problem. The other half is getting them retained in the bash shell. – lit Jul 19 '14 at 01:45
0

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.

AlG
  • 14,697
  • 4
  • 41
  • 54