How can I associate .sh files with Cygwin?

前端 未结 13 1265
旧时难觅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 22:22

    I developed a .bat script on my own (not originated from other's answer) to associate a file type (e.g. *.cygwin) to open with this .bat, as follows:

    === file run-script-with-Cygwin-in-same-dir.bat ===

    @echo off
    REM   Info: A script created by Johnny Wong.  (last modified on 2014-7-15)
    REM   It is used to pass a file argument to run a bash script file.  The current directory is setting to the path of the script file for convenience.
    REM   Could be copied to C:\cygwin;  and then you manually associate .cygwin file extension to open with this .bat file.
    set CYGWIN=nodosfilewarning
    
    C:\cygwin\bin\bash --login -i -c 'cd "`dirname "%~1"`"; exec bash "%~1" %2 %3 %4 %5 %6 %7 %8 %9'
    
    REM finally pause the script (press any key to continue) to keep the window to see result
    pause
    

    === file run-script-with-Cygwin-in-same-dir.bat ===

    Detail explanations on syntax used (if you are interested) :

    1. %1 is "..." quoted if associated a file to open with this .bat. For dragging a file to this .bat, it is "..." quoted only if the file's path has spaces.
    2. %~1 is same as %1 with surrounding double-quotes eliminated, if they exist
    3. to remove surrounding double-quotes from %p%, use for %%a in (%p%) do set p=%%~a
    4. you must use "%~1" to force the script file's path double-quoted, so that its folder separators '\' (in %1) won't be removed by bash when being treated as escape characters. Otherwise, it does not work when dragging a file, which has no spaces in its path, to this .bat.
    5. "exec bash" can be just "bash", the former is for saving resources for one more bash process.

    Enjoys :)

    0 讨论(0)
提交回复
热议问题