LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 08-28-2018, 09:39 AM   #1
bluethundr
Member
 
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 144

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


Hello,

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:

Code:
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")
      sg_startvalue=$((sg_startvalue+1))
    done

And these lines add the security groups to the instance:

Code:
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
      done
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 10:08 AM.
 
Old 08-28-2018, 12:58 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

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

Code:
for ((i=0;i<${#array[@]};i++)); do
    array[i]="somevalue"
done
 
  


Reply

Tags
bash, bash scripting



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 12:35 PM
trouble with a FOR loop not adding values from an array like it should ashevillan Programming 2 05-12-2013 05:05 PM
[SOLVED] how to assign output from bash command ps to array ? masuch Linux - Newbie 3 10-06-2012 09:35 AM
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 03:41 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:39 PM.

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