Bash - 参数内部的不同值如果条件

时间:2012-12-30 22:18:15

标签: bash if-statement

从文件$ 2输入:1 - > 2

while read -a line; do

  if (( ${line[2]} > linesNumber )); then
  echo "Graph does not match known sites4"
    exit
  fi

done < "$2"

由于if条件中的某些原因,${line[2])的值不是2 但如果我在if之外打印值:

echo `${line[2]}`

2

1 个答案:

答案 0 :(得分:1)

什么是linesNumber?即使你放$linesNumber,它来自哪里?

如果您要跟踪行号,则需要设置它并递增它。这是我的示例程序和数据。它的例子是启发,但并不完全符合您的要求。但是,它会向您展示如何设置跟踪行号的变量,如何增加它,以及如何在if语句中使用它:

foo.txt的:

this 1
that 2
foo  4
barf 4
flux 5

该计划:

lineNum=0
while read -a line
do
    ((lineNum++))
    if (( ${line[1]} > $lineNum ))
    then
        echo "Line Number Too High!"
    fi
    echo "Verb = ${line[0]}  Number = ${line[1]}"
done < foo.txt

输出:

Verb = this  Number = 1
Verb = that  Number = 2
Line Number Too High!
Verb = foo  Number = 4
Verb = barf  Number = 4
Verb = flux  Number = 5