How can I associate .sh files with Cygwin?

Viewed 33631

I'd like to run a long rsync command in Cygwin by double clicking on a .sh file in Windows. It must start in the file's containing directory (e.g. /cygdrive/c/scripts/) so that relative paths work. Anyone gotten this to work?

Note: I've just found here, a Cygwin package that manages Windows context menus (Bash Prompt Here). It might have some clues.

13 Answers

Ok, I've found something that works. Associating a batch file as Vladimir suggested didn't work, but the bash arguments were key.

Short and sweet: associate with this command: "C:\cygwin\bin\bash.exe" -li "%1" %*

Long version if you don't know how:

  1. In Explorer, go to Tools/Folder Options/File Types.
  2. I already had an SH entry for Bash Script. If you don't have one, click New and enter "SH" to create one.
  3. With the SH extension selected, click Advanced.
  4. Choose the "open" action and click edit (or create the action).
  5. This is the command to use: "C:\cygwin\bin\bash.exe" -li "%1" %*. Note that without the -li, it was returing "command not found" on my scripts.

You may also want to add SH to your PATHEXT environment variable:

WinKey+Pause / Advanced / Environment Variables / System Variables / PATHEXT

Thanks for your help, guys!

Here is my solution. It works well for my *.sh scripts regardless of where they are in the directory hierarchy. Notice that I cd to the cygpath dirname before calling bash on the cygpath. It just works.

assoc .sh=bashscript

ftype bashscript=C:\cygwin\bin\bash.exe --login -i -c 'cd "$(dirname "$(cygpath -u "%1")")"; bash "$(cygpath -u "%1")"'

This doesn't associate .sh files, but it might get you what you want. I started with the cygwin.bat batch file that launches the Cygwin bash shell, and modified it like so:

$ cat test.bat
@echo off

set MYDIR=C:\scripts

C:\cygwin\bin\bash --login -c "cd $MYDIR && echo 'Now in' `pwd`; sleep 15"

That's a toy script but you could modify it to call rsync or call a separate shell script. I admit that it would be nicer if it didn't have MYDIR hard coded. There's probaby a way do get it to automagically set that.

Oh yeah, when I created the .bat file in a bash shell in Cygwin, I noticed I had to actually "chmod +x test.bat" before I could launch it with a double-click. I think it's setting NTFS permissions. You wouldn't need to do that if you just used notepad.

After looking around different places. What I have managed to come up with is, first select C:\cygwin64\bin\mintty.exe from the windows "Open with..." dialog Then edit the registry value of

[Computer\HKEY_CLASSES_ROOT\Applications\mintty.exe\shell\open\command]

to,

C:\cygwin64\bin\mintty.exe -t "%1" /bin/bash -l -i -c "v1=\"$(cygpath -u \"%0\" -a)\" && v2=\"$(dirname \"$v1\")\" && cd \"$v2\" ; exec bash  \"%1\" %*"  

You should be able to associate .sh files with \CYGWIN\usr\bin\bash.exe. The script will have to change its own working directory, I suggest sticking something like this at the top:

cd `dirname "$0"`

I use PuttyCyg (awesome putty in Cygwin window) here's how to get it all going:

Create a batch script, eg. on my machine I used

C:\Dev\scripts\cygbashrun.bat

with contents

SET CYGWIN=nodosfilewarning
C:\Cygwin\bin\putty.exe -cygterm /bin/bash.exe %1

Obviously adapt to contain the paths of your install of PuttyCyg.

Then in Windows File Explorer go to Tools - Folder Options - File Types

Create a ".sh" entry if there isn't already (or .bash depending on what you like your scripts to have).. then Advanced..

[optional step] change the icon and select the Cygwin icon from your install

Then:

  1. New..
  2. Action = Run Bashscript..
  3. Application used to perform this action = C:\Dev\scripts\cygbashrun.bat "%1"

Works like a charm for me :O)

One solution that works is to create a .bat file that will open cygwin and execute your script.

The script to execute the script go.sh located on my home directory:

@echo off

C:
chdir C:\cygwin\bin

bash --login -i ./go.sh

Look at the assoc and ftype commands in a dos box. Here's an example for .jpg on my machine

c:\>assoc .jpg
.jpg=jpegfile

c:\>ftype jpegfile
jpegfile="C:\Program Files\Common Files\Microsoft Shared\PhotoEd\PHOTOED.EXE" "%1"

assoc .sh=bashscript

ftype bashscript="c:\cygwin\bin\bash.exe" "%1"

Make sure you change the path to bash in the ftype command to match where you have cygwin installed

I just didn't bother. I associated .sh files with Crimson Editor (since I spend as much time working out the bugs as I do actually running them). Now it's a matter of getting the right "open with/edit with" combination to work in File Types>Advanced. If I knew what DDE code Crimson Editor used, that would make things easier; as of this post I've not been able to find it, however.

This reminds me of my Mac days (1993-2008) when I used to try and scan applications for more than rudimentary AppleScript scriptability.

BZT

Related