delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2014/11/11/15:05:28

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:date:from:reply-to:message-id:to:subject
:in-reply-to:references:mime-version:content-type
:content-transfer-encoding; q=dns; s=default; b=D2lFUudVkg6iOmDM
GOAW9uLptFx7A7vwLChUqB4Gkm1sSo7DoZqFGzLEQMbEuG3Do/42BeDPDuzuxJNE
oYzd6vHxf23ykeeSNRjRdN+QYGddP/e3K8/SWyMlUuSSrD/JiqVPSQHR4lwfIIEg
rsLUG752iLxheIgAEuA7t/aDD8Q=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:date:from:reply-to:message-id:to:subject
:in-reply-to:references:mime-version:content-type
:content-transfer-encoding; s=default; bh=rdM3wBzvjxrWF+HE9Hp3Xm
KmVB0=; b=ErTbUoYnR3d9cHJTF0VthKD3kx1pCsM78MbXLoRGCTw7yst/QB6M1E
wioJxeeW/LKE7FT06Aq7kNs5EaYUexy5k7FHbyxMcyZWDQTGeY/wye0ZJPIPUgh9
dLqJ3Dpb26NcKGXhRP7GLDKj/vloqtN+IF5Qs/pah38GyOSLtwyaI=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-3.3 required=5.0 tests=AWL,BAYES_50,FREEMAIL_FROM,KAM_THEBAT,SPF_SOFTFAIL autolearn=no version=3.3.2
X-HELO: smtp.ht-systems.ru
Date: Tue, 11 Nov 2014 22:55:47 +0300
From: Andrey Repin <anrdaemon AT yandex DOT ru>
Reply-To: cygwin AT cygwin DOT com
Message-ID: <65830765.20141111225547@yandex.ru>
To: "Nellis, Kenneth" <Kenneth DOT Nellis AT xerox DOT com>, cygwin AT cygwin DOT com
Subject: Re: how to embed shell script within a .BAT file
In-Reply-To: <0D835E9B9CD07F40A48423F80D3B5A702E839DB4@USA7109MB022.na.xerox.net>
References: <87bnoforlc DOT fsf AT Rainer DOT invalid> <0D835E9B9CD07F40A48423F80D3B5A702E839DB4 AT USA7109MB022 DOT na DOT xerox DOT net>
MIME-Version: 1.0
X-IsSubscribed: yes

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 AT yandex DOT 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019