如何将.sh文件与Cygwin关联?

我想通过双击Windows中的.sh文件在Cygwin中运行一个长的rsync命令。 它必须从文件的包含目录(例如/ cygdrive / c / scripts /)开始,以便相对path起作用。 任何人得到这个工作?

注意:我刚刚find了chere ,一个pipe理Windows上下文菜单的Cygwin包(Bash Prompt Here)。 它可能有一些线索。

好的,我发现了一些可行的方法。 将一个batch file作为弗拉基米尔build议的build议不起作用,但bash的参数是关键。

短而甜:与此命令关联: “C:\ cygwin \ bin \ bash.exe”-li“%1”%*

长版,如果你不知道如何:

  1. 在资源pipe理器中,转到工具/文件夹选项/文件types。
  2. 我已经有了Bash脚本的SH条目。 如果你没有,点击新build,然后input“SH”创build一个。
  3. selectSH扩展名后,单击高级。
  4. select“打开”操作,然后单击编辑(或创build操作)。
  5. 这是要使用的命令: “C:\ cygwin \ bin \ bash.exe”-li“%1”%* 。 请注意,如果没有-li ,那么在我的脚本中反复的“找不到命令”。

您可能还想将SH添加到PATHEXT环境variables中:

WinKey +暂停/高级/环境variables/系统variables/ PATHEXT

感谢您的帮助,伙计!

这是我的解决scheme。 无论在目录层次结构中的什么位置,它都适用于我的* .sh脚本。 注意,在cygpath中调用bash之前,我需要cd到cygpath的dirname。 它只是工作。

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

我一直在使用Dragos的解决scheme一段时间,我认为它是最好的,因为它意味着需要在shell脚本中使用“cygpath -u”。

然后我想拥有诸如.sh和.bash文件的拖放支持等附加function。 经过一番挖掘,我写了一个.bat,将.sh和.bash文件关联为“bashscript”,并为它们激活Windows资源pipe理器的拖放处理程序。 我不得不编辑Dragos的命令,使其处理1个参数(在脚本上放置该项目的path)。

.bat文件大致如下:

 echo Registering .sh and .bash files as "bashscript"... assoc .sh=bashscript assoc .bash=bashscript echo. echo Setting the run command for the file type "bashscript"... ftype bashscript=C:\cygwin\bin\bash.exe --login -i -c 'cd "$(dirname "$(cygpath -u "%%1")")"; bash "$(cygpath -u "%%1")" "$(/argshandler.sh "%%2")"' echo. echo Activating the drag^&drop capability for "bashscript" files (only 1 dropped item echo will be passed to the script, multiple items are not supported yet)... reg add HKEY_CLASSES_ROOT\bashscript\shellex\DropHandler /v "" /t REG_SZ /d "{60254CA5-953B-11CF-8C96-00AA00B8708C}" /f 

Cygwin根目录下的“argshandler.sh”脚本只是cygpath返回它接收到的第一个参数,如果没有任何东西(例如,如果你只是双击一个脚本文件):

 #!/bin/bash if [ ! "$1" == "" ] then cygpath -u "$1" fi 

所有这些工作到目前为止很好。 但是,仍然有一些缺点可以解决:

  • 当涉及位于UNCpath上的脚本时,Dragos的命令及其衍生失败,例如\\ myserver \ myshare \ scriptfile.sh
  • 只有一个被丢弃的项目会被传递给shell脚本。

不知何故,关于1-drop-item-only的问题,改变参数处理程序脚本来回馈类似的东西

 "cygpathed-arg1" "cygpathed-arg2" "cygpathed-arg3" 

并把Dragos的命令改为类似的东西

 ...; bash "$(cygpath -u "%%1")" $(/argshandler.sh "%%2" "%%3" ... "%%9")' 

(注意argshandler.sh部分的“”消失了)似乎不能正常工作:如果拖到脚本上的某些项目在其path中包含一个空白,则所述path将在空白处被分成多个参数即使他们每个都用双引号括起来…怪异。

有没有任何命令行的专业人士可以解决这两个问题?

这不会关联.sh文件,但它可能会得到你想要的。 我开始使用启动Cygwin bash shell的cygwin.batbatch file,然后像这样修改它:

 $ cat test.bat @echo off set MYDIR=C:\scripts C:\cygwin\bin\bash --login -c "cd $MYDIR && echo 'Now in' `pwd`; sleep 15" 

这是一个玩具脚本,但你可以修改它来调用rsync或调用一个单独的shell脚本。 我承认如果没有MYDIR的硬编码会更好。 有一种方法可以让它自动设置。

哦,是的,当我在Cygwin的bash shell中创build.bat文件时,我注意到我必须实际上“chmod + x test.bat”,才能通过双击启动它。 我认为这是设置NTFS权限。 如果你只是使用记事本,你不需要这样做。

您应该能够将.sh文件与\ CYGWIN \ usr \ bin \ bash.exe相关联。 该脚本将不得不改变自己的工作目录,我build议在顶部粘贴这样的东西:

 cd `dirname "$0"` 

这是我正在使用的命令:

 "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")"' 

它运行在mintty,最大化,设置窗口标题为正在运行的脚本(Windowspath),更改目录到脚本的位置,运行它,并保持打开完成后。

或者,这会将标题设置为脚本的cygwinpath:

 "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")"' 

批处理脚本为您设置关联:

标题中的Windowspath:

 @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 

和标题中的cygwinpath:

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

一种解决方法是创build一个打开cygwin并执行脚本的.bat文件。

执行位于我的主目录下的脚本go.sh的脚本:

 @echo off C: chdir C:\cygwin\bin bash --login -i ./go.sh 

我只是没有打扰。 我使用Crimson Editor将.sh文件关联起来(因为我花了很多时间来处理错误,就像我实际运行它们一样)。 现在就是在“文件types”>“高级”中使用右键“打开并/编辑”的组合。 如果我知道Crimson Editor使用的是什么DDE代码,那会让事情变得简单; 作为这篇文章,我还没有find它,但是。

这让我想起我的Mac时代(1993-2008),当时我曾经尝试扫描应用程序以获取AppleScript脚本的基本function。

BZT

我使用PuttyCyg(在Cygwin窗口中的真棒腻子)以下是如何完成这一切:

创build一个批处理脚本,例如。 在我使用的机器上

 C:\Dev\scripts\cygbashrun.bat 

与内容

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

显然适应包含你的安装PuttyCyg的path。

然后在Windows文件资源pipe理器中转到工具 – 文件夹选项 – 文件types

如果还没有(或.bash取决于你喜欢你的脚本),那么创build一个“.sh”。

[可选步骤]更改图标并从安装中selectCygwin图标

然后:

  1. 新..
  2. 行动=运行Bashscript ..
  3. 用于执行此操作的应用程序= C:\ Dev \ scripts \ cygbashrun.bat“%1”

对我来说就像一个魅力:O)

我自己开发了一个.bat脚本(而不是源自其他人的答案),以便使用.bat打开文件types(例如* .cygwin),如下所示:

===文件运行脚本与Cygwin在同一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 

===文件运行脚本与Cygwin在同一dir.bat ===

所用语法的详细说明(如果您有兴趣):

  1. %1是“…”引用,如果关联文件打开这个.bat。 为了将文件拖到这个.bat文件中,只有当文件的path有空格时才会引用“…”。
  2. %〜1与%1一样,如果存在周围的双引号,则消除
  3. 从%p%中删除周围的双引号, for %%a in (%p%) do set p=%%~a
  4. 你必须使用"%~1"来强制脚本文件的path为双引号,这样它的文件夹分隔符'\'(在%1中)在被视为转义字符时不会被bash删除。 否则,在将其path中没有空格的文件拖动到该.bat文件时不起作用。
  5. “exec bash”可以只是“bash”,前者是为了节省资源的一个bash过程。

喜欢:)

看一下dos框中的assoc和ftype命令。 以下是我的机器上的.jpg示例

 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" 

请确保在ftype命令中将path更改为bash,以匹配安装了cygwin地方