简体   繁体   中英

Shell script - Two for loops and changing extension of file

I have this Shell script and I've managed to muck it up and I was hoping I could be corrected and put on the right path and hopefully add a few things that I am not competent enough to do myself. I have put what I want do as comments in the Shell script below.

#!/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
}

Basic idea of the script is to: Get all files from dir and run an ffmpeg command on them and see how long it takes. I am trying to collect some stats

Thank you for any help

Update

Making use of Juliano's script and swapping for loops 2 and 3. I have managed to get this output below:

.
.
.
/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

It pauses there.

Many things are wrong.

  1. Don't use dir or ls in for loops.
  2. Why eval? What you expected to get?
  3. You use $line without defining it.
  4. Don't use bc to do math, since bash is already equipped to do it.
  5. Don't use date to measure time, bash already provides a command for that.
  6. What is "-f" passed to processLine() ?

Another try, fixing some issues:

#!/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) Where is $line defined?

2) Do you ever use $i ?

3) You can loop over extensions via

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

4) You can do basic arithmetic in bash with double parens. So $(($x-$y)) instead of piping to bc

I smell a bash pitfall.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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