转换文件如果不存在

时间:2016-05-10 18:08:40

标签: bash file scripting

我正在尝试编写bash脚本。我想要的是以下

  

1)以递归方式扫描所有.mkv个文件的目录   2)发现 IF .mkv文件   
a)如果.mkv档案正在使用中            i)如果是,请离开目录
           ii)如果没有继续处理
        b)检查 CURRENT 目录是否有.mp4   
i)如果是的话            ii)如果没有转换为.mp4   
3)扫描下一个目录


这是我目前的剧本,我认为除了两件外,我有所有元素在玩

  

1)捕获当前目录
2)检查当前目录是否有.mp4

我尝试使用$PWD命令捕获当前目录,但它始终只返回此目录/home/omartinez/Downloads

我必须做些什么才能完成上述2个未知步骤?

#/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'|(。)/(。)。mkv $ | \ 1 / \ 2.mp4 |')'

我确信这对我来说是百分之百,但有人可以指出我做错了什么吗?

编辑---
使用@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

有没有办法实现我的目标?

2 个答案:

答案 0 :(得分:0)

也许这样的事情会有所帮助:

for file in $(find . -type f -name "*.mkv")
do
    find . -wholename "$(echo ${file} | perl -pe 's|(.*)/(.*).mkv$|\1/\2.mp4|')" > /dev/null
    || convert ${file} $(echo ${file} | perl -pe 's|(.*)/(.*).mkv$|\1/\2.mp4|')
done
  • 查找以.mkv结尾并设置为变量
  • 的文件
  • 将文件名发送到perl以去除路径;找到定位mp4版本并丢弃结果
  • 如果找不到mp4,请使用文件名和条带路径将mkv版本转换为mp4

答案 1 :(得分:0)

语法错误cm_empleado由空cm_valuacion子句引起。尝试:

id_siniestro

如果您在文件正在使用时不想做任何事情。

整体而言,您的解决方案似乎过于复杂类似的东西:

id