根据用户输入重新启动bash脚本

时间:2018-07-23 17:52:01

标签: bash

我正在编写一个bash脚本,该脚本可旋转用户的AWS访问密钥。它采取的所有操作都在函数中。省略了每个函数的细节(对于这个问题来说并不重要),下面我将显示脚本的基本布局。

如何询问用户是否要检查另一个AWS用户的访问密钥?然后从头再开始直到他说“不”?

这是我脚本的基本轮廓:

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 个答案:

答案 0 :(得分:1)

使用循环。

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

    printf 'Go again? (y/n) ' >&2
    IFS= read -r again
done