![](/img/trans.png)
[英]assign output of memcache command to a variable in shell/bash script
[英]How to assign a command contains ` ` to a variable in bash shell script?
我想編寫一個 bash 腳本,用於在 aws 中從目標組自動注冊和注銷目標。
腳本是這樣的:
#!/bin/bash
target_group_name=$1
instance_Id=$2
target_group_arn="$(aws elbv2 describe-target-groups --query 'TargetGroups[?contains(TargetGroupName, `$1`)].TargetGroupArn' --output text)"
echo -n "Enter the option you want to do (enter 1 for register and enter 2 for deregister): "
read OPTION
case $OPTION in
1)
if register_output=$(aws elbv2 register-targets --target-group-arn '$target_group_arn' --targets Id="$2");
then
echo "Registering instance into target group. Please wait for 5 minutes registration progress finished"
else
echo "!!!! ERROR !!!!"
fi
;;
2)
if deregister_output=$(aws elbv2 deregister-targets --target-group-arn '$target_group_arn' --targets Id="$2");
then
echo "Deregistering instance into target group. Please wait for 5 minutes registration progress finished"
else
echo "!!!! ERROR !!!!"
fi
;;
*)
echo "unknown option"
;;
esac
但是,當我運行腳本時,沒有找到 arn。
我嘗試打印出$1
(S1 在左操作數符號內)。 但是,似乎bash腳本不理解,它什么也沒打印出來。
你能幫我弄清楚我的腳本有什么問題嗎? 或者我如何重現我的查詢以獲取目標組的 arn 以便我可以將它傳遞給我的命令?
非常感謝!
您的目標組 ARN 解析器如下所示:
aws elbv2 describe-target-groups \
--query 'TargetGroups[?contains(TargetGroupName, `$1`)].TargetGroupArn' \
--output text
我不確定您是否在$1
變量周圍使用了反引號,但是:
"
)。我從未見過這樣使用反引號。$1
變量不會解析 - AWS CLI 將完全按照所寫的方式獲取查詢,甚至不會執行反引號,如果您在開始時使用echo
運行該命令,您會看到.你可能想寫這個:
aws elbv2 describe-target-groups \
--query "TargetGroups[?contains(TargetGroupName, \"$1\")].TargetGroupArn" \
--output text
因為我剛剛找到了我的問題的答案,所以我會在這里為感興趣的人發布我的答案並關閉我的帖子。
非常感謝你們幫助我! :D
#!/bin/bash
profile=$1
target_group_name=$1
instance_id=$2
target_group_arn="$(aws elbv2 describe-target-groups --query 'TargetGroups[].[TargetGroupArn]' --names "$2" --profile "$1" --output text)"
echo -n "Enter the option you want to do (enter 1 for register and enter 2 for deregister): "
read OPTION
case $OPTION in
1)
if register_output=$(aws elbv2 register-targets --target-group-arn "$target_group_arn" --targets Id="$2");
then
echo "Registering instance into target group. Please wait for 5 minutes registration progress finished"
else
echo "!!!! ERROR !!!!"
fi
;;
2)
if deregister_output=$(aws elbv2 deregister-targets --target-group-arn "$target_group_arn" --targets Id="$2");
then
echo "Deregistering instance into target group. Please wait for 5 minutes registration progress finished"
else
echo "!!!! ERROR !!!!"
fi
;;
*)
echo "unknown option"
;;
esac
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.