繁体   English   中英

Shell脚本-两个for循环和更改文件扩展名

[英]Shell script - Two for loops and changing extension of file

我有这个Shell脚本,我已经设法把它弄糊涂了,我希望我可以被纠正并走上正确的道路,并希望添加一些我没有能力胜任自己的事情。 我在下面的Shell脚本中将我想做的事情作为注释。

#!/bin/bash
#Get all files from dir "videos" and send to processLine function one at a time
cd /home/test/videos/
for file in `dir -d *` ; do
processLine -f $file
done

processLine(){
# I was hoping to have a further for loop that would loop 4 times and change the $ext
#variable to avi, mpg, wmv and mov
#For loop, execute a command on each file
for i in 1 2 3 4 5 6 7 8 9 10
do
START=$(date +%s.%N)
echo "$line"
#The saved file in done dir should have filename as $file + START.
eval "ffmpeg -i $file -ar 44100 /home/test/videos/done/$fileSTART.$ext" > /dev/null 2>&1

END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)

echo "$line, $START, $END, $DIFF" >> file.csv 2>&1
echo "It took $DIFF seconds"
echo $line
done
}

该脚本的基本思想是:从dir中获取所有文件,并对它们运行ffmpeg命令,并查看需要花费多长时间。 我正在尝试收集一些统计数据

感谢您的任何帮助

更新资料

利用Juliano的脚本并交换循环2和3。我设法在下面获得此输出:

.
.
.
/home/test/videos/done 8 mov took 0.012 seconds
/home/test/videos/done 9 mov took 0.012 seconds
/home/test/videos/video1236104961.flv 0 avi took 0.446 seconds

它在那里暂停。

许多事情是错误的。

  1. 不要在循环中使用dir或ls。
  2. 为什么要评估? 您期望得到什么?
  3. 您使用$ line而不定义它。
  4. 不要使用bc进行数学运算,因为bash已经具备执行数学的能力。
  5. 不要使用日期来衡量时间,bash已经提供了一个命令。
  6. 什么是传递给processLine()的“ -f”?

另一种尝试,修复了一些问题:

#!/bin/bash
TIMEFORMAT=%6R
for file in /home/test/videos/* ; do
  if [ ! -f "$file" ]; then
    continue   # anything that is not a regular file
  fi
  for ext in avi mpg wmv mov; do
    for (( i = 0; i < 10; i++ )); do
      base="${file##*/}"
      elapsed=$({ time ffmpeg -i "$file" -ar 44100 -y "${file%/*}/done/${base%.*}-$i.$ext" &>/dev/null; } 2>&1)
      echo "$file $ext $i took $elapsed seconds"
    done
  done
done

1) $line在哪里定义?

2)您曾经使用$i吗?

3)您可以通过

for ext in avi mov mpg wmv; do
    ffmpg ...
done

4)您可以使用双括号在bash中执行基本算术。 所以$(($x-$y))而不是管道到bc

我闻到了一个陷阱。

$ touch aaa
$ touch "bbb ccc"
$ ls -1
aaa
bbb ccc
$ for file in `dir -d *`; do echo $file; done
aaa
bbb\
ccc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM