Running a shell script through Cygwin on Windows

后端 未结 6 1911
有刺的猬
有刺的猬 2020-12-07 18:14

I have a bunch of shell scripts that used to run on a Linux machine. Now, we\'ve switched over to Windows, and I need to run these scripts there. I have Cygwin installed, bu

相关标签:
6条回答
  • 2020-12-07 18:52

    Just wanted to add that you can do this to apply dos2unix fix for all files under a directory, as it saved me heaps of time when we had to 'fix' a bunch of our scripts.

    find . -type f -exec dos2unix.exe {} \;
    

    I'd do it as a comment to Roman's answer, but I don't have access to commenting yet.

    0 讨论(0)
  • 2020-12-07 18:54

    The existing answers all seem to run this script in a DOS console window.

    This may be acceptable, but for example means that colour codes (changing text colour) don't work but instead get printed out as they are:

    there is no item "[032mGroovy[0m"

    I found this solution some time ago, so I'm not sure whether mintty.exe is a standard Cygwin utility or whether you have to run the setup program to get it, but I run like this:

    D:\apps\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico  bash.exe .\myShellScript.sh
    

    ... this causes the script to run in a Cygwin BASH console instead of a Windows DOS console.

    0 讨论(0)
  • 2020-12-07 19:05

    If you have access to the Notepad++ editor on Windows there is a feature that allows you to easily get around this problem:

    1. Open the file that's giving the error in Notepad++.
    2. Go under the "Edit" Menu and choose "EOL Conversion"
    3. There is an option there for "UNIX/OSX Format." Choose that option.
    4. Re-save the file.

    I did this and it solved my problems.

    Hope this helps!

    Read more at http://danieladeniji.wordpress.com/2013/03/07/microsoft-windows-cygwin-error-r-command-not-found/

    0 讨论(0)
  • 2020-12-07 19:11

    Sure. On my (pretty vanilla) Cygwin setup, bash is in c:\cygwin\bin so I can run a bash script (say testit.sh) from a Windows batch file using a command like:

    C:\cygwin\bin\bash testit.sh
    

    ... which can be included in a .bat file as easily as it can be typed at the command line, and with the same effect.

    0 讨论(0)
  • 2020-12-07 19:12

    One more thing - if You edited the shell script in some Windows text editor, which produces the \r\n line-endings, cygwin's bash wouldn't accept those \r. Just run dos2unix testit.sh before executing the script:

    C:\cygwin\bin\dos2unix testit.sh
    C:\cygwin\bin\bash testit.sh
    
    0 讨论(0)
  • 2020-12-07 19:12

    If you don't mind always including .sh on the script file name, then you can keep the same script for Cygwin and Unix (Macbook).

    To illustrate:
    1. Always include .sh to your script file name, e.g., test1.sh
    2. test1.sh looks like the following as an example:
    #!/bin/bash echo '$0 = ' $0 echo '$1 = ' $1 filepath=$1 3. On Windows with Cygwin, you type "test1.sh" to run
    4. On a Unix, you also type "test1.sh" to run


    Note: On Windows, you need to use the file explorer to do following once:
    1. Open the file explorer
    2. Right-click on a file with .sh extension, like test1.sh
    3. Open with... -> Select sh.exe
    After this, your Windows 10 remembers to execute all .sh files with sh.exe.

    Note: Using this method, you do not need to prepend your script file name with bash to run

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