Bash脚本无法识别函数

时间:2018-05-15 16:23:50

标签: bash amazon-web-services

我正在尝试创建一个bash script,允许您指定要在其中执行某些操作的 AWS 帐户。

我有一个我需要传递的变量,它指的是脚本将循环的文件。

script无法识别执行操作的功能。

这是我得到的error

  

./ delete_snapshots.sh

     

什么是AWS账户:

     

12345678910

     

删除帐户中的快照:AWS Lab

     

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

这是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
}

该脚本现在可以使用一些有用的建议。这是脚本的工作形式:

#!/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

0 个答案:

没有答案