无法在AWS Bash脚本中调用函数

时间:2018-05-18 20:05:40

标签: bash amazon-web-services

我正在编写一个bash脚本,它在AWS中执行许多常用功能。

当我运行脚本时,我试图调用的函数没有运行,脚本以“你在AWS中的工作完成”的消息结束:

These are the AWS Accounts:
1 Lab
2 Billing

Please enter a number for the account you want to work in.
Account Number: 
1
These are the actions possible in AWS: 
1 Add User and Access Keys
2 Delete Snapshots
Enter an action in AWS: 
1

Your work in AWS is done.
Your work in AWS is done.

我的脚本中的两个函数为脚本的其余部分奠定了基础:

  • choose_account
  • choose_action

choose_account设置您将使用的AWS账户.select_action设置一个名为user_action的变量,该变量包含函数的名称。

但是当我尝试在脚本末尾的if / else语句中调用该函数时,不会调用任何函数,脚本以“您的工作在AWS中完成”结束。

这是我的剧本:

#!/usr/bin/env bash

PATH=/home/myuser/.local/bin:$PATH

choose_account(){

echo "These are the KPMG AWS Accounts:"
  echo "1 Lab"
  echo "2 Billing"
  echo
  echo "Please enter a number for the account you want to work in."
  echo "Account Number: "
  read -r aws_account_number

if [ "$aws_account_number" -eq 1 ]; then
     declare -i accountnumber=423214542344
     return
  elif [ "$aws_account_number" -eq 2 ]; then
     declare -i accountnumber=188523423411
     return
  else
     echo "Unkown account"
  fi

  echo
  echo
}

choose_action(){

  echo "These are the actions possible in AWS: "
  echo "1 Add User and Access Keys"
  echo "2 Delete Snapshots"
  echo "Enter an action in AWS: "
  read -r action
  echo
  echo
  if [ "$action" -eq "1" ]; then
     user_action=add_user
     aws_file="source_files/aws_environments_nonprod.txt"
     return
  elif [ "$action" = ="2" ]; then
     user_action=add_user
     aws_file="source_files/aws_snapshots_prod.tx"
     return
  else
     echo "Action Not Chosen"
  fi
}

add_user(){
aws_user="us-svc-food"
aws_group1="grp-read-only"
aws_group2="grp-food-read-only"

IFS=$'\n'
set -f
for i in $aws_file
do
    echo "adding user: $aws_user to account: $i"
    aws iam create-user --user-name $aws_user --output=json --profile="$i"
        echo
        sleep 5
        echo "add user: $aws_user to group: $aws_group1"
        aws iam add-user-to-group --user-name $aws_user --group-name $aws_group1 --output=json --profile="$i"
        sleep 5
        echo "add user: $aws_user to group: $aws_group2"
        aws iam add-user-to-group --user-name $aws_user --group-name $aws_group2 --output=json --profile="$i"
        sleep 5
        echo
        echo "verify the user $aws_user is there in: $i"
        aws iam get-user --user-name $aws_user --output=json  --profile="$i"
        sleep 5
        echo
        echo "*************************************************"
        echo "creating the access key for $aws_user to account: $i"
        aws iam create-access-key --user-name $aws_user --output=json --profile="$i"
        echo "**************************************************"
        sleep 30
        echo
        echo "verify the user access keys for $aws_user on account: $i"
        aws iam list-access-keys --user-name $aws_user --output=json --profile="$i"
        echo
        echo "***************************************************"
        echo
    sleep 5
done
set +f
unset IFS
}

delete_snapshots(){
IFS=$'\n'
set -f
for i in $aws_file
do
  echo "*****************************************************************"
  echo "Deleting snapshot: $i:"
  aws ec2 delete-snapshot --snapshot-id="$i" --profile="$aws_key" 2>&1 | sed -e 's/^An error occurred.*when calling the DeleteSnapshot operation: //'
  echo "*****************************************************************"
  echo; echo; echo; echo; echo
  sleep 5
  echo "*****************************************************************"
  echo "Verify that snapshot: $i is gone:"
  aws ec2 describe-snapshots --snapshot-ids="$i" --profile="$aws_key" 2>&1 | sed -e 's/^An error occurred.*when calling the DescribeSnapshots operation: //g'
  echo "*****************************************************************"
  echo; echo; echo; echo; echo
done
set +f
unset IFS
}

choose_account
choose_action

declare -a arr=("423214542344" "188523423411")
for i in "${arr[@]}"
do
  if [ "$accountnumber" == 423214542344 ]; then
     aws_account="AWS Lab"
     aws_key="kpmg-lab"
     echo "$user_action in account: $aws_account"
     "$user_action"
     return
  elif [ "$accountnumber" == 188523423411 ]; then
     aws_account="AWS Billing"
     aws_key="kpmg-bill"
     echo "$user_action in account: $aws_account"
     "$user_action"
     return
  else
     echo "Your work in AWS is done."
  fi
done

我希望$ user_action是一个函数调用。

编辑:

我创造了一些详细的输出。

PS4=':$BASH_SOURCE:$LINENO+' bash -x aws_tools_test.sh 
    :aws_tools_test.sh:3+PATH=/home/myuser/.local/bin:/home/myuser/bin:/home/myuser/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
    :aws_tools_test.sh:113+choose_account
    :aws_tools_test.sh:7+echo 'These are the KPMG AWS Accounts:'
    These are the KPMG AWS Accounts:
    :aws_tools_test.sh:8+echo '1 Lab'
    1 Lab
    :aws_tools_test.sh:9+echo '2 Billing'
    2 Billing
    :aws_tools_test.sh:10+echo

    :aws_tools_test.sh:11+echo 'Please enter a number for the account you want to work in.'
    Please enter a number for the account you want to work in.
    :aws_tools_test.sh:12+echo 'Account Number: '
    Account Number: 
    :aws_tools_test.sh:13+read -r aws_account_number
    1
    :aws_tools_test.sh:15+'[' 1 -eq 1 ']'
    :aws_tools_test.sh:16+declare -i accountnumber=423214542344
    :aws_tools_test.sh:17+return
    :aws_tools_test.sh:114+choose_action
    :aws_tools_test.sh:31+echo 'These are the actions possible in AWS: '
    These are the actions possible in AWS: 
    :aws_tools_test.sh:32+echo '1 Add User and Access Keys'
    1 Add User and Access Keys
    :aws_tools_test.sh:33+echo '2 Delete Snapshots'
    2 Delete Snapshots
    :aws_tools_test.sh:34+echo 'Enter an action in AWS: '
    Enter an action in AWS: 
    :aws_tools_test.sh:35+read -r action
    1
    :aws_tools_test.sh:36+echo

    :aws_tools_test.sh:37+echo

    :aws_tools_test.sh:38+'[' 1 -eq 1 ']'
    :aws_tools_test.sh:39+user_action=add_user
    :aws_tools_test.sh:40+aws_file=source_files/aws_environments_nonprod.txt
    :aws_tools_test.sh:41+return
    :aws_tools_test.sh:116+arr=("423214542344" "188523423411")
    :aws_tools_test.sh:116+declare -a arr
    :aws_tools_test.sh:117+for i in '"${arr[@]}"'
    :aws_tools_test.sh:119+'[' '' == 423214542344 ']'
    :aws_tools_test.sh:125+'[' '' == 188523423411 ']'
    :aws_tools_test.sh:132+echo 'Your work in AWS is done.'
    Your work in AWS is done.
    :aws_tools_test.sh:117+for i in '"${arr[@]}"'
    :aws_tools_test.sh:119+'[' '' == 423214542344 ']'
    :aws_tools_test.sh:125+'[' '' == 188523423411 ']'
    :aws_tools_test.sh:132+echo 'Your work in AWS is done.'
    Your work in AWS is done.

看起来$ accountnumber变量未设置。所以函数没有被调用。那是为什么?

1 个答案:

答案 0 :(得分:1)

在函数中使用时,declare内置使变量本地化为该函数。因此,当您在accountnumber中声明choose_account时,该值在脚本的顶层不可见。

看起来确实需要声明此变量的类型,因此declare -i是不必要的。只做一个普通的变量赋值。如果你真的觉得需要将它作为整数变量,请将声明放在顶级脚本而不是函数中,然后在函数中分配它。或者完全避免访问全局变量,并让函数回显结果:

declare -i accountnumber=$(choose_account)