循环遍历文件的各行

循环遍历文件的各行

这是我的脚本:

#! /bin/sh

for line in $(cat list);
do
    echo "[$line]: Getting my workers list..";
    var=$line-"worker1";
    echo "$var" >> "hostlistworkers.txt"
    var=$line-"worker2";
    echo "$var" >> "hostlistworkers.txt"
    var=$line-"worker3";
    echo "$var" >> "hostlistworkers.txt"
done
fi

我怎样才能改变文件第一行的处理方式?如下所示:

If(it is the first line)
  echo "$var" >> "hostlistworkers.txt"
  var=$line-"worker2";
  echo "$var" >> "hostlistworkers.txt"
  var=$line-"worker3";
Else 
  echo "[$line]: Getting my workers list..";
  var=$line-"worker1";
  echo "$var" >> "hostlistworkers.txt"
  var=$line-"worker2";
  echo "$var" >> "hostlistworkers.txt"
  var=$line-"worker3";
  echo "$var" >> "hostlistworkers.txt"

答案1

只需声明一个标志:

FIRST=0

if [ ! $FIRST ]; then
  ; do what you need
  FIRST=1
else
  ...
fi

相关内容