X-Recipient: archive-cygwin@delorie.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:message-id:date:from:mime-version:to:subject
	:references:in-reply-to:content-type; q=dns; s=default; b=dC8ZGo
	TblJFmfrj0FFShJajXOttQqZi8NEbws7JV+rqLY/LC6dN54jiAijfZxieWh5ueCj
	o3xxiuMpn+dkK9IIUsIHWlG2tyLF72kVFQlI8nJlbuF1yeY5zsQ1iy9zjMV604gS
	4jzVSPU3abHjiJvHmz/+ZLNXjqbjmmiaww1G8=
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:message-id:date:from:mime-version:to:subject
	:references:in-reply-to:content-type; s=default; bh=uHbR+WSbgLwQ
	WoocnK8YaobJTk8=; b=k5X6DaB/fKrX69lhXbUZ7ZecG2ST6jk4bi7/9EXk7/SR
	FeC1PSDMoj64j/ITPpZ9RG2NGOTL97D73GiozI+dW+VzP25T2rgNdV8TvcSHSgXH
	AzzrFvndqnqg9FAGGRsZZZdWQLXfOtz+77nhejdiw8zmqchlXgjYme7ZnkB9oVk=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD autolearn=ham version=3.3.2
X-HELO: etr-usa.com
Message-ID: <52F2D9AF.1060409@etr-usa.com>
Date: Wed, 05 Feb 2014 17:39:11 -0700
From: Warren Young <warren@etr-usa.com>
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
MIME-Version: 1.0
To: Andrey Repin <cygwin@cygwin.com>
Subject: Re: Newbie Questions
References: <1898639722.6893470.1391541591920.JavaMail.root@ptd.net> <52F153AE.5080704@gmail.com> <52F28215.5030801@ptd.net> <52F28330.6060101@cygwin.com> <52F2AA5D.4000000@etr-usa.com> <52F2AD84.1050008@etr-usa.com> <709102296.20140206020738@mtu-net.ru>
In-Reply-To: <709102296.20140206020738@mtu-net.ru>
Content-Type: multipart/mixed; boundary="------------010002060407050007080109"
X-IsSubscribed: yes

--------------010002060407050007080109
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

On 2/5/2014 15:07, Andrey Repin wrote:
>
>> But if you associate .sh with bash.exe, then double-click that script
>> from Windows Explorer, it won't work right, since bash.exe will try to
>> run it as a shell script.
>
> Have you actually tried that?

Yep.

> Try it, you'll be surprised.

I did try it, before sending the previous message pair.

Save the attached file as foo.sh, then run it with "bash foo.sh", rather 
than "./foo.sh".  This is what happens when you associate *.sh with 
bash.exe in Windows Explorer.

Bash tries to interpret the file as a shell script, despite the shebang 
line.  This is because Bash doesn't do the shebang handling, exec() 
does, and Bash treats passed file names as names of shell scripts.  It 
runs them directly, not through exec().

When you say "bash foo.sh" on the attached file, it complains:

     foo.sh: line 2: use: command not found
     Unable to initialize device PRN

Then it sleeps for 2 seconds, since Bash and Perl both interpret line 4 
the same way, if you ignore the minor semantic difference ";\n" makes to 
Bash.

There is one small but critical difference in behavior when you do this 
by double-clicking foo.sh from Windows Explorer, with *.sh associated 
with Cygwin's bash.exe.  That is, it only pauses on line 4 if your 
Cygwin bin directory is in the Windows system PATH, since otherwise, 
bash.exe can't find sleep.exe.

Instead of adding Cygwin's bin directory to the Windows PATH, you can 
fix this with a minor registry hack.  Open

     HKCU\Software\Classes\sh_auto_file\shell\open\command

Change the default value to something like:

     "C:\cygwin32\bin\bash.exe" -l "%1"

Adjust c:\cygwin32 as needed.

This makes bash.exe a login shell, which forces it to run its startup 
scripts before running the script passed to it, so PATH gets set the 
same way it does for an interactive shell.

This doesn't make bash.exe send foo.sh to perl.exe, though.  To do that, 
you have to change the key's default value to:

     "C:\cygwin32\bin\bash.exe" -l -c 'exec `cygpath -u "%1"`'

A registry hack that involves triply-nested quotes seems like a long way 
to go to make Windows Explorer run files the same way Bash would 
interactively.

Plus, it only helps when you're attempting to deceive Bash.

My motto: Don't lie to computers; they find ways to get even.

--------------010002060407050007080109
Content-Type: application/x-shellscript;
 name="foo.sh"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="foo.sh"

IyEvdXNyL2Jpbi9wZXJsCnVzZSB3YXJuaW5nczsKcHJpbnQgIkhlbGxvIGZy
b20gUGVybCFcbiI7CnNsZWVwIDI7Cg==


--------------010002060407050007080109
Content-Type: text/plain; charset=us-ascii

--
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
--------------010002060407050007080109--
