我想运行长可以命令在Cygwin通过双击。sh文件。它必须开始在文件中包含的目录(例如/cygdrive/c/scripts/),以便相对路径的工作。任何人得到这工作吗?

注:我刚刚找到 在这里,, 一Cygwin包,管理Windows上下文的菜单(Bash提示这里)。它可能有一些线索。

有帮助吗?

解决方案

好的,我找到了有用的东西。将批处理文件关联为Vladimir建议不起作用,但bash参数是关键。

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

长版如果你不知道如何:

  1. 在资源管理器中,转到工具/文件夹选项/文件类型。
  2. 我已经有了Bash Script的SH条目。如果您没有,请单击“新建”并输入“SH”。创建一个。
  3. 选择SH扩展名后,单击“高级”。
  4. 选择“打开”按钮操作并单击编辑(或创建操作)。
  5. 这是要使用的命令:" C:\ cygwin \ bin \ bash.exe" -li“%1” %* 即可。请注意,如果没有 -li ,则表示正在撤消“未找到命令”。在我的脚本上。
  6. 您可能还想将 SH 添加到 PATHEXT 环境变量中:

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

    感谢您的帮助,伙计们!

其他提示

这是我的解决方案。它适用于我的* .sh脚本 无论它们在目录层次结构中的何处。 请注意,我在调用之前cd到cygpath dirname bash在cygpath上。它只是有效。

assoc .sh=bashscript

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

我一直在与德拉戈什'溶液对于某些时间,现在我把它作为最佳之一,因为它eleminates需要使用"cygpath-u"在你的外壳脚本。

然后我想要有额外的功能,如拖拉的支持。sh。庆典的文件。经过一些周围挖我写的一个。蝙蝠伙伴。sh。庆典的文件,如"bashscript"和激活Windows资源管理器的拖下的处理程序。我不得不编辑德拉戈什'的命令,以使它处理1个参数(路径的项目下降了shell script)。

。蝙蝠的文件大致如下:

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

该"argshandler.sh"脚本Cygwin根只是cygpaths回第一个参数,它接收并什么都没有,如果没有任何(例如如果你只是双击一个脚本文件):

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

所有这些职工很好地为止。然而,仍然有一些缺点,将是很好可以解决:

  • 德拉戈什'的命令,我的衍生物就会失败的时候脚本,位于北的路径,例如\\myserver\myshare\scriptfile.sh
  • 只有1下降的项目将传递给壳脚本。

不知怎的,关于1-下降项目-唯一的问题,更改的参数处理程序脚本给回喜欢的东西

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

和改变传手的德拉戈什'的命令喜欢的东西

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

(请注意""周围argshandler.sh 部分都消失了)似乎不会正常工作:如果某些项目的拖到一个脚本包含一个空白,在他们的道路,说路径,将被分解为多个论点在的空白即使他们每个人都是封闭的双引号...奇怪的。

是否有任何命令行专业人员觉得它解决一个或两个这些问题?

这不会关联.sh文件,但它可能会让你得到你想要的。我开始使用启动Cygwin bash shell的cygwin.bat批处理文件,并对其进行修改:

$ 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中创建.bat文件时,我注意到我必须实际上是“chmod + x test.bat”。在我通过双击启动它之前。我认为它正在设置NTFS权限。如果您只是使用记事本,则不需要这样做。

这是我正在使用的命令:

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

它以精简形式运行,最大化,将窗口标题设置为正在运行的脚本(Windows路径),将目录更改为脚本所在的位置,运行它并在完成后保持打开状态。

或者,这会将标题设置为脚本的cygwin路径:

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

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

标题中的Windows路径:

@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

标题中的cygwin路径:

@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

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

cd `dirname "<*>"`
    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 

环顾不同的地方。我设法提出的是,首先从Windows中选择 C:\ cygwin64 \ bin \ mintty.exe &quot; Open with ...“对话 然后编辑

的注册表值
[Computer\HKEY_CLASSES_ROOT\Applications\mintty.exe\shell\open\command]

要,

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\" %*"  

一个有效的解决方案是创建一个 .bat 文件,它将打开cygwin并执行你的脚本。

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

@echo off

C:
chdir C:\cygwin\bin

bash --login -i ./go.sh

我只是没有打扰。我将.sh文件与Crimson Editor关联起来(因为我花了尽可能多的时间来解决bug,因为我实际运行它们)。现在,这是一个正确的“开放与/编辑”的问题。组合工作在文件类型&gt;高级。如果我知道Crimson Editor使用的DDE代码,那会让事情变得更容易;但是,就这篇文章而言,我无法找到它。

这让我想起了我的Mac时代(1993-2008),当时我曾尝试扫描应用程序而不是基本的AppleScript脚本编写能力。

BZT

我用PuttyCyg(真棒腻子在Cygwin窗口),这里是如何得到它的所有会:

创建一批脚本,例如。在我的计算机上我用

C:\Dev\scripts\cygbashrun.bat

与内容

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

显然适应包含的道路安装的PuttyCyg.

然后在Windows文件管理工具文件夹的选项文件的类型

创建一个".sh"条目,如果没有已经(或.bash根据什么你喜欢你的脚本来有)..那么先进的..

[可选步骤]变化的图标和选择Cygwin标从您的安装

然后:

  1. 新的..
  2. Action=运行Bashscript..
  3. 应用程序,用于执行这一行动=C:\Dev\scripts\cygbashrun.bat "%1"

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

我自己开发了一个.bat脚本(不是源自其他人的答案),将文件类型(例如* .cygwin)与此.bat关联打开,如下所示:

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

使用语法的详细解释(如果您感兴趣):

  1. %1是“......”如果关联要使用此.bat打开的文件,则引用。为了将文件拖动到该.bat,它是“...”。仅在文件的路径包含空格时引用。
  2. %〜1与%1相同,如果它们存在则消除了周围的双引号
  3. 从%p%中删除周围的双引号,使用表示%% a in(%p%)do p = %% ~a
  4. 您必须使用&quot;%〜1&quot; 强制脚本文件的路径加双引号,以便在bash时不会删除其文件夹分隔符'\'(在%1中)被视为转义字符。否则,将此路径中没有空格的文件拖动到此.bat时,它不起作用。
  5. &quot; exec bash&quot;可以只是“bash”,前者用于为另外一个bash进程节省资源。
  6. 喜欢:)

查看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 命令中更改bash的路径,以匹配已安装 cygwin 的位置

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top