Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-28-2018, 10:39 AM   #1
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 142

Rep: Reputation: 15
Post Loop Through Array Values and Assign At The Same Time in Bash


I am allowing users of our AWS accounts to attach multiple security groups to an AWS instance with a bash script I'm writing.

These lines get the user to specify how many SG's they want to use:

printf "Enter number of security groups to add: "
  read -r sg_answer

while [ "$sg_startvalue" -le "$sg_answer" ]; do
      read -rp "Enter Security Group ID $sg_startvalue : " security_group && security_groups+=("$security_group")

And these lines add the security groups to the instance:

for aws_sg in "${security_groups[@]}"; do
         printf "Adding %s to %s\\n" "$tag_instance_id" "$aws_sg"
         aws ec2 modify-instance-attribute --instance-id "$tag_instance_id" --groups "$aws_sg" --profile="$aws_key"
         sleep 5
Problem is this AWS command (aws ec2 mofify-instance-attribute) needs to add ALL of the security groups at the same time. I won't know how many security groups the user is adding ahead of time.

If you run that command in a loop (as I'm doing here) each time that command runs it replaces the $aws_sg value that preceded it.

How do I gather the arbitrary number of aws_sg values into one line that I can use with aws ec2 modify-instance-attribute?

Last edited by bluethundr; 08-28-2018 at 11:08 AM.
Old 08-28-2018, 01:58 PM   #2
LQ Addict
Registered: Dec 2013
Posts: 12,879
Blog Entries: 9

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
i'm not sure i understand, but to literally answer "Loop Through Array Values and Assign At The Same Time in Bash":

for ((i=0;i<${#array[@]};i++)); do


bash, bash scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to assign null values to an array in C++ newinubuntu Programming 5 05-08-2015 01:35 PM
trouble with a FOR loop not adding values from an array like it should ashevillan Programming 2 05-12-2013 06:05 PM
[SOLVED] how to assign output from bash command ps to array ? masuch Linux - Newbie 3 10-06-2012 10:35 AM
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 04:41 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 10:38 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration