This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Newbie Questions


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.

Attachment: foo.sh
Description: application/shellscript

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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]