Convert simple Bash script to PowerShell?

偶尔善良 提交于 2019-12-25 06:25:29

问题


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?

Script: (path: /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

回答1:


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.

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

for (_init_, _cond_, _incr_) { 
   _operators_
}

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

(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
}


来源:https://stackoverflow.com/questions/8650689/convert-simple-bash-script-to-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!