【发布时间】:2018-07-09 16:27:21
【问题描述】:
我有以下脚本,但它有一个错误。我正在尝试将所有文件合并为一个大文件。从命令行 cat commant 工作正常,内容被打印到重定向文件。从脚本它有时可以工作,但其他时间不行。我不知道为什么它的行为异常。请帮忙。
#!/bin/bash
### For loop starts ###
for D in `find . -type d`
do
combo=`find $D -maxdepth 1 -type f -name "combo.txt"`
cat $combo >> bigcombo.tsv
done
这是bash -x app.sh的输出
++ find . -type d
+ for D in '`find . -type d`'
++ find . -maxdepth 1 -type f -name combo.txt
+ combo=
+ cat
^C
更新: 以下对我有用。路径有问题。我仍然不知道是什么问题,所以欢迎回答。
#!/bin/bash
### For loop starts ###
rm -rf bigcombo.tsv
for D in `find . -type d`
do
psi=`find $D -maxdepth 1 -type f -name "*.psi_filtered"`
# This will give us only the directory path from find result i.e. removing filename.
directory=$(dirname "${psi}")
cat $directory"/selectedcombo.txt" >> bigcombo.tsv
done
【问题讨论】:
-
cat工作正常,考虑到$combo的未引用扩展。主要问题是您的find命令并不总是找到至少一个文件。您可能只想要find . -maxdepth 2 -type f -name "combo.txt" -exec cat {} + > bigcombo.tsv之类的东西。 -
另外,为什么要使用反引号和“新”形式的命令替换,即
directory=$(dirname "${psi}")?directory=$(...)形式是首选形式,因此加入 90 年代并停止使用反引号进行命令替换 ;-) 即psi=`find $D -maxdepth 1 -type f -name "*.psi_filtered"`应该是psi=$(find $D -maxdepth 1 -type f -name "*.psi_filtered")。祝你好运。