将bash脚本转换为Windows脚本 [英] Convert bash script into Windows script

查看:152
本文介绍了将bash脚本转换为Windows脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Unix shell脚本.我想将其转换为Windows .bat文件(我知道我可以使用Cygwin而不是使其适应Windows环境.但是Cygwin对我来说不是一个选择).

I have the following Unix shell script. I would like to convert this into a Windows .bat file (I know I can use Cygwin instead of adapting it to a Windows environment. But Cygwin is not an option for me).

我知道我可以在线使用Windows PowerShell阅读材料.但是我不想花数小时从这一基础上在线学习基础知识.请不要因为懒惰而羞辱我.我相信这可以为将来的在线搜索提供快速指南,对其他人也有帮助.

I know I can use Windows PowerShell reading material online. But I don't want to spend hours online learning from basics for this one time requirement. Please don't bash me for being lazy. I am sure this helps others as well by being a quick guide for someone searching online in the future.

这是脚本:

#!/bin/bash
echo ""
cat $1 | grep -A4 "Device_name"; echo ""
cat $1 | grep -A45 "Device_Oops"; echo ""
cat $1 | grep -A150 "Processes:" | sed '/Radar/q'; echo ""
cat $1 | grep -E '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]' | grep -i -E 'error|restart' 

要回答有关我尝试过的问题,我在运行"find"命令时遇到困难,该命令等效于每个网站grep

To answer questions on what I tried, I have trouble running the "find" command which is the equivalent of grep per this website http://tldp.org/LDP/abs/html/dosbatch.html

这是我的Joy.txt文件(下两行):

Here is my Joy.txt file (next two lines):

Device_name router@home
testing only

然后在PowerShell提示符下,运行以下命令:

Then at the PowerShell prompt I ran the following command:

cat Joy.txt | find "Device_name"

我希望看到上面文件中的第一行.但是相反,我得到了参数格式不正确的错误.有人可以帮忙吗?

I was expecting to see the first line in the above file. But instead I get the parameter format not correct error. Can someone help please?

推荐答案

在Windows的本机grep等效物findstr(或find)中,不存在或多或少的直接与grep -A等效的内容.但是,PowerShell中的Select-String带有-Context参数.

A more or less direct equivalent of grep -A does not exist in findstr (or find), Windows' native grep equivalent. However, Select-String in PowerShell has this with the -Context parameter.

如果我正确理解了您的脚本,则表示:

If I understand your script correctly, it means:

  • 写空行
  • 写所有包含"Device_name"的行,然后是四行上下文,然后是空行
  • 写所有包含"Device_Oops"的行,然后是上下文的45行,然后是空行
  • 如果所有包含"Radar"的行也包含"Processes:",或者在包含"Processes:"的行之后的前150行之内,则将其写入.
  • 不区分大小写地写所有包含三对数字的行,并用冒号隔开,冒号也包含错误"或重新启动".

所以它或多或少地归结为:

So it more or less comes down to something like:

$f = Get-Content $args[0]
function Emulate-Grep {
    begin { $first = $true }
    process {
      if (!$first) { '--' }
      $_.Line
      $_.Context.PostContext
      $first = false
    }
}

Write-Host
$f | Select-String -CaseSensitive -Context 0,4 'Device_name' | Emulate-Grep; Write-Host
$f | Select-String -CaseSensitive -Context 0,45 'Device_Oops' | Emulate-Grep; Write-Host
[string[]]($f | Select-String -CaseSensitive -Context 0,150 'Processes:' | Emulate-Grep) -split "`n" -cmatch 'Radar'; Write-Host
$f -match '\d{2}(:\d{2}){2}' -match 'error|restart'

(未经测试)

请注意,由于尝试模仿grep的输出行为,因此这有点难看.

Note that this is slightly ugly due to the attempt to emulate grep's output behaviour.

如果您只需要匹配行和以下内容,那么我只需编写一个小函数:

If you just need matching lines and the following, then I'd simply write a small function:

function Get-Matching([array]$InputObject, [string]$Pattern, [int]$Context = 0) {
    $n = $InputObject.Length - 1
    0..$n |
      where { $InputObject[$_] -cmatch $Pattern } |   
      foreach { $InputObject[$_..($_+$Context)] }
}

然后在不再那么复杂的脚本中使用它(仍然尝试重新创建一些输出选择,例如空行):

And then use it in a script that isn't so complex anymore (still trying to recreate some of your output choices, e.g. empty lines):

$f = gc $args[0]
Write-Host
Get-Matching $f Device_name 4; Write-Host
Get-Matching $f Device_Oops 45; Write-Host
Get-Matching $f 'Processes:' 150 | ? { $_ -cmatch 'Radar' }; Write-Host
Get-Matching $f '\d{2}(:\d{2}){2}' | ? { $_ -match 'error|restart' }

您还将注意到,我摆脱了Select-String,这是我从未真正了解其目的的cmdlet(除了提供grep/findstr的紧密匹配,但通常我发现其他方式更灵活)

You'll also notice that I got rid of Select-String, a cmdlet I never really understood the purpose of (except providing a close match of grep/findstr, but usually I find other means more flexible).

这篇关于将bash脚本转换为Windows脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆