0

I have a bash script that I'm writing that rotates a user's AWS access keys. All the actions it takes are in functions. Leaving out the detail of each function (that's not important for this question), I'm showing the basic layout of my script below.

How do I ask the user if he want's to check another AWS user's access keys? And then begin again at the top until he says 'no'?

This is the basic outline of my script:

init() { 
     echo "Enter your script user name:"
     read -r user_name
     ........
 }

main() {
  # AWS Lab
  if [ "$accountnumber" == 123456789 ]; then
     "$user_action"
     return
  ...............
  fi

}

choose_account() {

  echo "*********************************************"
  echo "*      Choose an AWS Account            *"
  echo "*********************************************"
  .......


}

choose_action() {

  echo "*********************************************"
  echo "*         Choose an Action                  *"
  echo "*********************************************"

   .......................


}


aws_user_info() {


   echo "Enter an AWS IAM user name: "
   read -r aws_user_name
   .....
}

process_keys() {

   # Get the IAM user access key
   user_access_key1=$(aws iam list-access-keys --user-name "$aws_user_name" --profile "$aws_key" --output text --query 'AccessKeyMetadata[*].[AccessKeyId,CreateDate]' | awk 'NR==1 { print $1 }')
   user_access_key2=$(aws iam list-access-keys --user-name "$aws_user_name" --profile "$aws_key" --output text --query 'AccessKeyMetadata[*].[AccessKeyId,CreateDate]'  | awk 'NR==2 { print $1 }')
   ......

}

send_email() {

  if  [ "$key1dtSec" -lt "$taSec" ] || [ "$key2dtSec" -lt "$taSec" ]; then
  echo "*********************************************"
  echo "*        Send Email to AWS IAM Users         *"
  echo "*********************************************"
  .............
  fi

}

list_keys() {

   if  [ "$key1dtSec" -lt "$taSec" ]; then
       printf "%s has the following keys:\\n" "$aws_user_name"
      echo; echo
    ...............
    fi

}

deactivate_keys() {

       if [ "$key1dtSec" -lt "$taSec" ]; then
            echo "Deactivate $user_access_key1 for $aws_user_name? Enter Y or N:"
            read -r deactivate_key
       ..............
       fi

}

delete_keys() {


   if [ "$key1dtSec" -lt "$taSec" ]; then
        echo "Destroy $user_access_key1 for $aws_user_name? Enter Y or N:"
            read -r destroy_key
   ............
   fi

}

rotate_keys() {

         if [ "$key1dtSec" -lt "$taSec" ]; then
         echo "Rotate $user_access_key1 for $aws_user_name? Enter Y or N:"
            read -r rotate_key
        ...........
        fi

}

  init
  aws_user_info
  choose_account
  process_keys
  choose_action
  main "$@"

1 Answer 1

1

Use a loop.

again=y
until [ "$again" = n ]; then
    # Do stuff here

    printf 'Go again? (y/n) ' >&2
    IFS= read -r again
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.