This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: how to embed shell script within a .BAT file
- From: Andrey Repin <anrdaemon at yandex dot ru>
- To: "Nellis, Kenneth" <Kenneth dot Nellis at xerox dot com>, cygwin at cygwin dot com
- Date: Tue, 11 Nov 2014 22:55:47 +0300
- Subject: Re: how to embed shell script within a .BAT file
- Authentication-results: sourceware.org; auth=none
- References: <87bnoforlc dot fsf at Rainer dot invalid> <0D835E9B9CD07F40A48423F80D3B5A702E839DB4 at USA7109MB022 dot na dot xerox dot net>
- Reply-to: cygwin at cygwin dot com
Greetings, Nellis, Kenneth!
>> From: Achim Gratz
>>
>> Nellis, Kenneth writes:
>> > Jeremy's solution is closest to what I was looking for; however I need
>> > it to work from a networked, non-drive-mapped folder.
>> > (CMD.EXE doesn't like UNC paths.) I hadn't realized that I could pipe
>> > a script into bash.
>>
>> The solution to the UNC path problem is to put something
>>
>> PUSHD %~dp0
>>
>> near the beginning of the script. That is if you really want to have .bat
>> files and click on them in Explorer. Associating the shell scripts
>> properly so that Explorer will hand them to Cygwin sounds like a better
>> solution to me.
> BTW, works great! Here's the completed .BAT file preface with added error
> handling so the CMD window doesn't auto-close if the bash code exits with
> an error.
> @echo off
> set PATH=C:\cygwin64\bin;%PATH%
> PUSHD %~dp0
PUSHD "%~dp0"
But be aware, that you will lose the ability to run such script from different
CWD.
> type %0 | sed "0,/^---BASH SCRIPT FOLLOWS---/ d; s/\r*$//" | bash
sed "0,/^---BASH SCRIPT FOLLOWS---/ d; s/\r*$//" < "%~f0" | bash
> if errorlevel 1 pause
> exit
That's nice for desktop application.
But
EXIT %ERRORLEVEL%
would be more useful in general.
And keep in mind: not everything can be run that way, unfortunately.
Only interpreters accepting command stream from STDIN (such as bash).
And you will lose the ability to pipe data to such script yourself.
> ---BASH SCRIPT FOLLOWS---
> ...
> I'll pursue associating a shell script with a file type
> when I have more time.
On a slightly unrelated note, please avoid naming your scripts ".bat", when
you are using extended CMD features. Not every .bat file interpreter supports
a full range of bugs in CMD.
--
WBR,
Andrey Repin (anrdaemon@yandex.ru) 11.11.2014, <22:35>
Sorry for my terrible english...
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple