bash - 读取文件名中的空格时
问题描述
在这个.ogg
文件上
$ tree
.
├── Disc 1 - 01 - Procrastination.ogg
├── Disc 1 - 02 - À carreaux !.ogg
├── Disc 1 - 03 - Météo marine.ogg
└── mp3
我尝试使用while
循环将 ffmpeg 转换为 mp3,在文件名中保留空格::
$ ls *.ogg | while read line; do ffmpeg -i "$line" mp3/"$line".mp3 ; done
但我得到这个错误::
$ ls *.ogg | while read line; do ffmpeg -i "$line" mp3/"$line".mp3 ; done
...
Parse error, at least 3 arguments were expected, only 0 given
in string ' 1 - 02 - À carreaux !.ogg' ...
...
此报告bash ffmpeg find and space in filenames即使看起来相似,也适用于更复杂的脚本并且没有答案。
此ffmpeg 不适用于具有空格的文件名,仅当输出为 http:// URL 时才修复它
解决方案
用于find -print0
获取 NUL 分隔的文件列表,而不是解析ls
输出,这绝不是一个好主意:
#!/bin/bash
while read -d '' -r file; do
ffmpeg -i "$file" mp3/"$file".mp3 </dev/null
done < <(find . -type f -name '*.ogg' -print0)
您也可以使用简单的 glob 来执行此操作:
shopt -s nullglob # make glob expand to nothing in case there are no matching files
for file in *.ogg; do
ffmpeg -i "$file" mp3/"$file".mp3
done
看:
推荐阅读
- python - 动态启用和禁用文本框 Python Tkinter
- json - json模式中的多个条件必填字段
- ios - SwiftUI contextMenu 仅在暗模式下
- recaptcha - Recaptcah V3 站点密钥超出限制
- java - 如何使用 JAVA 在 android 的 RecyclerView 中的其他活动中显示不同的数据
- python-3.x - 一种在 Python 中不使用 time.sleep() 来创建时间延迟的方法?
- msg - msg.exe 在邮槽方面是如何工作的?
- powershell - Powershell 输出抑制
- javascript - 使用 revved css 更新 _document.twig,使用 Gulp 更新 js
- javascript - 如何修复错误“未知的自定义元素:
“?