我想在bash中映射变量的索引

时间:2014-12-08 09:19:28

标签: linux bash

NList=(5)
VList=(1)
FList=("input/flower1.jpg" "input/flower2.jpg" "input/flower3.jpg" "input/flower4.jpg")
IList=("320X240"            "640X480"           "1280X960"          "1920X1200")
SList=(2 3)
for VM in ${VList[@]}; do

    for ((index=0; index < ${#FList};)) do
        file=$FList[$index]
        image_size=$IList[$index]
        width=`echo $image_size|cut -d "X" -f1`
        height=`echo $image_size|cut -d "X" -f2`
        for scale_factor in ${SList[@]}; do
            for users in ${NList[@]}; do 
                echo "V: $VM, " "F: $file, " "S: $scale_factor, " "I: $width $height , " "N: $users"
                for i in `seq 1 $users` ; do
                    ./sr_run_once.sh $file $width $height $scale_factor &
                done
                wait
            done # for users            
        done # for scale_factor
    done # for index
done # for VM
exit 0

我希望Flistsay flower1应该只处理320*240的图像大小 因此引入了变量索引......但无法解决它。它给出了一个错误。

1 个答案:

答案 0 :(得分:1)

那里有一些语法错误。我在评论中注释了它们:

#!/bin/bash
# ^-- shebang for bash because you use bash extensions.    

NList=(5)
VList=(1)
FList=("input/flower1.jpg" "input/flower2.jpg" "input/flower3.jpg" "input/flower4.jpg")
IList=("320X240"            "640X480"           "1280X960"          "1920X1200")
SList=(2 3)

for VM in ${VList[@]}; do
                           #  +-- You've used the right syntax above, you have
                           #  |   to use it here as well.
                           #  |
                           #  |             +--- and here you have to increment
                           #  v             v    the index to move forward.
    for ((index=0; $index < ${#FList[@]}; ++index)) do

        #     v--- the braces here are not optional.
        file=${FList[$index]}
        image_size=${IList[$index]}
        width=`echo $image_size|cut -d "X" -f1`
        height=`echo $image_size|cut -d "X" -f2`
        for scale_factor in ${SList[@]}; do
            for users in ${NList[@]}; do 
                echo "V: $VM, " "F: $file, " "S: $scale_factor, " "I: $width $height , " "N: $users"
                for i in `seq 1 $users` ; do
                    ./sr_run_once.sh $file $width $height $scale_factor &
                done
                wait
            done # for users            
        done # for scale_factor
    done # for index
done # for VM
exit 0