Problem with the arithmetics in the bash scripting
I'm trying to cut a video into 2-minute clips using FFMpeg. I am using Ubuntu 10.10.
Her开发者_开发问答e is my code:
#!/bin/sh
COUNTER=0
BEG=0
MIN=`ffmpeg -i ${1} 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,// | cut -d ":" -f 2`
echo $MIN
((MIN=MIN-2))
before_last_dot=${1%.*};
while [ $COUNTER -lt $MIN ]; do
((BEG=COUNTER*60))
echo "MIN:${MIN}"
echo "ffmpeg -sameq -i ${1} -ss ${BEG} -t 120 ${before_last_dot}.${COUNTER}.wmv"
((COUNTER=COUNTER+2))
done
echo "ffmpeg -sameq -i ${1} -ss ${BEG} -t 120 ${before_last_dot}.${COUNTER}.wmv"
should be ffmpeg -sameq -i ${1} -ss ${BEG} -t 120 ${before_last_dot}.${COUNTER}.wmv
. I print it to check it. ${1} is the video name.
But the problem is, ((COUNTER=COUNTER+2))
or ((COUNTER+=2))
never works! COUNTER
is always 0, BEG
is always 0 too. ((MIN=MIN-2))
never works too.
I tried to replace ((MIN=MIN-2))
with let "MIN-=2"
I get an error: let: not found
I+ve double checked but still don't know why. I'm getting gray hair on this.
The ((MIN=MIN-2))
syntax that you're using is a bash
-specific feature.
I don't have Ubuntu 10.10 to hand to test with, but I'd guess that your /bin/sh
is not bash
, but a smaller and simpler shell with only the basic features required by POSIX. (In which case, ((MIN=MIN-2))
probably launches a sub-shell, which launches a sub-shell, which does nothing but set a variable MIN
to the string MIN-2
and then exit.)
Try #!/bin/bash
on the first line instead.
精彩评论