【问题标题】:Bash like management in windows environment [closed]Windows环境中的类似Bash的管理[关闭]
【发布时间】:2015-04-25 20:26:21
【问题描述】:

我已经在 linux 上的 bash 工作了几年。最近我搬到了窗户。在 bash 中,我可以运行几个 .cpp 文件,从一个文件中获取输出并以非常流畅的方式将其提供给另一个文件。但是我也想对.bat 文件做同样的事情,但我找不到关于它的教程。请指导我完成整个过程。

【问题讨论】:

  • 为什么不使用bash? :) cygwin.com
  • 如果您对某个答案感到满意,您可以通过单击其左侧的管道图标来接受它。对回答者来说,这是一个很大的奖励。
  • 如果我用 bash (cygwin) 编写,它会为其他不使用 cygwin 的系统制作可移植脚本吗?

标签: linux windows bash batch-file


【解决方案1】:

您可以在 Windows 中完全使用 bash。它是Cygwin 分发的一部分。

还有MinGW,试图做一个极简的Cygwin。在我看来,它目前几乎一样大,并且存在重大问题 - 除了其名为 mintty 的集成终端仿真器之外。

作为替代方案,如果您想要原生技术,您可以使用PowerShell。但它需要大量的适应,大多数 bash 功能都不是微不足道的。

在类似情况下,我通常将 cygwin64 与 mintty 一起使用(AFAIK 它也包含在 cygwin 中)。

【讨论】:

  • 不错的概述;如果你只想要bash + 实用程序,MinGW 的配套产品 MSYS 就足够了:mingw.org/wiki/MSYS
  • @mklement0 非常感谢!
【解决方案2】:

DOS 由 Unix 负责人 Microsoft 编写,其语法类似于 Unix。这是他们购买 DOS 时添加的第一件事。

&    separates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

>    output to a file

>>    append output to a file

<    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatenate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% an inbuilt or user-set environment variable

!variablename! a user-set environment variable expanded at execution time, turned with command SetLocal EnableDelayedExpansion

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batch file's name.

%* (%*) the entire command line.

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.

此外,由于 Windows 运行 Unix(不同的风格,取决于版本。当前的至少带有 Korn shell,但不是 Bash)。根据 Windows 版本和版本,它要么在 DVD 上,要么必须从 MS 下载。首先,在控制面板中查看 Windows 程序和功能以打开。否则,http://www.microsoft.com/en-au/download/details.aspx%3Fid%3D274&rct=j&frm=1&q=&esrc=s&sa=U&ei=KgY8VaGtC4W1mAWVqoCQCQ&ved=0CB8QFjAB&usg=AFQjCNEpycuKGGDSnWw8_RExMybUkITv3Q

Windows NT 是一个运行其他操作系统的操作系统。 Windows、OS/2 2.1 和几个 POSIX/Unix 实现。

【讨论】:

  • 在基于 NT 的系统上没有更多的 dos。 dos 中断仅导致页面冲突。 cmd.exe,command.com 的继承者,是一个原生的 win32 (win64) 应用程序。 dos只有模拟器。
  • 我没有提到dos,我说的是Windows、OS/2(直到NT4)和unix/posix。 NT 确实有 Dos 5.5,但那是一个 Win32 模拟器而不是一个单独的操作系统,这就是我没有提到它的原因。但是我们的命令行语法来自 MS 的 unix 程序员,当时他们发布了 Dos ver 2
  • 您两次提到 DOS,这本身不是问题,您是否还提到过 DOS 批处理语言的 扩展 版本(尽管具有不同的技术基础)是在当今基于 NT 的 Windows 平台上仍在使用(请参阅en.wikipedia.org/wiki/Cmd.exe)。您的语法概述对想要学习如何创建 批处理文件 的人很有帮助,但对希望使用 bash 技术和现有 Unix shell 脚本的人没有帮助。真正希望切换到与bash 和 Unix 实用程序相当的 Windows 原生技术的人应该考虑使用 PowerShell。
  • 由于您仅针对 %variablename% 而不是 !variablename! 提及内置变量:任何一种形式都可以访问任何变量,无论是预定义的还是用户定义的,区别仅在于 when 它们被扩展(如你所说)。
  • 关于将 POSIX/Unix 作为操作系统运行"Windows Services for Unix and Subsystem for Unix-based Applications provide header files and libraries that make it easier to recompile or port Unix applications for use on Windows; they do not make Unix binaries compatible with Windows binaries."。 POSIX 层在 Windows Server 2003(您链接到的那个)之前称为 SFU(Unix 的 Windows 服务),然后是 SUA(基于 UNIX 的应用程序的子系统),现在已弃用从 Windows 8.1 和 Windows Server 2012 R2 中删除
猜你喜欢
  • 2010-11-11
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 2017-10-26
相关资源
最近更新 更多