我正在争论我是否应该学习PowerShell,还是坚持使用Cygwin/Perl脚本/Unix shell脚本等等。

PowerShell的好处是,没有Cygwin的队友可以更容易地使用脚本;然而,我不知道我是否真的会写那么多通用脚本,或者人们是否会使用它们。

Unix脚本功能如此强大,PowerShell是否足以让我们切换到它呢?

以下是我在PowerShell中寻找的一些具体内容(或等效内容):

grep 排序 uniq Perl (PowerShell与Perl的能力有多接近?) AWK sed File(提供文件信息的命令) 等。


当前回答

我已经使用了一些PowerShell来实现脚本自动化。虽然环境看起来比Unix shell考虑得更周到,但实际上使用对象而不是文本流要笨拙得多,而且过去30年开发的许多Unix设施仍然缺失。

Cygwin仍然是我在Windows主机上选择的脚本环境。在完成任务方面,它肯定胜过其他选择。

其他回答

我发现PowerShell编程不值得付出努力。

我有几年在Unix下编写shell脚本的经验,但是我发现用PowerShell做任何事情都非常困难。

似乎许多函数都需要您查询Windows管理界面并发出类似sql的命令来获得所需的信息。

例如,我想编写一个脚本来从目录树中删除具有特定后缀的所有文件。在Unix下,这将是一个简单的…

find . -name \*.xyz -exec rm {} \;

在几个小时的脚本之后。FileSystemObject和WScript。Shell并发出“SELECT * FROM Win32_ShortcutFile WHERE Drive = '”& Drive & " and Path = '" & searchFolder & "'”,我最终放弃了,并解决了Windows资源管理器的搜索命令,只是手动执行。可能有一些方法可以做到我想要的,但我没有看到任何明显的东西,MSDN网站上的所有示例都是如此微不足道,以至于毫无价值。

EDIT嘿,当然,当我写这篇文章的时候,我又戳了一下,发现了我遗漏的东西:remove-item命令的-递归选项是错误的(如果你使用get-help remove-item -detailed就会显示出来)。

我一直在尝试“remove-item -filter '* .xyz' -recurse”,但它不起作用,所以我放弃了它。

事实证明你需要使用get-childitem -filter '*。Xyz ' -递归| remove-item

TL;DR——我不讨厌Windows或PowerShell。我在Windows或PowerShell上什么都做不了。


我个人仍然觉得PowerShell充其量只能说是平淡无奇。

tab completion of directory paths do not compound, requiring the user to enter a path separator after every name completion. I still feel like Windows doesn't even have the concept of a path or of what a path is, with no accessible user home indicator ~/ short of some @environment://somejibberish/%user_home% NTFS is still a mess and seemingly always will be. Good luck navigating. cmd-esque interface, The dinosaur cmd.exe is still visible in PowerShell, Edit → Mark still being the only way to copy information, and copying only in the form of rectangular blocks of visible terminal space. and Edit → Mark still being the only way to paste strings into the terminal. Painting it blue doesn't make it any more attractive. I don't mind Microsoft developers having a taste in color though. Windows always opens at top left corner of screen. For somebody who uses vertical task bars this is incredibly annoying, especially considering that the Windows task bar will cover the only corner of the window that gives access to copy/paste functionality.

关于Windows包含的工具,我不能说太多。由于有一整套开源的、免费授权的CLI工具,而且PowerShell附带了这些工具,据我所知,没有一个是完全令人失望的。

PowerShell的wget的参数似乎与GNU的wget无法相比。谢谢,希望之光便携无用。 PowerShell POSIX与bash不兼容,特别是&&运算符没有被处理,使得最简单的条件命令也不能跟在后面。

我不了解人类;我试过了,真的;我仍然试着给它一个机会,希望下次我打开它的时候,它不会那么无用。我不能在PowerShell中做任何事情,我也几乎不能在真正的项目中将GNU工具引入Windows。

MySysGit为我提供了带有几个GNU工具的陈旧cmd.exe提示符,它仍然非常令人印象深刻,但最终路径补全工作了。Git命令将在Git Bash中运行。

Mintty for MySysGit为MySysGit的环境提供了Cygwin界面,使复制和粘贴一个东西(选择复制(鼠标),Shift+Ins粘贴,多么现代…)然而,像git push这样的东西在Mintty中被破坏了。

我并不是要抱怨,但我仍然看到Windows上命令行可用性的巨大问题,即使使用了Cygwin这样的工具。


注:仅仅因为在PowerShell中可以做一些事情,并不意味着它就可用。可用性比能力更重要,这也是我作为消费者在使用产品时所关注的问题。

我已经使用了一些PowerShell来实现脚本自动化。虽然环境看起来比Unix shell考虑得更周到,但实际上使用对象而不是文本流要笨拙得多,而且过去30年开发的许多Unix设施仍然缺失。

Cygwin仍然是我在Windows主机上选择的脚本环境。在完成任务方面,它肯定胜过其他选择。

您还可以尝试使用BashWin at在Windows上运行Bash脚本 https://github.com/skanga/BashWin。

这里有很多很棒的答案,这是我的看法。PowerShell是准备好了,如果你是…例子:

grep = "Select-String -Pattern"

sort = " sort - object "

uniq = "Get-Unique"

file = "Get-Item"

cat = "Get-Content"

Perl/AWK/Sed不是命令,而是实用程序,因此很难比较,但在PowerShell中几乎可以做所有事情。