什么是 UNIX shell 脚本的 Windows 等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/296910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the Windows equivalent of a UNIX shell script?
提问by Murat Ayfer
I want to have an executable file that will call some other programs. The way I would do this in Linux is with a simple bash script that looks like this:
我想要一个可执行文件来调用其他一些程序。我在 Linux 中这样做的方法是使用一个简单的 bash 脚本,如下所示:
#!/bin/bash
echo "running some-program"
/home/murat/some-program arg1 arg2
What's the best way to do this kind of thing under Windows?
在 Windows 下做这种事情的最好方法是什么?
回答by Scott Dorman
Take a look at PowerShell, which is the closest you will get to a true scripting language like you have in Unix. Other than that, for simple things such as simply runnning an application, take a look at Windows command script/MS-DOS batch files.
看看PowerShell,它是最接近真正的脚本语言,就像在 Unix 中一样。除此之外,对于简单的事情,比如简单地运行一个应用程序,看看 Windows 命令脚本/MS-DOS 批处理文件。
回答by Bogdan
You could go with either an old school batch file (http://en.wikipedia.org/wiki/Batch_file) or you can go with a brand new shiny Windows PowerShell script (http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx).
您可以使用旧式批处理文件 ( http://en.wikipedia.org/wiki/Batch_file),也可以使用全新的闪亮 Windows PowerShell 脚本 ( http://www.microsoft.com/technet/脚本中心/集线器/msh.mspx)。
I use batch for simple operations and PowerShell for complex scripts that also involve data processing.
我将批处理用于简单操作,将 PowerShell 用于还涉及数据处理的复杂脚本。
回答by Joel Coehoorn
There are a number of scripting options on windows:
Windows 上有许多脚本选项:
- Batch files
- VBScript/CScript/JScript (very powerful when combined with WMI)
- PowerShell
- You could install Perl, Python, or other engine
- You can install unix tools, via Cygwin for example, to get make most of the common unix commands available in windows as well.
- Since we're all (mostly?) programmers here, there's nothing stopping you from making a .exe file to do what you want.
- 批处理文件
- VBScript/CScript/JScript(与 WMI 结合时非常强大)
- 电源外壳
- 您可以安装 Perl、Python 或其他引擎
- 例如,您可以通过 Cygwin 安装 unix 工具,以使大多数常见的 unix 命令也可以在 Windows 中使用。
- 因为我们都是(大部分?)程序员,所以没有什么能阻止你制作一个 .exe 文件来做你想做的事。
回答by Eric Wendelin
You can use a Windows batch file. Find more information in the Wikipedia article.
您可以使用 Windows 批处理文件。在Wikipedia 文章中查找更多信息。
回答by Martin Hollingsworth
The Windows NT family of Operating Systems (NT 3.51, NT 4.0, 2000, XP, Vista) have a reasonably powerful Command Shell even if it is a bit esoteric. This shell does not have the power of the typical UNIX shells however it is ubiquitous on Windows machines and it is pretty amazing what can be achieved. A great book for this is Windows NT Shell Scriptingby Timothy Hill. I always found Rob van der Woude's Scripting Pagesa great resource for batch files.
Windows NT 系列操作系统(NT 3.51、NT 4.0、2000、XP、Vista)具有相当强大的命令外壳,即使它有点深奥。这个 shell 没有典型的 UNIX shell 的功能,但是它在 Windows 机器上无处不在,并且可以实现的功能非常惊人。Timothy Hill所著的Windows NT Shell Scripting是一本很好的书。我总是发现Rob van der Woude 的 Scripting Pages是批处理文件的绝佳资源。
PowerShell is Microsoft's latest offering in this field and takes scripting to a whole new level and gives you access to all of the power of the .NET framework as well as WMI, COM and any other interface into Windows. There is a pretty steep learning curve with this but it is definitely the way to go and I think it has the potential to become one of those foundational technology skills (like RegEx, XPath etc) in the Microsoft world. A good starting point is Microsoft Windows PowerShell Technology Centerbut there are a huge number of resources out there for this.
PowerShell 是 Microsoft 在该领域的最新产品,它将脚本编写提升到一个全新的水平,让您可以访问 .NET 框架以及 WMI、COM 和任何其他 Windows 接口的所有功能。这有一个非常陡峭的学习曲线,但它绝对是要走的路,我认为它有可能成为 Microsoft 世界中的基础技术技能之一(如 RegEx、XPath 等)。一个很好的起点是Microsoft Windows PowerShell 技术中心,但有大量资源可用于此目的。
回答by Eric Wendelin
As long as we're suggesting *nix command-line tools, I'd recommend checking out Cygwin (http://www.cygwin.com) as it emulates a *nix-like environment.
只要我们建议使用 *nix 命令行工具,我就建议您查看 Cygwin ( http://www.cygwin.com),因为它模拟了类似 *nix 的环境。
回答by grieve
I used to use batch files. And for simple examples like you gave above I still do, but more and more I have been using pythonto do these sorts of things. It has the added benefit of working across platforms for those rare scripts that are actually meaningful on multiple platforms.
我曾经使用批处理文件。对于像你上面给出的简单例子,我仍然这样做,但我越来越多地使用python来做这些事情。对于那些在多个平台上真正有意义的罕见脚本,它具有跨平台工作的额外好处。
Python is just one of many scripting languages you could use for this sort of thing. Perl, ruby, or php may be more your speed. I find that a scripting language gives me a richer and easier to understand syntax if I want to do something a little more complicated than just start a program with a few default arguments.
Python 只是您可以用于此类事情的众多脚本语言之一。Perl、ruby 或 php 可能更适合您的速度。我发现,如果我想做一些比使用几个默认参数启动程序更复杂的事情,脚本语言可以为我提供更丰富、更易于理解的语法。
回答by Tim
batch files work for me. Just remove the first line in your example and it should work the same in win32 cmd line.
批处理文件对我有用。只需删除示例中的第一行,它应该在 win32 cmd 行中工作。
note - make a text file, name it with a .bat extension and use a backslash rather than a forward slash.
注意 - 创建一个文本文件,使用 .bat 扩展名命名并使用反斜杠而不是正斜杠。
回答by Wedge
Update: Wow, this is woefully out of date now (though that's inevitable). Powershell is definitely becoming more and more the language of choice for automation in Windows. It has many of the advantages of any .net language (like C#) combined with much of the expediency that exists in any good shell language, without the serious downsides of batch files.
更新:哇,这现在已经过时了(尽管这是不可避免的)。Powershell 肯定越来越成为 Windows 中自动化的首选语言。它具有任何 .net 语言(如 C#)的许多优点以及任何优秀的 shell 语言中存在的许多权宜之计,而没有批处理文件的严重缺点。
For what it's worth, the most popular options I've seen at a certain large, Redmond based software company (in rough order of popularity):
就其价值而言,我在某家总部位于雷德蒙德的大型软件公司看到的最受欢迎的选项(按受欢迎程度排序):
- Batch files
- Perl
- Windows Scripting Host (Javascript)
- Tiny exes
- 批处理文件
- 珀尔
- Windows 脚本宿主 (Javascript)
- 微小的前身
Much less popular:
不那么受欢迎:
- Powershell
- 电源外壳
Note that Perl doesn't require an install, it only requires a few environment variable updates, so it's easy to just shove a full distro into your source control system along with other tools.
请注意,Perl 不需要安装,它只需要一些环境变量更新,因此很容易将完整的发行版与其他工具一起推送到您的源代码控制系统中。
My personal preference would be to avoid batch files as much as possible, even with the most careful and rigorous of processes the benefit for the cost is just not worth it. You'll never end up with a satisfactory solution if you use batch. Perl and Javascript are tempting to use but there are troublesome fundamental deficiencies which leave a lot to be desired, without strong development guidelines these methods will more likely than not lead to low quality code.
我个人的偏好是尽可能避免使用批处理文件,即使采用最谨慎和最严格的流程,成本收益也不值得。如果您使用批处理,您永远不会得到令人满意的解决方案。Perl 和 Javascript 很容易使用,但存在一些麻烦的基本缺陷,还有很多不足之处,如果没有强有力的开发指南,这些方法很可能会导致低质量的代码。
Python and Powershell are good candidates but I'm not sufficiently adept with them and, more importantly, a lot of people aren't that familiar with them. Perhaps that's not a good enough excuse. Tiny exes can have many problems as well and require discipline and process in order to ensure that the source is always checked in, that the exes conform to certain UI norms (e.g. they have useful help if you run -? or /?), that they have an obvious owner, that the process for updating the exes is discoverable, etc.
Python 和 Powershell 是不错的选择,但我对它们还不够熟练,更重要的是,很多人并不熟悉它们。也许这不是一个足够好的借口。微小的 exe 也可能有很多问题,需要纪律和流程,以确保始终检查源,确保 exe 符合某些 UI 规范(例如,如果您运行 -? 或 /?,它们会得到有用的帮助),他们有一个明显的所有者,更新 exe 的过程是可发现的,等等。
回答by Nicole Hamilton
As an alternative to the underpowered Windows command line, the not very Windows-friendly Cygwin or the quirky and slow Powershell, you might consider Hamilton C shell. I am the author, so of course I'm possibly biased, but I am a real person and I do care about trying to make my customers happy. Hamilton C shell is a complete recreation of the UNIX C shell and utilities but all of it's from scratch for Windows and, as such, it's designed to follow the Windows conventions for filenames and so on. I just released a new version this summer for Vista that includes both 32-bit and 64-bit builds. (The 64-bit version is about 2x to 3x as fast as the 32-bit version.) There's a free demo, so you can try before you buy.
作为功能不足的 Windows 命令行、不太适合 Windows 的 Cygwin 或古怪而缓慢的 Powershell 的替代方案,您可能会考虑Hamilton C shell。我是作者,所以当然我可能有偏见,但我是一个真实的人,我确实关心如何让我的客户满意。Hamilton C shell 是对 UNIX C shell 和实用程序的完整重新创建,但所有这些都是针对 Windows 从头开始的,因此,它被设计为遵循 Windows 的文件名约定等。今年夏天,我刚刚为 Vista 发布了一个新版本,其中包括 32 位和 64 位版本。(64 位版本的速度大约是 32 位版本的 2 到 3 倍。)有一个免费演示,因此您可以先试用再购买。