我有一个包含国家/地区文件夹的目录,其中可能存在名称翻倍的文件夹:
en_en es_es fr_fr de_de
我编写了一个脚本来删除重复项:
for code in en de es fr it nl pt tr; do
if [[ -d $code_$code ]]; then
mv "${code}_${code}" "${code}"
fi
end
但这是行不通的-if
永远不会执行下面的代码(我也尝试在其中放置代码echo
)。但是,以下工作原理:
if [[ -d es_es ]]; then
echo "foo"
end
我正在使用(log)bash 3.2。
因为$code_$code
正在寻找code_
+ code
,同时要检查code
+ _
+ code
。为此,请使用花括号:${code}_${code}
。
for code in en de es fr it nl pt tr; do
if [[ -d "${code}_${code}" ]]; then # <---- here (note also it is good to quote)
mv "${code}_${code}" "${code}"
fi
end
发生这种情况是因为_
可以在变量名中出现,因此在调用它们时使用大括号来确切定义名称。
参见另一个示例:
$ this=3
$ that=4
$ echo $this_$that
4
$ echo ${this}_${that}
3_4
$ this_=5
$ echo $this_$that
54
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句