如何处理将其名称中的文件传递到命令时的文件
我是新手bash和编写shell脚本以在音乐文件中添加一些元数据,并且在文件名中有一些空间
FFMPEG_TARGET_COMMAND="\"$INPUT_DIRECTORY\" $SOME_METADATA_ARG \"$OUTPUT_DIRECTORY\""
echo $FFMPEG_TARGET_COMMAND
> "/user/musicEx/Dream In Drive.mp3" \
-metadata album="soundtrack" \
-codec copy "/user/output/Dream In Drive.mp3"
ffmpeg -i $FFMPEG_TARGET_COMMAND
,我得到了“/user/user/musicex/dream:没有这样的文件
目录
ffmpeg -i "/user/musicEx/Dream In Drive.mp3" \
-metadata album="soundtrack" \
-codec copy "/user/output/Dream In Drive.mp3"
或 想知道为什么我已经在$ ffmpeg_target_command中逃脱了
报价
我
COMMAND="ffmpeg -i \"/user/musicEx/SCARLET NEXUS.mp3\" -codec copy \"/user/output/SCARLET NEXUS.mp3\""
。 $命令
因此,在脚本中,如何处理名称中有空格的文件?
I'm new to bash and writing a shell script to add some metadata to music files and there are some spaces in filename
FFMPEG_TARGET_COMMAND="\"$INPUT_DIRECTORY\" $SOME_METADATA_ARG \"$OUTPUT_DIRECTORY\""
echo $FFMPEG_TARGET_COMMAND
> "/user/musicEx/Dream In Drive.mp3" \
-metadata album="soundtrack" \
-codec copy "/user/output/Dream In Drive.mp3"
ffmpeg -i $FFMPEG_TARGET_COMMAND
and I got "/user/musicEx/Dream: No such file or directory
But if I execute in shell by typing
ffmpeg -i "/user/musicEx/Dream In Drive.mp3" \
-metadata album="soundtrack" \
-codec copy "/user/output/Dream In Drive.mp3"
this will work
I wonder why. I've already escape the quote in $FFMPEG_TARGET_COMMAND. I tried to escape space character, but it didn't work too.
also, I tried to put all of them into one variable:
COMMAND="ffmpeg -i \"/user/musicEx/SCARLET NEXUS.mp3\" -codec copy \"/user/output/SCARLET NEXUS.mp3\""
and I execute it by $COMMAND
So, in script, how to deal with files that have spaces in their names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 bash 数组。
正确引用变量扩展以禁止分词扩展。使用 shellcheck 检查您的脚本。重要的不是您在变量中放入的内容,而是您如何使用变量。阅读 https://mywiki.wooledge.org/BashFAQ/020 并
https://mywiki.wooledge.org/BashFAQ/050 。
没有理由大喊大叫。对于本地非导出变量,优先使用小写变量。
Use bash arrays.
Properly quote variable expansions to prohibit word splitting expansion. Check your scripts with shellcheck. It's not what you put in the variable, it's how you use the variable. Read https://mywiki.wooledge.org/BashFAQ/020 and
https://mywiki.wooledge.org/BashFAQ/050 .
No reason to SHOUT_THAT_MUCH. Prefer to use lower case variables for local non-exported variables.