title: Cannot Call Function in AWS Bash Script
body: <p>I'm writing a bash script that performs a number of common functions in AWS.</p>
  
  <p>When I run the script the functions I'm trying to call are not run, and the script ends with the message "Your work in AWS is done":</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>The two functions in my script that lay the ground work for the rest of the script are called:</p>
  
  <ul>
  <li>choose_account</li>
  <li>choose_action</li>
  </ul>
  
  <p>choose_account sets the AWS account you will work in. choose_action sets a variable called user_action that contains the name of a function.</p>
  
  <p>But when I try to call the function in the if/else statements at the end of the script no function gets called and the script ends with "Your work is done in AWS." </p>
  
  <p>Here is my script:</p>
  
  <pre><code>#!/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&gt;&amp;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&gt;&amp;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
  </code></pre>
  
  <p>I want $user_action to be a function invocation.</p>
  
  <p>EDIT:</p>
  
  <p>I've created some verbose output.</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>It looks like the $accountnumber variable isn't getting set. So the functions are not getting called. Why is that?</p>