user1303306
user1303306

Reputation: 3

How to initialize script variables in a loop?

I have to initialize a script variable in a for loop with different values taken from a input file as following:

#!/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

How to initialize VAR1 with $1 and VAR2 with $2 ??

Thanks in advance!!

Upvotes: 0

Views: 2021

Answers (2)

glenn jackman
glenn jackman

Reputation: 246774

I assume your input file has 2 whitespace-delimited fields:

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

Upvotes: 2

Teja
Teja

Reputation: 13534

#!/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 is the script name.

Upvotes: 1

Related Questions