LinuxQuestions.org
Help answer threads with 0 replies.
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 09-07-2018, 10:19 AM   #1
bluethundr
Member
 
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 144

Rep: Reputation: 15
Post While Loop With 2 Values in Bash


Hello,

I have a script that reports on aws keys older than 90 days. It loops through all AWS environments with this file:

Code:
aws_env_list="source_files/aws_environments_all.txt"
This works well. But I also want to loop through a list of AWS account numbers defined in this file:

Code:
aws_account_numbers="source_files/aws_account_numbers.txt"
The account numbers should line up with the account names.

How do I add the aws account numbers to the while loop in this function?

Code:
create_user_keys_list() {
  echo
  echo "****************************************"
  echo "*        Create User Keys Lists      *"
  echo "****************************************"
  aws_user_info
  printf "Enter user's first name: "
  read -r first_name
  printf "Enter the user's email address: "
  read -r email_address
  echo
  fusion_link='<a href="https://company.service-now.com/myfusion?id=sc_cat_item&sys_id=7949c77e6ffc310048c83cb44b3ee4a7">Change AWS Configuration</a>'
  mail_body="<font size="3" face="Callibri" color="black">Hello $first_name, <br /><br />Enclosed, please find a list of AWS Access keys that need to be replaced for user name: $aws_user_name.<br /><br />The list is from all company AWS acccounts that have access keys that are older than 90 days for this user.<br /><br />All AWS Keys need to be replaced if they are older than 90 days.<br /><br />You can open up a fusion ticket using this catalog item: $fusion_link to have Cloud Ops rotate or destroy your key.<br /><br />Please attach this spread sheet to the ticket.<br /><br />Regards,<br />Cloud Ops</font>"
  ofile=source_files/aws_user_keys/"$aws_user_name"-aws-access-keys.csv
  echo "User Name, Access Key,Date Created,Last Used,Days Old,AWS Account" > $ofile
  while IFS= read -r aws_key
  do
    user_lives_here=$(aws iam get-user --user-name "$aws_user_name" --profile="$aws_key" 2> /dev//null  | jq -r '.User.UserName')
    if [[ -z "$user_lives_here" ]]; then
      printf "AWS user name: %s does not exist in AWS account: %s\\n" "$aws_user_name" "$aws_key"
    else
      process_keys "$aws_key"
      if  [ "$key1dtSec" -lt "$taSec" ] || [ "$key2dtSec" -lt "$taSec" ]; then
        echo
        echo "********************************************************"
        echo "*        List $aws_user_name's keys in $aws_key        *"
        echo "********************************************************"
        echo
        printf "%s has the following keys in %s:\\n" "$aws_user_name" "$aws_key"
        echo; echo
        if  [ "$key1dtSec" -lt "$taSec" ]; then
          printf "%s created on %s\\nThis key is %s days old and needs to be replaced.\\nKey was last used on: %s.\\n" "$user_access_key1" "$key1_date_created." "$key1AgeDays" "$key1_last_used" 
          echo "$aws_user_name,$user_access_key1,$key1_date_created,$key1_last_used,$key1AgeDays,$aws_key" >> $ofile
          echo
        elif [ "$key2dtSec" -lt "$taSec" ]; then
          printf "%s created on %s\\nThis key is %s days old and needs to be replaced.\\nKey was last used on: %s.\\n" "$user_access_key2" "$key2_date_created." "$key2AgeDays" "$key2_last_used"
          echo "$aws_user_name,$user_access_key2,$key2_date_created,$key2_last_used,$key2AgeDays,$aws_key" >> $ofile
          echo
        else
          echo "**********************************************************************"
          echo "*  No keys older than 90 days for $aws_user_name in $aws_key         *"
          echo "**********************************************************************"
        fi
      fi
    fi  
  done < "$aws_env_list"
  echo "sending mail"
  echo $mail_body | mutt -e  'set from=cloudops@noreply.company.com realname="Cloud Ops" content_type=text/html' -a "$ofile" -s "AWS Key Rotation Needed" -- $email_address
  /bin/rm -v $ofile
  end_banner
  echo
}
 
Old 09-07-2018, 10:38 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I found this on the net, just need to figure out the rest
Code:
while true
do
read -r f1 <&3 || break
read -r f2 <&4 || break

printf 'f1: %s\n' "$f1"
printf 'f2: %s\n' "$f2"
done 3<file1 4<file2
 
Old 09-07-2018, 07:33 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
In positive logic
Code:
while
  IFS= read -r f1 <&3 && IFS= read -r f2 <&4
do
  printf 'f1: %s\n' "$f1"
  printf 'f2: %s\n' "$f2"
done 3<file1 4<file2
 
  


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
Loop Through Array Values and Assign At The Same Time in Bash bluethundr Linux - Newbie 1 08-28-2018 12:58 PM
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] while loop termination and read values in c language UnixCube Programming 21 04-09-2012 10:54 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
Comparing Multiple Values in while loop rahulruns Programming 5 10-27-2009 05:59 AM

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

All times are GMT -5. The time now is 06:36 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