You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ruby脚本中向Shell脚本传递含空格的文件名?

Ruby调用Shell传递含空格文件名的问题

我想在Ruby脚本里调用Shell脚本并传递一个带空格的文件名,试了好几种方法都不行,以下是部分尝试:

尝试1:手动转义空格

filename = "/Users/gscar/Pictures/_Photo Processing Folders/Watched folder for import to Photos/2024.03.25-16.09.59.gs.O.orf"
filename = filename.gsub(' ', '\ ')
'exiftool -Camera:DriveMode #{filename}'

尝试2:硬编码转义后的路径

result = %x{exiftool -Camera:DriveMode /Users/gscar/Pictures/_Photo\ Processing\ Folders/Watched\ folder\ for\ import\ to\ Photos/2024.03.25-16.09.59.gs.O.orf}

执行后提示:

8 files could not be read

尝试3:用引号包裹路径

system 'exiftool -Camera:DriveMode "/Users/gscar/Pictures/_Photo Processing Folders/Watched folder for import to Photos/2024.03.25-16.09.59.gs.O.orf"'

执行后无输出。

命令行直接执行正常

直接在终端运行以下两种命令都能正常返回结果:

➜ exiftool -Camera:DriveMode "/Users/gscar/Pictures/_Photo Processing Folders/Watched folder for import to Photos/2024.03.25-16.09.59.gs.O.orf"
Drive Mode                      : Single Shot; Electronic shutter
➜ exiftool -Camera:DriveMode /Users/gscar/Pictures/_Photo\ Processing\ Folders/Watched\ folder\ for\ import\ to\ Photos/2024.03.25-16.09.59.gs.O.orf
Drive Mode                      : Single Shot; Electronic shutter

最终尝试的错误代码

我想实现的功能类似下面的代码,但执行报错:

filename = "/Users/gscar/Pictures/_Photo Processing Folders/Watched folder for import to Photos/2024.03.25-16.09.59.gs.O.orf"
gsubfilename = filename.gsub(' ', '\ ')
`"exiftool -Camera:DriveMode #{gsubfilename}"`

报错信息:

sh: exiftool -Camera:DriveMode /Users/gscar/Pictures/_Photo\ Processing\ Folders/Watched\ folder\ for\ import\ to\ Photos/2024.03.25-16.09.59.gs.O.orf: No such file or directory

但这条命令在Shell里执行完全正常。

疑问

请问问题出在哪里?另外,mini_exiftool似乎无法处理DriveMode字段。

内容的提问来源于stack exchange,提问作者Greg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:55:18