【发布时间】:2016-09-05 20:52:33
【问题描述】:
我正在尝试编写bash 脚本。我想要的是以下
1) 递归扫描目录中所有
.mkv文件
2) IF.mkv文件找到检查
a) 如果.mkv文件正在使用中
i) 如果是,请离开目录
ii) 如果没有继续处理
b) 检查 CURRENT 目录是否有.mp4
i) 如果是,请执行
ii) 如果没有转换为.mp4
3) 扫描下一个目录
这是我目前的剧本,我认为除了两部分之外,我拥有所有的元素
1) 捕获当前目录
2) 检查当前目录是否有.mp4
我尝试使用$PWD 命令捕获当前目录,但它总是只返回此目录/home/omartinez/Downloads
我必须怎么做才能完成上面的两个未知步骤?
#/bin/sh
#recursive file search
#setting the directory to scan and filetype to scan for
for f in $(find /home/omartinez/Downloads) -name '*mkv' );
do
#checking if the file is open
if lsof $f
then
#this file is in use
else
#the file is closed and can be converted
avconv -i $f.mkv -codec copy $f.mp4
fi
done
编辑——
根据@M 的建议。下面的 Hicklen 我将我的代码修改为此(尝试使用该答案)
#/bin/sh
#recursive file search
for file in $(find /home/omartinez/Downloads -type f -name "*.mkv")
do
if lsof $f
then
#this file is in use
else
find /home/owner/Downloads -wholename "$(echo ${file} | perl -pe 's|(.*)/(.*).mkv$|\1/\2.mp4|')" > /dev/null
|| convert ${file} $(echo ${file} | perl -pe 's|(.*)/(.*).mkv$|\1/\2.mp4|')
fi
完成
但是,这会产生以下错误:
./search: 第 16 行:意外标记附近的语法错误
||' ./search: line 16:||转换 ${file} $(echo ${file} | perl -pe 's|(.)/(.).mkv$|\1/\2.mp4|')'
我确信这对我来说是 100%,但有人能指出我做错了什么吗?
编辑——
使用@jil 下面的建议,它递归地扫描我的整个系统,而不仅仅是列出的位置。我通过观察它扫描目录来验证这一点,例如
/usr/
/火狐
/Teamviewer
等
这是我使用的语法:
for file in $(find /home/omartinez/Downloads -type f -name "*.mkv"); do
mp4=${file%.mkv}.mp4
if ! lsof $f && [ ! -e "$mp4" ]; then
convert $file $mp4
fi
done
有没有办法实现我的目标?
【问题讨论】: