'shell script how to compare two comma separated variable and loop it once

I am passing 2 arguments in shell script input=data1,data2,data3 and git_path=path1,path2,path3 I want to perform some code only when input=data1 git_path=path1 then input=data2 git_path=path2 and so on. I separated the comma seperated varaible, but while running it is iterating 9 times. I just want to iterate 3 times. this is my shell script:

inno=`echo $input|tr ',' '\n'|wc -l`
echo -e "inputs are $inno"
gitno=`echo $gitPath|tr ',' '\n'|wc -l`
echo -e "git paths are $gitno"

if [ "$inno" -eq "$gitno" ];
then
  echo -e "in if loop"
  for i in $(echo $input | sed "s/,/ /g")
  do
    for j in $(echo $gitPath | sed "s/,/ /g")
    do
      # call your procedure/other scripts here below
      echo "input is $i and gitpath is $j"
    done
   done
else
   echo -e "\n number of inputs don't match number of git path"
fi

The output I am getting is

input is data1 and gitpath is path1
input is data1 and gitpath is path2
input is data1 and gitpath is path3
input is data2 and gitpath is path1
input is data2 and gitpath is path2
input is data2 and gitpath is path3
input is data3 and gitpath is path1
input is data3 and gitpath is path2
input is data3 and gitpath is path3

while I want the output as:

input is data1 and gitpath is path1
input is data2 and gitpath is path2
input is data3 and gitpath is path3


Solution 1:[1]

In bash, using two arrays and a C style for loop:

IFS=, read -ra array_input <<< "$input"
IFS=, read -ra array_git_path <<< "$git_path"

input_count=${#array_input[*]}
if ((input_count == ${#array_git_path[*]})); then
    for ((i = 0; i < input_count; ++i)); do
        echo "input is ${array_input[i]} and gitpath is ${array_git_path[i]}"
    done
fi

Alternatively:

if [[ ${input//[^,]} = "${git_path//[^,]}" ]]; then
    input+=,
    while [[ $input ]]; do
        echo "input is ${input%%,*} and gitpath is ${git_path%%,*}"
        input=${input#*,}
        git_path=${git_path#*,}
    done
fi

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1