0

I'm trying to create a bash script that will allow you to specify an AWS account that you want to perform some actions in.

I have a variable I need to pass that refers to a file that the script will cycle through.

But the function that performs the actions isn't recognized by the script.

This is the error I get :

./delete_snapshots.sh

What AWS Account:

12345678910

Delete Snapshots in account: AWS Lab

./delete_snapshots.sh: line 14: delete_snapshots: command not found

This is the script :

#!/bin/bash
echo "What AWS Account: ";
read accountnumber
declare -a arr=("12345678910" "109876543212")
for i in "${arr[@]}"
do
  if [ "$i" -eq 12345678910 ]; then
     aws_account="AWS Lab"
     aws_key="lab"
     aws_file="aws_lab_snapshots.txt"
     echo "Delete Snapshots in account: $aws_account"
     delete_snapshots
     break
  elif [ "$i" -eq 109876543212 ]; then
     aws_account="AWS Billing"
     aws_key="bill"
     aws_file="aws_bill_snapshots.txt"
     echo "You are currently in account: $aws_account"
     delete_snapshots
     break
  else
     echo "Unkown account"
  fi
done

delete_snapshots(){
for i in $(cat $aws_file)
do
  echo "*****************************************************************"
  echo "deleting snapshot: $i"
  aws ec2 delete-snapshot --snapshot-id=$i --profile=lab 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=lab 2>&1 | sed -e 's/^An error occurred.*when calling the DescribeSnapshots operation: //g'
  echo "*****************************************************************"
  echo; echo; echo; echo; echo
done
}

The script works now thanks for some helpful suggestions. Here's the working form of the script:

#!/bin/bash


delete_snapshots(){
for i in $(cat $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
}

echo "What AWS Account: ";
read accountnumber
declare -a arr=("123456789101" "109876543212")
for i in "${arr[@]}"
do
  if [ "$i" -eq 123456789101 ]; then
     aws_account="AWS Lab"
     aws_key="lab"
     aws_file="source_files/aws_lab_snapshots.txt"
     echo "Delete Snapshots in account: $aws_account"
     delete_snapshots
     break
  elif [ "$i" -eq 109876543212 ]; then
     aws_account="AWS Billing"
     aws_key="bill"
     aws_file="source_files/aws_bill_snapshots.txt"
     echo "You are currently in account: $aws_account"
     delete_snapshots
     break
  else
     echo "Unkown account"
  fi
done
6
  • 3
    You need to define function using the function keyword. Also, your function should be defined first before invoking it. Bash is very particular about it. Commented May 15, 2018 at 16:25
  • Defining a function after you use it. Bash doesn't work this way. It reads a command and immediately executes it. It doesn't read and parse the entire script beforehand, unlike e.g. Python. Commented May 15, 2018 at 16:27
  • Ok. If I define the function first, how can I use the aws_key and aws_file variables in the function? Commented May 15, 2018 at 16:29
  • Just try using them normally. (When bash reads a function, it does not execute its lines as they are read) Commented May 15, 2018 at 16:35
  • 1
    @krishna_mee2004, the function keyword is absolutely not needed in bash -- its use makes code incompatible with the POSIX sh standard for no good reason. See wiki.bash-hackers.org/scripting/obsolete Commented May 15, 2018 at 17:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.