非破折号相关的错误替换错误
Non-dash related bad substitution error
我正在尝试编写一个 bash 脚本来执行查找和提取特定文件类型的其他地方。到目前为止,我想出了以下脚本:
find ./to_compress -type f -iname "*.tar" -mindepth 1 -maxdepth 1 -exec mv {} ./compressed/${{}##*/}
然而 bash 抱怨 ${{}##*/}
是一个错误的替代
bash: ./compressed/${{}##*/}: bad substitution
一些谷歌搜索表明这可能是由于 -exec 调用 dash
而不是 bash
,我用
检查过
find ./to_compress -type f -iname "*.tar" -mindepth 1 -maxdepth 1 -exec echo [=15=] {} \;
以 bin/bash xxxx
响应,表明 bash 确实被 exec 命令调用。
此错误可能是由于什么原因造成的?我如何调解它才能单独获取文件名并删除尾随目录?
find
命令的-exec
标志的{}
占位符不是变量。
您只能对变量进行参数扩展。
该问题与 shell 为 dash
或 bash
.
无关
在这种情况下,你可以做的是 运行 一个 sh
和 -exec
,
并将 {}
作为参数传递给子 shell,如下所示:
find ... -exec bash -c 'mv "" "./compressed/${1##*/}"' -- {} \;
我正在尝试编写一个 bash 脚本来执行查找和提取特定文件类型的其他地方。到目前为止,我想出了以下脚本:
find ./to_compress -type f -iname "*.tar" -mindepth 1 -maxdepth 1 -exec mv {} ./compressed/${{}##*/}
然而 bash 抱怨 ${{}##*/}
是一个错误的替代
bash: ./compressed/${{}##*/}: bad substitution
一些谷歌搜索表明这可能是由于 -exec 调用 dash
而不是 bash
,我用
find ./to_compress -type f -iname "*.tar" -mindepth 1 -maxdepth 1 -exec echo [=15=] {} \;
以 bin/bash xxxx
响应,表明 bash 确实被 exec 命令调用。
此错误可能是由于什么原因造成的?我如何调解它才能单独获取文件名并删除尾随目录?
find
命令的-exec
标志的{}
占位符不是变量。
您只能对变量进行参数扩展。
该问题与 shell 为 dash
或 bash
.
在这种情况下,你可以做的是 运行 一个 sh
和 -exec
,
并将 {}
作为参数传递给子 shell,如下所示:
find ... -exec bash -c 'mv "" "./compressed/${1##*/}"' -- {} \;