从bash中的另一个函数中调用函数
我有一个非常简单的函数,可以在脚本工作完成时打印横幅。这就是它所做的一切:从bash中的另一个函数中调用函数,bash,Bash,我有一个非常简单的函数,可以在脚本工作完成时打印横幅。这就是它所做的一切: end_banner() { echo "*********************************************" echo "* Your work in AWS is done *" echo "*********************************************" } 但是当我从脚本中的其他函数调用该函数时,
end_banner() {
echo "*********************************************"
echo "* Your work in AWS is done *"
echo "*********************************************"
}
但是当我从脚本中的其他函数调用该函数时,它不会被调用
例如,我尝试从此函数调用end_banner函数:
create_instance() {
...lines that do stuff...
end_banner
}
并且end_banner函数中的输出不会被打印出来。当我使用bash-x运行脚本时,它甚至没有显示。好像那条横幅线根本就不存在
以下是我的整个脚本的最低版本,以及end_banner、create_volume和create_instance的完整版本:
end_banner() {
echo "*********************************************"
echo "* Your work in AWS is done *"
echo "*********************************************"
}
main() {
# 1 Lab
if [ "$accountnumber" == 123456789 ]; then
"$user_action"
return
fi
}
choose_account() {
echo "1 Lab"
echo "Please enter a number for the account you want to work in."
printf "Choose AWS Account: "
read -r aws_account_number
echo
# 1 Lab
if [ "$aws_account_number" -eq 1 ]; then
aws_account="Company Lab"
aws_key="lab"
accountnumber=123456789
return
fi
}
choose_action() {
echo "These are the actions possible in AWS: "
echo
echo "5 Create Volume"
echo "20 Create AWS Instance"
echo; echo
printf "Enter an action in AWS: "
read -r action
echo
# 5 Create AWS Volume
if [ "$action" -eq "5" ]; then
user_action=create_volume
return
# 20 Create AWS Instance
elif [ "$action" -eq "20" ]; then
user_action=create_instance
return
fi
}
create_volume() {
echo "* Create an EBS volume in AWS $aws_account *"
echo
printf "Enter a volume name: "
read -r volume_name
echo
# Lab
if [ "$accountnumber" -eq 123456789 ]; then
echo "Availability Zones for $aws_account"
echo "AZ 1: us-east-1a"
echo "AZ 2: us-east-1b"
echo
fi
#Availability Zones
printf "Enter availability zone\\nExample. Type out: us-east-1a\\n"
printf "AZ: "
read -r availability_zone
echo
# Volume Size
printf "Enter size: "
read -r volume_size
echo
# Create from snapshot
printf "Create from snapshot (y/n): "
read -r from_snapshot
echo
if [[ "$from_snapshot" = [Yy] ]]; then
printf "Enter Snapshot ID: "
read -r snapshot_id
echo
else
printf "No Snapshot Required\\n\\n"
fi
# Add encryption
printf "Encrypted (y/n): "
read -r encrypted
echo
if [[ "$encrypted" = [Yy] ]]; then
printf "Enter KMS Key ID: "
read -r kms_key_id
echo
else
printf "No Encryption Required\\n\\n"
fi
# Set the Owner / Application / Engagement
printf "Enter the Owner Name: "
read -r owner
echo
printf "Enter the application name: "
read -r application
echo
printf "Enter the engagement code: "
read -r engagement
echo
# Create the volume based on the above
if [[ "$from_snapshot" = [Yy] ]]; then
#Create EBS Volume from Snapshot"
volume_id=$(aws ec2 create-volume --size "$volume_size" --availability-zone "$availability_zone" --snapshot-id "$snapshot_id" --volume-type gp2 --tag-specifications "ResourceType=volume,Tags=[{Key=\"Name\",Value=\"$volume_name\"},{Key=Engagement,Value=\"$engagement\"},{Key=\"Owner\", Value=\"$owner\"},{Key=\"Application\", Value=\"$application\"}]" --profile="$aws_key" | jq -r '.VolumeId')
return
elif [[ "$encrypted" = [Yy] ]]; then
#Create EBS Volume with KMS Key"
volume_id=$(aws ec2 create-volume --size "$volume_size" --availability-zone "$availability_zone" --kms-key-id="$kms_key_id" --volume-type gp2 --tag-specifications "ResourceType=volume,Tags=[{Key=\"Name\",Value=\"$volume_name\"},{Key=\"Engagement\",Value=\"$engagement\"},{Key=\"Owner\", Value=\"$owner\"},{Key=\"Application\", Value=\"$application\"}]" --profile="$aws_key" | jq -r '.VolumeId')
elif [[ "$from_snapshot" = [Yy] && "$encrypted" = [Yy] ]]; then
#Create EBS Volume from snapshot with KMS Key"
volume_id=$(aws ec2 create-volume --size "$volume_size" --availability-zone "$availability_zone" --snapshot-id "$snapshot_id" --volume-type gp2 --profile="$aws_key" --tag-specifications "ResourceType=volume,Tags=[{\"Key=Name,Value=\"$volume_name\"},{Key=\"Engagement\",Value=\"$engagement\"},{Key=\"Owner\", Value=\"$owner\"},{Key=\"Application\", Value=\"$application\"}]" --profile="$aws_key" | jq -r '.VolumeId')
else
#Create a plain volume without snapshot and encryption
volume_id=$(aws ec2 create-volume --size "$volume_size" --availability-zone "$availability_zone" --volume-type gp2 --tag-specifications "ResourceType=volume,Tags=[{Key=\"Name\",Value=\"$volume_name\"},{Key=\"Engagement\",Value=\"$engagement\"},{Key=\"Owner\", Value=\"$owner\"},{Key=\"Application\", Value=\"$application\"}]" --profile="$aws_key" | jq -r '.VolumeId')
fi
sleep 10
echo "Volume created is: $volume_id"
echo
local VOLID=$1
eval $VOLID="$volume_id"
}
create_instance() {
echo " Create An Instance in AWS $aws_account"
printf "Enter Platform\\n"
printf "Example: Windows / Linux\\n"
printf "Plaform: "
read -r platform
# using LAB amis for now
if [ \( "$platform" = "Windows" \) ] || [ \( "$platform" = "windows" \) ]; then
ami="ami-03bfe6d8e0e60051c"
elif [ \( "$platform" = "Linux" \) ] || [ \( "$platform" = "linux" \) ]; then
ami="ami-4fb38358"
else
printf "That is not a valid choice."
fi
printf "Enter an Instance Type: "
read -r instance_type
printf "Enter Number of Instances: "
read -r num_instances
printf "Enter a Subnet ID: "
read -r subnet_id
printf "Enter Security Group ID: "
read -r security_group
printf "Enter a key name: "
read -r key_name
printf "Enter a server name: "
read -r name_tag
printf "Enter an application name: "
read -r application_tag
printf "Enter an engagement code: "
read -r engagement_tag
printf "Enter an owner name: "
read -r owner_tag
printf "Enter a role: "
read -r role_tag
## Launch the instance
tag_instance_id=$(aws ec2 run-instances --count "$num_instances" --image-id "$ami" --instance-type "$instance_type" --key-name "$key_name" --subnet-id "$subnet_id" --security-group-ids "$security_group" --profile="$aws_key" | jq -r '.Instances[].InstanceId')
# Get volume ID and device name
root_vol=$(aws ec2 describe-instances --instance-ids "$tag_instance_id" --profile="$aws_key" | jq -r '.Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId')
root_vol_device_name=$(aws ec2 describe-volumes --volume-ids "$root_vol" --profile="$aws_key" | jq -r '.Volumes[].Attachments[].Device')
az=$(aws ec2 describe-instances --instance-ids "$tag_instance_id" --profile="$aws_key" | jq -r '.Reservations[].Instances[].Placement.AvailabilityZone')
echo
printf "Instance ID: %s has been created in Availability Zone: %s\\n" "$tag_instance_id" "$az"
printf "Wait till the instance is created to tag it.\\n"
sleep 15
# Tagging the new instance
aws ec2 create-tags --resources "$tag_instance_id" --tags Key=Name,Value="$name_tag" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
## Tagging the Root Volume - fixed tags
aws ec2 create-tags --resources "$root_vol" --tags Key=Name,Value="$name_tag root volume" Key=DeviceName,Value="$root_vol_device_name" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
printf "The instance %s now has the following tags:\\n" "$tag_instance_id"
printf "Host Name: %s\\n" "$name_tag"
printf "Application: %s\\n" "$application_tag"
printf "Engagement: %s\\n" "$engagement_tag"
printf "Owner: %s\\n" "$owner_tag"
printf "Role: %s\\n" "$role_tag"
echo
printf "Do you need additional volumes (y/n): "
read -r vol_answer
if [[ "$vol_answer" = [Yy] ]]; then
printf "Number of volumes to add: "
read -r num_volumes
volumes=()
devices=( {f..z} )
for (( i=0; i < num_volumes; i++ )); do
CURID=''
create_volume 'CURID'
volumes[$i]=${CURID}
printf "Attach volume ID: %s\\n" "${volumes[i]}"
if [ \( "$platform" = "Windows" \) ] || [ \( "$platform" = "windows" \) ]; then
device="/dev/sd${devices[i]}"
aws ec2 attach-volume --volume-id "${volumes[i]}" --instance-id "$tag_instance_id" --device "$device" --profile="$aws_key" | jq '.'
aws ec2 create-tags --resources "${volumes[i]}" --tags Key=Name,Value="$name_tag volume" Key=DeviceName,Value="$device" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
elif [ \( "$platform" = "Linux" \) ] || [ \( "$platform" = "linux" \) ]; then
device="/dev/xvd${devices[i]}"
aws ec2 attach-volume --volume-id "${volumes[i]}" --instance-id "$tag_instance_id" --device "$device" --profile="$aws_key" | jq '.'
aws ec2 create-tags --resources "${volumes[i]}" --tags Key=Name,Value="$name_tag volume" Key=DeviceName,Value="$device" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
else
printf "That is not a valid choice.\\n"
fi
done
return
elif [[ "$vol_answer" = [Nn] ]]; then
printf "Ok. No added volumes will be created.\\n"
else
printf "Input is incorrect.\\n"
fi
end_banner
}
choose_action
choose_account
main "$@"
end_banner(){
回声“***********************************************************************”
echo“*您在AWS中的工作已完成*”
回声“***********************************************************************”
}
main(){
#1实验室
如果[“$accountnumber”==123456789];则
“$user\u操作”
回来
fi
}
选择_帐户(){
回声“1实验室”
echo“请输入您要使用的帐户的号码。”
printf“选择AWS帐户:”
read-r aws\u账号
回响
#1实验室
如果[“$aws\U账号”-等式1];则
aws_account=“公司实验室”
aws_key=“实验室”
accountnumber=123456789
回来
fi
}
选择_action(){
echo“这些是AWS中可能的操作:
回响
回显“5创建卷”
echo“20创建AWS实例”
回声
printf“在AWS中输入操作:”
读操作
回响
#5创建AWS卷
如果[“$action”-等式“5”];则
用户\u操作=创建\u卷
回来
#20创建AWS实例
elif[“$action”-等式“20”];然后
用户\u操作=创建\u实例
回来
fi
}
创建卷(){
echo“*在AWS$AWS_帐户中创建EBS卷*”
回响
printf“输入卷名:”
read-r卷名
回响
#实验室
如果[“$accountnumber”-等式123456789];则
echo“$aws_帐户的可用区域”
echo“AZ 1:us-east-1a”
echo“AZ 2:us-east-1b”
回响
fi
#可用区
printf“输入可用区\\例如,键入:us-east-1a\\n”
printf“AZ:”
read-r可用性\u区域
回响
#体积大小
printf“输入大小:”
read-r卷大小
回响
#从快照创建
printf“从快照创建(是/否)”
从\u快照读取-r
回响
如果[[“$from_snapshot”=[Yy]];则
printf“输入快照ID:”
read-r快照\u id
回响
其他的
printf“不需要快照\\n\\n”
fi
#添加加密
printf“加密(是/否):”
read-r加密
回响
如果[[“$encrypted”=[Yy]];则
printf“输入KMS密钥ID:”
读取-r kms\U密钥\U id
回响
其他的
printf“无需加密\\n\\n”
fi
#设置所有者/应用程序/约定
printf“输入所有者名称:”
read-r所有者
回响
printf“输入应用程序名称:”
read-r应用程序
回响
printf“输入业务代码:”
read-r啮合
回响
#基于上述内容创建卷
如果[[“$from_snapshot”=[Yy]];则
#从快照创建EBS卷“
卷id=$(aws ec2创建卷--size“$volume\u size”--可用区“$availability\u zone”--快照id“$snapshot\u id”--卷类型gp2--标记规范“ResourceType=volume,Tags=[{Key=\\'Name\”,Value=\“$volume\u Name\”},{Key=Engagement,Value=\'$Engagement\},{Key='Owner\”,Value=\\\'''$Owner\,{Key=\'Application\,Value=$Application\}]“--profile=“$aws_key”| jq-r'.VolumeId')
回来
elif[“$encrypted”=[Yy]];然后
#使用KMS键创建EBS卷“
卷id=$(aws ec2创建卷--size“$volume\u size”--可用区“$availability\u zone”--kms密钥id=“$kms\u key\u id”--卷类型gp2--标记规范“ResourceType=volume,Tags=”[{key=“Name\”,Value=“$volume\u Name\”},{key=“Engagement\”,Value=“$Engagement\”},{key=“Owner\”,Value=“$Owner\”,{key=“Application\”,Value=“$Application=”\“}]”--profile=“$aws_key”| jq-r'.VolumeId')
elif[“$from_snapshot”=[Yy]&&“$encrypted”=[Yy]];然后
#使用KMS密钥从快照创建EBS卷“
卷id=$(aws ec2创建卷--size“$volume\u size”--可用区“$availability\u zone”--快照id“$snapshot\u id”--卷类型gp2--profile=“$aws\u key”--标记规范“ResourceType=volume,Tags=[{\”key=Name,Value=\“$volume\u Name\”},{key=\\'Engagement\”,Value={key=\'Owner\',{key='Application\',Value=\“$application\”}]“--profile=“$aws\u key”| jq-r'.VolumeId')
其他的
#创建没有快照和加密的普通卷
卷id=$(aws ec2创建卷--size“$volume\u size”--可用区“$availability\u zone”--卷类型gp2--标记规范“ResourceType=volume,标记=[{Key=\“Name\”,值=\“$volume\u Name\”},{Key=\“Engagement\”,值=\“$Engagement\”},{Key=“Owner\”,值=“$Owner\”},{Key=“Application\”,值=“$Application\”--profile=“$aws\”“| jq-r'.VolumeId')
fi
睡眠10
echo“创建的卷为:$Volume\u id”
回响
本地卷ID=$1
eval$VOLID=“$volume\u id”
}
创建_实例(){
echo“在AWS$AWS_帐户中创建实例”
printf“进入平台\\n”
printf“示例:Windows/Linux\\n”
printf“Plaform:”
read-r平台
#目前正在使用实验室amis
如果[\(“$platform”=“Windows”\)]|【\(“$platform”=“Windows”\)];则
ami=“ami-03bfe6d8e0e60051c”
elif[\(“$platform”=“Linux”\)][\(“$platform”=“Linux”\)];然后
ami=“ami-4fb38358”
其他的
printf“这不是一个有效的选择。”
fi
printf“输入实例类型:”
read-r实例类型
printf“输入实例数:”
read-r num_实例
printf“输入子网ID:”
read-r子网\u id
printf“输入安全组ID:”
read-r安全组
printf“输入密钥名称:”
read-r键名称
printf“输入服务器名称:”
read-r name_标签
printf“输入应用程序名称:”
read-r应用程序标签
printf“输入一个e
for (( i=0; i < num_volumes; i++ )); do
CURID=''
create_volume 'CURID'
volumes[$i]=${CURID}
printf "Attach volume ID: %s\\n" "${volumes[i]}"
if [ \( "$platform" = "Windows" \) ] || [ \( "$platform" = "windows" \) ]; then
device="/dev/sd${devices[i]}"
aws ec2 attach-volume --volume-id "${volumes[i]}" --instance-id "$tag_instance_id" --device "$device" --profile="$aws_key" 2>&1 | sed -e 's/An error occurred (InvalidVolume.ZoneMismatch) //g' | jq '.'
aws ec2 create-tags --resources "${volumes[i]}" --tags Key=Name,Value="$name_tag volume" Key=DeviceName,Value="$device" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
elif [ \( "$platform" = "Linux" \) ] || [ \( "$platform" = "linux" \) ]; then
device="/dev/xvd${devices[i]}"
aws ec2 attach-volume --volume-id "${volumes[i]}" --instance-id "$tag_instance_id" --device "$device" --profile="$aws_key" 2>&1 | sed -e 's/An error occurred (InvalidVolume.ZoneMismatch) //g' | jq '.'
aws ec2 create-tags --resources "${volumes[i]}" --tags Key=Name,Value="$name_tag volume" Key=DeviceName,Value="$device" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
else
printf "That is not a valid choice.\\n"
fi
return
done