samedi 16 juin 2018

Conditinals in Bash

I have an array called addresses and names of equal lengths. So I used the length of 'addresses' as a condition for the for loop. My problem is with the if and elif conditions in the for loop. Basically, I want to loop through each element(of both arrays) and compare it to the corresponding regular expression I have saved as 'address_validate' and 'name-validate'

However, I get a syntax error on the lines containing the conditions(the if and two elif lines). I believe it's a syntax problem, or am I missing something?

            k=0
            tLen=${#addresses[@]}
            echo ${tLen}
            for (( i=0; i<${tlen}; i++))"
            do
                    ((k++))
                    if [[ "${addresses[${i}]}" =~ ${address_validate} && "${names[${i}]}" =~ ${name_validate} ]]
                    then
                            echo "line ${k} is valid"
                    elif  [[ ! "${addresses[${i}]}" =~ ${address_validate} ]] && [[ "${names[${i}]}" =~ ${name_validate} ]]
                    then
                            echo "line ${k} has an invalid address"
                    elif  [[ "${addresses[${i}]}" =~ ${address_validate} ]] && [[ ! "${names[${i}]}" =~ ${name_validate} ]]
                    then
                            echo "line ${k} has an invalid name"
                    else
                            echo "line ${k} is invalid"
                    fi
            done

Aucun commentaire:

Enregistrer un commentaire