外壳平等不输出任何结果
我正在尝试通过文件目录进行迭代,并提取在5:10之间具有相同ID的文件,但并非相同的文件名。该脚本适用于第一个循环,但无法在第二个输出中识别任何合适的结果(退出而没有错误)。手动传递两个合适的文件名的示例无错误地运行。我不太确定这里怎么了。
$FILES="data/*"
for f in $FILES; do
for g in $FILES; do
if [[ ${f: 5: 10} == ${g: 5: 10} ]]; then
if [[ ${g: -2} != ${f: -2} ]]; then
echo "$f"
echo "$g"
fi
fi
done
done
例如,如果包含数据/*:
data/worda_ln
数据/wordb_ln
数据/worda_ap
data/wordb_ap
脚本将输出:
data/worda_ln
数据/worda_ap
数据/wordb_ln
数据/wordb_ap
I'm trying to iterate over a file directory and extract files which have the same id in the range between 5: 10 but are not identical filenames. This script works for the first loop, but won't identify any suitable results in the second output (exits without error). Manually passing in an example of two suitable filenames runs without error. I'm not really sure what's wrong here.
$FILES="data/*"
for f in $FILES; do
for g in $FILES; do
if [[ ${f: 5: 10} == ${g: 5: 10} ]]; then
if [[ ${g: -2} != ${f: -2} ]]; then
echo "$f"
echo "$g"
fi
fi
done
done
e.g. if the data/* contained:
data/wordA_ln
data/wordB_ln
data/wordA_ap
data/wordB_ap
The script would output:
data/wordA_ln
data/wordA_ap
data/wordB_ln
data/wordB_ap
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的困难是您在同一文件上循环两次。
和
$ {var:5:10}
将包括,worda_ln
或worda_cp
。因此,这些永远不会平等。无论如何,我采取了另一种方法。获取文件名列表,您要检查的部分:
ex:
查找
找到“ data/worda_ln”。basename {}
将返回“ worda_ln”。|剪切-d'_'-f1
将返回“ worda”。|排序| uniq
将删除重复。从该可能的文件名列表中,在目录数据中进行另一个查找以列出匹配文件。
完整脚本:
The difficulty here is that you are looping twice on the same files.
And
${var: 5: 10}
will include, for exemple,wordA_ln
orwordA_cp
. Therefore these will never be equal.Anyway, I took another approach. Get a list of filenames, well the portion you want to check:
Ex:
find
found "data/wordA_ln".basename {}
will return "wordA_ln".| cut -d'_' -f1
will return "wordA".| sort | uniq
will remove duplicates.From that list of possible filenames, do another find in directory data to list the matching files.
Complete script: