windows – 在PowerShell函数中不起作用的字符串比较 – 我做错
发布时间:2020-12-14 02:56:10 所属栏目:Windows 来源:网络整理
导读:我正在尝试创建一个git commit的别名,它也会将消息记录到一个单独的文本文件中.但是,如果git commit返回“没有提交(工作目录清理)”,它不应该将任何内容记录到单独的文件中. 这是我的代码. git commit别名有效;输出到文件的工作原理.但是,无论从git commit返
我正在尝试创建一个git commit的别名,它也会将消息记录到一个单独的文本文件中.但是,如果git commit返回“没有提交(工作目录清理)”,它不应该将任何内容记录到单独的文件中.
这是我的代码. git commit别名有效;输出到文件的工作原理.但是,无论从git commit返回什么,它都会记录消息. function git-commit-and-log($msg) { $q = git commit -a -m $msg $q if ($q –notcontains "nothing to commit") { $msg | Out-File w:log.txt -Append } } Set-Alias -Name gcomm -Value git-commit-and-log 我正在使用PowerShell 3. 解决方法
$q包含Git stdout每行的字符串数组.要使用-notcontains,您需要匹配数组中项的完整字符串,例如:
$q -notcontains "nothing to commit,working directory clean" 如果要测试部分字符串匹配,请尝试使用-match运算符. (注意 – 它使用正则表达式并返回匹配的字符串.) $q -match "nothing to commit" 如果左操作数是一个数组,-match将起作用.所以你可以使用这个逻辑: if (-not ($q -match "nothing to commit")) { "there was something to commit.." } 另一种选择是使用-like / -notlike运算符.这些接受通配符并且不使用正则表达式.将返回匹配(或不匹配)的数组项.所以你也可以使用这个逻辑: if (-not ($q -like "nothing to commit*")) { "there was something to commit.." } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows-phone-7 – 没有执行Periodictask
- Windows等同于sync()
- 如何在Windows上获取线程堆栈信息?
- windows – 在哪里使用null BSTR记录?
- windows – 一个很好的免费屏幕共享程序,用于配对编程?
- Windows系统·添加Python import 的库/模块路径
- Windows – Win32命名管道和邮件大小限制 – 旧的64K限制仍
- windows-server-2008 – 在Active Directory中禁用密码复杂
- .net – 在WPF中,如何从包含ListBox的DataTemplate内部数据
- windows-server-2016 – 存储空间直接指南