Linux Shell脚本-两个用于循环和更改文件扩展名的脚本
我有这个Shell脚本,我已经设法把它弄糟了,我希望我能被纠正,走上正确的道路,希望能添加一些我自己没有能力做的事情。我已经在下面的Shell脚本中将我想要做的作为注释Linux Shell脚本-两个用于循环和更改文件扩展名的脚本,linux,bash,shell,command-line,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 do
#!/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)哪里定义了$line
2) 您是否使用过$i
3) 您可以通过
for ext in avi mov mpg wmv; do
ffmpg ...
done
4) 可以使用双参数在bash中执行基本算术。因此,$($x-$y))
而不是管道到bc1)哪里定义了$line
2) 您是否使用过$i
3) 您可以通过
for ext in avi mov mpg wmv; do
ffmpg ...
done
4) 可以使用双参数在bash中执行基本算术。因此,$($x-$y))
而不是管道到bc许多事情都是错误的
#!/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
#/bin/bash
时间格式=%6R
用于/home/test/videos/*中的文件;做
如果[!-f“$file”];然后
继续#任何非常规文件
fi
对于avi mpg wmv mov中的ext;做
(i=0;i<10;i++);做
base=“${file##*/}”
运行时间=$({time ffmpeg-i“$file”-ar 44100-y“${file%/*}/done/${base%.*}-$i.$ext”&>/dev/null;}2>&1)
echo“$file$ext$i花费了$s秒”
完成
完成
完成
很多事情都是错的
#!/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
#/bin/bash
时间格式=%6R
用于/home/test/videos/*中的文件;做
如果[!-f“$file”];然后
继续#任何非常规文件
fi
对于avi mpg wmv mov中的ext;做
(i=0;i<10;i++);做
base=“${file##*/}”
运行时间=$({time ffmpeg-i“$file”-ar 44100-y“${file%/*}/done/${base%.*}-$i.$ext”&>/dev/null;}2>&1)
echo“$file$ext$i花费了$s秒”
完成
完成
完成
我闻到了一种猛击陷阱的味道
$ touch aaa
$ touch "bbb ccc"
$ ls -1
aaa
bbb ccc
$ for file in `dir -d *`; do echo $file; done
aaa
bbb\
ccc
我闻到一股猛击陷阱的味道
$ touch aaa
$ touch "bbb ccc"
$ ls -1
aaa
bbb ccc
$ for file in `dir -d *`; do echo $file; done
aaa
bbb\
ccc
最好保持函数运行ffmpeg。时间是一个内置的bash,它也可以处理函数。看看这个ffmpeg命令,我打赌它在第一轮维护中肯定不会成功。:)真是太复杂了!:)-我已经取消了所有内容的注释并运行了它。从输出来看,它工作得非常好。但我猜,它实际上并没有执行ffmpeg命令,对吗?未注释的什么?请注意,在我的示例中没有任何注释。只是语法高亮显示程序不理解bash代码。它运行ffmpeg,使用内置的时间来测量它的执行时间。啊,我明白了-对不起,我的错误。我已经运行了shell脚本,但由于某些原因,它在对视频目录中的一个文件执行ffmpeg命令后停止。我还换了循环2和3。在我交换了for循环之后,它只执行了一次转换。更新的问题。是否必须按enter键才能继续shell脚本?当我按enter键时,它会继续转换。最好保持函数运行ffmpeg。时间是一个内置的bash,它也可以处理函数。看看这个ffmpeg命令,我打赌它在第一轮维护中肯定不会成功。:)真是太复杂了!:)-我已经取消了所有内容的注释并运行了它。从输出来看,它工作得非常好。但我猜,它实际上并没有执行ffmpeg命令,对吗?未注释的什么?请注意,在我的示例中没有任何注释。只是语法高亮显示程序不理解bash代码。它运行ffmpeg,使用内置的时间来测量它的执行时间。啊,我明白了-对不起,我的错误。我已经运行了shell脚本,但由于某些原因,它在对视频目录中的一个文件执行ffmpeg命令后停止。我还换了循环2和3。在我交换了for循环之后,它只执行了一次转换。更新的问题。是否必须按enter键才能继续shell脚本?当我按enter键时,它将继续转换。