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

时间:2009-03-14 20:31:20

标签: linux bash shell command-line

我有这个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

它停在那里。

3 个答案:

答案 0 :(得分:3)

很多事情都错了。

  1. 不要使用dir或ls for for循环。
  2. 为何选择eval?你期望得到什么?
  3. 您使用$ line而不定义它。
  4. 不要使用bc来做数学,因为bash已经准备好了。
  5. 不要使用日期来衡量时间,bash已经为此提供了命令。
  6. 什么是“-f”传递给processLine()?
  7. 另一个尝试,解决一些问题:

    #!/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 :(得分:0)

1)$line定义在哪里?

2)你有没有使用$i

3)您可以通过

循环扩展
for ext in avi mov mpg wmv; do
    ffmpg ...
done

4)你可以用双parens进行bash的基本算术运算。所以$(($x-$y))而不是管道到bc

答案 2 :(得分:0)

我闻到了一个bash陷阱。

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