How can I associate .sh files with Cygwin?

前端 未结 13 1260
旧时难觅i
旧时难觅i 2021-01-29 21:32

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

相关标签:
13条回答
  • 2021-01-29 21:57

    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)

    0 讨论(0)
  • 2021-01-29 21:58
        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 
    
    0 讨论(0)
  • 2021-01-29 22:02

    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
    
    0 讨论(0)
  • 2021-01-29 22:03

    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

    0 讨论(0)
  • 2021-01-29 22:04

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题