【发布时间】:2015-05-26 14:53:21
【问题描述】:
我正在进行反复分析,必须向大型计算机集群上的批处理系统提交 5000 多个作业。
我想运行一个 bash for 循环,但同时调用当前列表项和脚本中的下一项。我不确定使用这种格式的最佳方法:
#! /bin/bash
for i in `cat list.txt`;
do
# run a bunch of code on $i (ex. Data file for 'Apples')
# compare input of $i with next in list {$i+1} (ex. Compare 'Apples' to 'Oranges', save output)
# take output of this comparison and use it as an input for the next analysis of $i (ex. analyze 'Apples' some more, save output for the next step, analyze data on 'Oranges')
# save this output as the input for next script which analyses the next item in the list {$i+1} (Analysis of 'Oranges' with input data from 'Apples', and comparing to 'Grapes' in the middle of the loop, etc., etc.)
done
在 while 循环中提供表格输入列表对我来说最简单吗?我真的不想这样做,因为我必须做一些代码编辑,虽然很小。
感谢您对新手的帮助——我浏览了整个互联网并翻阅了一堆书籍,但没有找到一个好的方法。
编辑:出于某种原因,我认为可能有一个 for 循环技巧可以做到这一点,但我猜不是;对我来说,使用表格输入进行 while 循环可能更容易。我准备这样做,但我不想重写我已经编写的代码。
更新:非常感谢大家的时间和投入!非常感谢。
【问题讨论】:
-
看起来你想要一个递归函数,但我不太明白你的指示。请在循环主体中发布实际命令,即使它们是基本的。
-
为什么要看下一个项?这需要
peek()-type 功能,而 bash 没有。相比之下,查看 previous 项很容易——只需将其存储在一个变量中并在循环中引用该变量即可。 :) -
您希望您的配对重叠吗?即
1 2、2 3、3 4,还是1 2、3 4、5 6? -
改变你的策略,不要使用current和next,而是current and pass one。结果相同,但更容易。