Home > Software engineering >  Get the value from the file & add the values in an array...But getting values multiple times
Get the value from the file & add the values in an array...But getting values multiple times

Time:02-26

#!/bin/bash

while IFS= read -r -a line; do

for raw_value in $line; do

raw_value_1=`echo $line | sed "s/,/ /g" | head -n1 | awk '{print $1;}'`
raw_value_2=`echo $line | sed "s/,/ /g" | head -n1 | awk '{print $2;}'`
raw_value_3=`echo $line | sed "s/,/ /g" | head -n1 | awk '{print $3;}'`
REPO=`echo $raw_value_1`
BRANCH=`echo $raw_value_2`
TAG=`echo $raw_value_3`

echo $REPO
echo $BRANCH
echo $TAG

done

arrVar=("$REPO" "$BRANCH" "$TAG")

for value in "${arrVar[@]}"
do

cmd="echo ${arrVar[@]}"
           cmd_out=$($cmd)
           status=$?

           if [[ $status -eq 0 ]]
           then
echo "First Repo is ${arrVar[0]}; Branch is ${arrVar[1]}"
     echo ${arrVar[2]}
fi
done
done < /var/lib/jenkins/Jack/test

My File content -

Jack,master,1.0.1 Tom,master,1.0.2

Desired output -

Repo is Jack; Branch is master Tag is 1.0.1

Repo is Tom; Branch is master 1.0.2

Current output -

Jack master 1.0.1 Repo is Jack; Branch is master 1.0.1 Repo is Jack; Branch is master 1.0.1 Repo is Jack; Branch is master 1.0.1 Tom master 1.0.2 Repo is Tom; Branch is master 1.0.2 Repo is Tom; Branch is master 1.0.2 Repo is Tom; Branch is master 1.0.2

CodePudding user response:

It's not entirely clear what your purpose is, but given this input

cat file.txt
Jack,master,1.0.1
Tom,master,1.0.2

here's how to parse the data with three different action scenarios:

while IFS=, read -r repo branch tag; do
  
  # Printing your "desired output"
  printf 'Repo is %s; Branch is %s\nTag is %s\n\n' \
    "$repo" "$branch" "$tag"
  
  # Populating an array (three more elements per input row)
  arrVar =("$repo" "$branch" "$tag")
  
  # the git command from your answer post
  git clone -b "${branch}" "ssh://[email protected]:8080/test_dir/${repo}.git"
  git tag "${tag}"

done < file.txt

The printing scenario would output

Repo is Jack; Branch is master
Tag is 1.0.1

Repo is Tom; Branch is master
Tag is 1.0.2

CodePudding user response:

Thanks for the quick help @pmf. But I've to use this arrVar to clone all Repo names (Jack, Tom & so on...). Clone all the Repos one by one which is listed in the file.txt file. I tried this But not working -

code

#!/bin/bash
while IFS=, read -r repo branch tag; do
  printf 'Repo is %s; Branch is %s\nTag is %s\n\n' \
    "$repo" "$branch" "$tag"
arrVar =("$repo" "$branch" "$tag")
for value in "${arrVar[@]}"
do
git clone -b "${arrVar[@]}" ssh://[email protected]:8080/test_dir/"${arrVar[0]}".git
git tag ${arrVar[3]}
done
done < file.txt
  • Related