我在通配符上遇到了这个"问题",我无法弄清楚为什么会这样。
以下示例:
#!/bin/bash
basefolder=/tmp/
rm $basefolder"processed/*.txt*"
这导致了一个错误消息" rm find/tmp/processed/processed/ .txt "。当然,这是不正确的,因为此文件夹中有文件,当我从错误消息中复制并经过该文件夹时,它会正确删除它。
适用于循环
#!/bin/bash
basefolder=/tmp/
for file in $basefolder"processed/*.txt*"
...
将行不通,我的解决方法是以下
#!/bin/bash
basefolder=/tmp/
filepattern=$basefolder"processed/*.txt*"
for file in $filepattern
...
另一种方法是设置基本文件夹/tmp,然后使用$ basefolter/processed/ .txt (这可以使用),但是我更好奇为什么这个问题甚至存在。p>谁能给我解释?
问题是关于引用:
ls /tmp/"foo*"
表示列出了名为/tmp/foo*
的文件,它将不匹配/tmp/foobar
或/tmp/foo
ls /tmp/foo*
(无引号)匹配/tmp/foo
和/tmp/foobar
所以,这些都应该工作:
basefolder=/tmp/
rm ${basefolder}processed/*.txt*
# I like this better, no need for ${}:
basefolder=/tmp
rm $basefolder/processed/*.txt*
# the loop:
basefolder=/tmp
for file in $basefolder/processed/*.txt*
而无需保护模式*.txt*
。因此,脚本应该是:
#!/bin/bash
basefolder=/tmp
/bin/rm "$basefolder"/processed/*.txt*