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 th
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:
Works like a charm for me :O)
Windows Registry Editor Version 5.00
;File:ConfigureShToBeRunUnderExplorer.reg v:1.0 docs at the end
[HKEY_CLASSES_ROOT\Applications\bash.exe]
[HKEY_CLASSES_ROOT\Applications\bash.exe\shell]
[HKEY_CLASSES_ROOT\Applications\bash.exe\shell\open]
[HKEY_CLASSES_ROOT\Applications\bash.exe\shell\open\command]
@="C:\\cygwin\\bin\\bash.exe -li \"%1\" %*"
; This is a simple registry file to automate the execution of sh via cygwin on windows 7, might work on other Windows versions ... not tested
; you could add this setting by issueing the following command: reg import ConfigureShToBeRunUnderExplorer.reg
; Note the path of your bash.exe
; Note that you still have to add the .sh to your %PATHTEXT%
; usage: double - click the file or reg import file
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
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
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.
This is the command I'm using:
"C:\cygwin\bin\mintty.exe" -w max -h always -t "%1" -e /bin/bash -li -c 'cd "$(dirname "$(cygpath -u "%1")")" && bash "$(cygpath -u "%1")"'
It runs it in mintty, maximised, sets the window title to the script being ran (Windows path to it), changes directory to where the script is, runs it and stays open after it completes.
Alternatively, this will set the title to the cygwin path to the script:
"C:\cygwin\bin\mintty.exe" -w max -h always -t "%1" -e /bin/bash -li -c 'printf "\033]0;$(cygpath -u "%1")\007" && cd "$(dirname "$(cygpath -u "%1")")" && bash "$(cygpath -u "%1")"'
Batch scripts to set the association for you:
Windows path in title:
@echo off
assoc .sh=shellscript
ftype shellscript="C:\cygwin\bin\mintty.exe" -w max -h always -t "%%1" -e /bin/bash -li -c 'cd "$(dirname "$(cygpath -u "%%1")")" ^&^& bash "$(cygpath -u "%%1")"'
pause
And cygwin path in title:
@echo off
assoc .sh=shellscript
ftype shellscript="C:\cygwin\bin\mintty.exe" -w max -h always -t "%%1" -e /bin/bash -li -c 'printf "\033]0;$(cygpath -u "%%1")\007" ^&^& cd "$(dirname "$(cygpath -u "%%1")")" ^&^& bash "$(cygpath -u "%%1")"'
pause