猛砸对目录
问题描述:
循环我有一个bash脚本,我想在一个目录下运行程序,使用一个文件从另一个目录中输入猛砸对目录
有几个输入文件,位于几个不同的目录,其中的每一个被用作输入的程序
的一个迭代中的文件是若干文件类型一个(包含.foo)在每个目录
的我的代码是
cd /path/to/data/
for D in *; do
# command 1
if [ -d "$D" ]
then
cd /path/to/data
# command 2
for i in *.foo
do
# command 3
done
fi
done
当我运行该脚本,输出如下
# command 1 output
# command 2 output
# command 3 output
# command 2 output
# command 2 output
# command 2 output
# command 2 output
# command 2 output
.
.
.
所以脚本做什么,我希望它究竟做一次,然后似乎
后不遍历决赛圈为什么是这样?
答
我想你以后“然后”有一个错字错误... 它更有意义的是:
then
cd /path/to/data/$D
# command 2
但是,作为cdarke建议,这是好事,避免在您的脚本中使用CD 。 你可以有相同的结果是这样的:
for D in /path/to/data; do
# command 1
if [ -d "$D" ]
then
# command 2
for i in /path/to/data/$D/*.foo
do
# command 3
done
fi
done
或者你甚至可以使用发现和避免,如果零件(更少的代码使之加快你的脚本):
for D in $(find /path/to/data -maxdepth 1 -type d)
# -type d in find get's only directories
# -maxdepth 1 means current dir. If you remove maxdepth option all subdirs will be found.
# OR you can increase -maxdepth value to control how deep you want to search inside sub directories.
你改变了目录,并改变了它不背部?你的代码不清楚,因为你使用了两次'cd/path/to/data /'。 – Cyrus
一般来说,尽量避免在脚本中使用'cd',你可以将自己打结。尽可能构建和使用完整路径名称更为容易。例如:'对于我在/路径/到/数据/ *。foo'。 – cdarke
这些命令做了什么? – Jdamian