Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 调用bash for循环中的当前项和下一项_Linux_Bash_For Loop - Fatal编程技术网

Linux 调用bash for循环中的当前项和下一项

Linux 调用bash for循环中的当前项和下一项,linux,bash,for-loop,Linux,Bash,For Loop,我正在进行反复分析,必须在一个大型计算机集群上向批处理系统提交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} (

我正在进行反复分析,必须在一个大型计算机集群上向批处理系统提交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循环可能更容易。我准备这样做,但我不想重新编写我已经拥有的代码

更新:非常感谢你们的时间和投入!非常感谢

$ printf '%d\n' {0..10} | paste - -
0   1
2   3
4   5
6   7
8   9
10  
所以,如果你只想对直线进行插值,这样每行就可以读取两个变量

while read -r odd even; do
    …
done < <(paste - - < inputfile)
读取时-r奇偶;做
…

完成<读取xx时,我将用
替换for循环。
类似于

cat list.txt | while read line; do
  if read nextline; then
     # You have $line and $nextline
  else
     # You have garbage in $nextline and the last line of list.txt in $line
  fi
done

另一个解决方案是使用bash数组。例如,给定一个包含内容的文件
list.txt

1
2
3
4
4
5
您可以创建一个数组变量,将文件的行作为元素,如下所示:

$ myarray=(1 2 3 4 4 5)
$ echo "${myarray[2]}"
3
虽然您也可以执行
myarray=($(echo list.txt))
这可能会在空白处分割并不适当地处理其他输出,但更好的方法是:

$ IFS=$'\n' read -r -d '' -a myarray < list.txt
数组的长度由
${myarray[@]}
给出。所有索引的列表由
${!myarray[@]}
给出,您可以循环使用此索引列表:

for i in "${!myarray[@]}"; do 
    echo "${myarray[$i]} ${myarray[$(( $i + 1))]}" 
done
输出:

1 2
2 3
3 4
4 4
4 5
5

虽然您的特定用例可能有更简单的解决方案,但这将允许您访问循环中数组元素的任意组合。

此答案假设您希望您的值重叠——这意味着作为
next
给定的值在下一次迭代中变为
curr

假设您将代码封装在一个函数中,该函数在下一项存在时使用两个参数(current和next),或在最后一项上使用一个参数:

# a "higher-order function"; it takes another function as its argument
# and calls that argument with current/next input pairs.
invokeWithNext() {
  local funcName=$1
  local curr next

  read -r curr
  while read -r next; do
    "$funcName" "$curr" "$next"
    curr=$next
  done
  "$funcName" "$curr"
}

# replace this with your own logic
yourProcess() {
  local curr=$1 next=$2
  if (( $# > 1 )); then
    printf 'Current value is %q, and next item is %q\n' "$curr" "$next"
  else
    printf 'Current value is %q; no next item exists\n' "$curr"
  fi
}
完成这些定义后,您可以运行:

invokeWithNext yourProcess <list.txt

看起来你想要一个递归函数,但我不太理解你的指令。请在循环体中发布实际命令,即使它们是基本命令。请参阅,为什么要查看下一项?这需要
peek()。相比之下,查看上一项很容易——只需将其存储在变量中,并在循环中引用该变量即可。:)你想让你的两条线重叠吗?也就是说,
12
23
34
,或
12
34
56
?改变策略,不要使用当前和下一个,而是使用当前和通过一个。这是同样的结果,但更简单。问题是我们是想运行
12
34
,等等,还是
12
23
34
,等等;我对这个问题有后一种解释,但这个脚本却有前一种解释。我会用
done
代替
cat list.txt阅读时的
done
。@TomFenech:是的,这样更好,保存一个fork/exec。@CharlesDuffy:再次阅读这个问题,我认为你的解释是正确的:-)嘿。显然不是,OP真正想要的确实是以不重叠的两对进行阅读我认为这个问题应该是
01
12
23
,而不是分成两组——如果一个“下一个”项目从来没有变成“当前”项目,那么它就很难是“下一个”,是吗?@CharlesDuffy因此,当问题需要解释时,它更有趣无论如何,我认为Olof的答案是在一个更好的轨道上。
arr=($(cat foo))
是一种用于将数据加载到数组中的反模式;它将单词分割成几行,并展开球体。如果在Bash4上使用
readarray-t
mapfile-t
更好,或者
IFS=$'\n'read-r-a arr…而且,
seq
不仅不是bash的一部分,也不是POSIX的一部分,因此它完全不可移植。如果要迭代数组索引,请在“${!array[@]}”中对idx使用
;这也修复了对稀疏数组的支持,这会导致数值迭代失败。另外,对于非内置变量或环境变量的变量,不要使用所有大写名称。请参阅POSIX规范中的环境变量命名约定(第四段),并记住环境变量和shell变量共享一个名称空间。(旁白:此功能的早期版本更像是一个传统的减速机,具有指定的初始值,可以传入;在重新考虑规范时,我去掉了该功能,但任何好奇的人都可以查看编辑历史)。
Current value is 1, and next item is 2
Current value is 2, and next item is 3
Current value is 3, and next item is 4
Current value is 4, and next item is 5
Current value is 5; no next item exists