2012-03-30 75 views
0

我有一個初始化腳本變量的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和VAR2以$ 2&

在此先感謝!

回答

2

我假設你輸入的文件有2個空格分隔的字段:

#!/bin/bash 
exec < "$1" 
while read VAR1 VAR2 
do 
    echo $VAR1 
    echo $VAR2 
done 
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是腳本名稱。