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
functionkeyword. Also, your function should be defined first before invoking it. Bash is very particular about it.functionkeyword 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