如何在循环中初始化脚本变量?

时间:2012-03-30 13:16:25

标签: bash

我必须在for循环中初始化一个脚本变量,并从输入文件中获取不同的值,如下所示:

#!/bin/bash
VAR1=0
VAR2=0

exec < $1
while read line 
do
    #initialize VAR1 AND VAR2 taking the values from '$line' ($1 and $2)

    #and then
    echo $VAR1
    echo $VAR2

    #here do whatever with both variables...

done

如何使用$ 1初始化VAR1,使用$ 2 ??

初始化VAR2

提前致谢!!

2 个答案:

答案 0 :(得分:2)

我假设您的输入文件有2个以空格分隔的字段:

#!/bin/bash
exec < "$1"
while read VAR1 VAR2 
do
    echo $VAR1
    echo $VAR2
done

答案 1 :(得分:1)

#!/bin/bash
VAR1=$1
VAR2=$2

exec < $1
while read line 
do
    #initialize VAR1 AND VAR2 taking the values from '$line' ($1 and $2)

    #and then
    echo $VAR1
    echo $VAR2

    #here do whatever with both variables...

done

PS:$0是脚本名称。