将简单的 Bash 脚本转换为 PowerShell?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8650689/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:13:30  来源:igfitidea点击:

Convert simple Bash script to PowerShell?

bashpowershellffmpegavi

提问by vulcan raven

I have pulled the Bash script from here, which checks the AVI file for bad frames using ffmpeg and cygwin extension. I am able to execute the code in Mingw. I put ffmpeg.exe (ren ffmpeg), cygwin1.dll & cygz.dll in Mingw's bin dir (/c/mingw/bin/). Now, I am looking to port this bash code to PowerShell. Can anyone shed some PowerShell light on this one?

我从这里提取了 Bash 脚本,它使用 ffmpeg 和 cygwin 扩展检查 AVI 文件是否有坏帧。我能够在 Mingw 中执行代码。我将 ffmpeg.exe (ren ffmpeg)、cygwin1.dll 和 cygz.dll 放在 Mingw 的 bin 目录 (/c/mingw/bin/) 中。现在,我希望将此 bash 代码移植到 PowerShell。任何人都可以对这个 PowerShell 有所了解吗?

Script:(path: /c/mygw/bin/AviConvert)

脚本:(路径:/c/mygw/bin/AviConvert)

#!/bin/bash

FFMPEG="ffmpeg"
LIST=`find | grep \.avi$`

for i in $LIST; do
    OUTP="$i.txt"
    OUTP_OK="$i.txt.ok"
    TMP_OUTP="$i.tmp"
    if [ -f "$OUTP" -o -f "$OUTP_OK" ] ; then
    echo Skipping "$i"
    else
    echo Checking "$i"...
    RESULT="bad"
    ffmpeg -v 5 -i "$i" -f null - 2> "$TMP_OUTP" && \
        mv "$TMP_OUTP" "$OUTP" && \
        RESULT=`grep -v "\(frame\)\|\(Press\)" "$OUTP" | grep "\["`
    if [ -z "$RESULT" ] ; then
        mv "$OUTP" "$OUTP_OK"
    fi
    fi
done

回答by pmod

If you would not be able to find similar already cooked in PowerShell, your only chance is to understand this script's logic and write one in PowerShell from scratch since there are big differences.

如果你在 PowerShell 中找不到类似的已经熟的,你唯一的机会是理解这个脚本的逻辑并从头开始在 PowerShell 中编写一个,因为存在很大差异

Look at the difference in syntax/commands and make appropriate translation. Some Bash vs Powershellrelated posts/docs available in web, e.g. this. And of course refer to PowerShell Getting Started manuals. For example syntax for foris different, for PowerShell it is:

查看语法/命令的差异并进行适当的翻译。网络中提供的一些Bash 与 Powershell相关的帖子/文档,例如这个. 当然,请参阅 PowerShell 入门手册。例如,for 的语法不同,对于 PowerShell,它是:

for (_init_, _cond_, _incr_) { 
   _operators_
}

BTW, in your case it's better to use foreach, i.e. having something like:

顺便说一句,在您的情况下,最好使用foreach,即具有以下内容:

(get-childitem $path -Recurse | select-string -pattern .avi | % {$_.Path} > matchingfiles.txt)
$FILESARRAY = get-content matchingfiles.txt
foreach ($FILE in $FILESARRAY)
{
(get-content $FILE ) |foreach-object {$_ -replace $find, $replace} | set-content $FILE
}