在bash中拆分一个单词for循环?

时间:2014-06-25 16:07:56

标签: bash scripting

我正在尝试使用简单的bash for循环来循环浏览我的视频并转码为其他格式

for i in $(ls *.MTS); do encode -i $i -o $i.mp4; done

但文件输出文件目前看起来像00000.MTS.mp4

如何替换输出变量,使其看起来像00000.mp4

2 个答案:

答案 0 :(得分:2)

两个等效的解决方案:

  1. 使用%.MTS删除原始扩展名,然后附加新扩展名:

    for file in *.MTS; do encode -i "$file" -o "${file%.MTS}.mp4"; done
    
  2. 执行搜索和替换:

    for file in *.MTS; do encode -i "$file" -o "${file/%MTS/mp4}"; done
    

    %此处将搜索锚定到文件名的末尾,以防它恰好在其他地方包含子串MTS。)

答案 1 :(得分:1)

for i in *.MTS; do encode -i "$i" -o "${i%.MTS}".mp4; done