将简单的Bash脚本转换为PowerShell? [英] Convert simple Bash script to PowerShell?
问题描述
我已经从此处中提取了Bash脚本,该脚本使用ffmpeg检查AVI文件中是否存在不良帧.和cygwin扩展.我可以在Mingw中执行代码.我放了ffmpeg.exe(ren ffmpeg),cygwin1.dll&Mingw的bin目录(/c/mingw/bin/)中的cygz.dll.现在,我希望将此bash代码移植到PowerShell.任何人都可以从PowerShell上了解到这一点吗?
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?
脚本: (路径:/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
推荐答案
如果您无法在PowerShell中找到已经煮熟的类似脚本,则唯一的机会是理解此脚本的逻辑,并从存在很大差异.
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.
查看语法/命令的差异并进行适当的翻译.网络上有一些 Bash vs Powershell 相关的帖子/文档,例如此.当然,请参考《 PowerShell入门》手册.例如, for 的语法是不同的,而对于PowerShell,语法是:
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_
}
顺便说一句,在您的情况下,最好使用 foreach ,即具有以下内容:
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
}
这篇关于将简单的Bash脚本转换为PowerShell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!