Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

How to execute a bat file from cygwin bash that uses the Windows find command

The find command does completely different in Windows and Unix. On Windows it is a fgrep-like utility listing matching lines in a file; on Unix -- and on Cygwin -- it list filenames matching some criteria.

Cygwin bash prepends its standard directories to the current path, so inside bash $PATH is typically /bin:/usr/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS.

Begin Update to provide additional details

For example I have many scripts that use the gnu find command -- for example scripts to purge directory trees that contain no files:

purge-empty-dirs.sh
find . -depth -type d -empty | xargs rmdir -p

I also have a bat file to start my build, which uses the windows find command, which searches for lines matching a string (similar to gnu grep).

build.bat
...
dir | find "target"
if errorlevel = 1 goto no_target_dir
...

Now, for my bash script to work I need /bin to be in path before c:windowssystem32. But for my bat file to run I need c:windowssystem32 to be in path before /bin

In general we might be able to claim that all bat files should be executed with the original environment inherited by bash, not the modified one. Does that make sense?

End Update

This how it should be, but breaks the bat files executed from bash. What is the best way to address this?

Is there a way to force Cygwin to execute bat files (or even all Windows executables) with the environment it started with? I am thinking of start /i behavior of cmd.exe. I am thinking of writing my own cygstart like utility that does this, by saving the environment (or at least $PATH) in .bash_profile/.bashrc. Does that make sense?

Any other suggestions?

Edit: See also Start new cmd.exe and NOT inherit environment

question from:https://stackoverflow.com/questions/17582778/how-to-execute-a-bat-file-from-cygwin-bash-that-uses-the-windows-find-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you chmod +x your.bat it works.

./your.bat

is run via cmd /c

Already answered in Why is it that Cygwin can run .bat scripts?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...