Bash csh循环遍历2个列表

时间:2016-01-29 19:28:54

标签: bash csh

在csh或任何其他mac友好脚本语言中,我想运行某种形式的循环,读取2个列表。需要一些语法帮助。

例如:

variable1 = (1, 2, 3, 4, ...)
variable2 = (one, two, three, four, ...)

Foreach number ($variable1)

echo  "{$variable1} equals {$variable2}"

end

当我尝试时,循环中无法识别第二个variable2

2 个答案:

答案 0 :(得分:2)

#!/bin/bash

variable1=(1 2 3 4) 
variable2=(one two three four)

for ((i=0;i<${#variable1[@]};i++)); do
  echo "${variable1[$i]} equals ${variable2[$i]}"
done

输出:

1 equals one
2 equals two
3 equals three
4 equals four

答案 1 :(得分:1)

请试试这个:

#! /bin/csh
set variable1 = (1 2 3 4) 
set variable2 = (one two three four)
set i = 1
foreach x ( $variable1 )
    echo "$x equals $variable2[$i]"
    @ i = $i + 1
end

结果是:

1 equals one
2 equals two
3 equals three
4 equals four