0

I have the following array stored as svc_list delimited by a ':'

declare -a svc_list=Scalability :Warehouse Cloud Solution :Log Analyis :Monitor and Scale :

I am trying to split it with the following bash script (

IFS=':'
for svc in "${svc_list[@]}"
do
  echo $svc
done 

When I execute the script, I only get Scalability.

Can some please let me know what I am doing wrong.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Where is `svc_list` defined? – JNevill Jan 30 '18 at 21:22
  • svc_list is defined before IFS as declare -a svc_list=Scalability :Warehouse Cloud Solution :Log Analyis :Monitor and Scale : –  Jan 30 '18 at 21:28
  • Oh, my bad. Is your question actually "how do I assign an array in bash?" – that other guy Jan 30 '18 at 21:30
  • No. I actually want to split the string (which is actually the output of a command that I have added : character as the delimiter). Now I want to extract each one of the strings delimited by (space and :). How do I do that in a bash script. When I execute the above script, I get only the first string. –  Jan 30 '18 at 21:37
  • When you said how `svc_list` is defined, is that the exact line you have in your file? If not, what is the exact line? (Please do not pretty it up or post a line that you believe is basically the same thing. Please copy-paste it from your file.) – that other guy Jan 30 '18 at 21:41
  • When you assign a string to a variable in bash, you should quote it, especially if it has the default IFS characters present in the string (in your case a space): `svc_list="Scalability :Warehouse Cloud Solution :Log Analyis :Monitor and Scale :"` otherwise only `Scalability` makes it into your variable and the rest is lost to the bash monster. – JNevill Jan 30 '18 at 21:46

1 Answers1

1

That's not the correct syntax to assign an array, you need to put all the array elements in ().

But if you want to split the string into an array using : as the separator, you should start with a string:

svc_string='Scalability :Warehouse Cloud Solution :Log Analyis :Monitor and Scale :'

Then use IFS to split it:

IFS=':'
svc_array=($svc_string)
Barmar
  • 741,623
  • 53
  • 500
  • 612