Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell脚本语法,转义字符_Shell_Sh - Fatal编程技术网

Shell脚本语法,转义字符

Shell脚本语法,转义字符,shell,sh,Shell,Sh,我有一个shell脚本,如下所示。此脚本实际上是在保护组中的自动调用比例中添加AWS实例。当我运行单独的命令时,一切正常。但当我创建一个shell文件并尝试执行相同的文件时,出现了错误。请参阅下面的脚本 set -x INSTANCE_ID=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id) ASG_NAME=$(aws ec2 describe-tags --filters "Name=resource-id,

我有一个shell脚本,如下所示。此脚本实际上是在保护组中的自动调用比例中添加AWS实例。当我运行单独的命令时,一切正常。但当我创建一个shell文件并尝试执行相同的文件时,出现了错误。请参阅下面的脚本

set -x
INSTANCE_ID=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)
ASG_NAME=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID"  --region us-east-2 | jq '.Tags[] | select(.["Key"] | contains("a:autoscaling:groupName")) | .Value')
ASG_NAME=$(echo $ASG_NAME | tr -d '"')
aws autoscaling set-instance-protection --instance-ids $INSTANCE_ID --auto-scaling-group-name $ASG_NAME  --protected-from-scale-in --region us-east-2
错误如下所示。我认为问题在于第二行。它无法获得ASG_名称,我尝试了一些转义字符,但没有任何效果

+++ wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
++ INSTANCE_ID=i-----
+++ aws ec2 describe-tags --filters Name=resource-id,Values=i------ --region us-east-2
+++ jq '.Tags[] | select(.["Key"] | contains("a:autoscaling:groupName")) | .Value'
++ ASG_NAME=
+++ echo
+++ tr -d '"'
++ ASG_NAME=
++ aws autoscaling set-instance-protection --instance-ids i---- --auto-scaling-group-name --protected-from-scale-in --region us-east-2
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument --auto-scaling-group-name: expected one argument

> Blockquote

通过@chepner的推荐解决了问题。修改第二行

ASG_NAME=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID"  --region us-east-2 --query 'Tags[1].Value')

你想干什么?本着这个论坛的精神,你的问题是什么?所以请注意:不要使用“反勾号”,它们是非常不受鼓励的。使用$。。。相反和而且尝试添加set-x来检查脚本。同意以上所有内容,在您精通之前,您将希望为您创建的每个变量包括printf dbg:INSTANCE_ID=[${INSTANCE_ID:-unset}]\n 1>&2这样的行。祝你好运我明白了,我更新了问题,@KamilCuk,我也更新了脚本。++ASG_NAME=-see,所以ASG_NAME变量是空的。一定是第二个aws或jq。aws ec2描述标签输出是否为空?可能是jq“…|.Value”没有输出任何东西?这里不一定需要jq。aws使用-query参数和实现自己的JSON处理。