求助:AWS SSM自动化文档为何自动添加 并移除脚本单引号
AWS SSM自动化文档脚本单引号被自动移除的原因
在AWS SSM中粘贴自动化文档脚本时,出现部分单引号被自动移除的问题,导致脚本无法正常运行。已知制表符被转义为\t的原因,但对单引号消失的问题存在疑问。
原始输入脚本
schemaVersion: "2.2" description: "Check HANA DB and stop it LINUX" mainSteps: - action: "aws:runShellScript" name: "HANA_DB_STOP" inputs: runCommand: - '#### errocodes' - '#### 0 = SCRIPT RAN OK' - '#### 10 = SCRIPT FAILED' - '' - '# stop hana db' - 'echo "Stopping Hana DB"' - 'sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop' - '' - '# check hana db state' - 'echo "Checking if Hana DB is running."' - 'HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList`' - 'sleep 20' - 'if [[ "$HANADBSTATUS" =~ "GRAY" ]]' - 'then' - ' echo "Hana DB is stopped."' - ' exit 0' - 'else' - 'i=1' - 'while [[ ! "$HANADBSTATUS" =~ "GRAY" ]] && [[ "$i" -lt 11 ]]' - ' do' - ' echo "Warning: HANA DB is running. Checking 10 times with 20 second intervalls until script aborts. This is check $i." ' - ' sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop' - ' sleep 20' - ' # check db state again' - ' HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList`' - ' ((i++))' - ' if [ "$i" = 10 ]' - ' then' - ' echo "Error: retried $i-Times. Couldnt stop DB. Exiting Script."' - ' echo "Script aborts with Error 0"' - ' exit 10' - ' fi' - ' done' - ' if [[ "$HANADBSTATUS" =~ "GRAY" ]]' - ' then' - ' echo "Hana DB is stopped."' - ' exit 0' - ' fi' - 'fi'
AWS返回的修改后脚本
schemaVersion: '2.2' description: Check HANA DB and stop it LINUX mainSteps: - action: 'aws:runShellScript' name: HANA_DB_STOP inputs: runCommand: - '#### errocodes' - "#### 0\t= SCRIPT RAN OK" - "#### 10\t= SCRIPT FAILED" - '' - '# stop hana db' - echo "Stopping Hana DB" - sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop - '' - '# check db state' - echo "Checking if Hana DB is running." - HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList` - sleep 20 - 'if [[ "$HANADBSTATUS" =~ "GRAY" ]]' - then - ' echo "Hana DB is stopped."' - ' exit 0' - else - i=1 - 'while [[ ! "$HANADBSTATUS" =~ "GRAY" ]] && [[ "$i" -lt 11 ]]' - "\tdo" - "\t\techo \"Warning: HANA DB is running. Checking 10 times with 20 second intervalls until script aborts. This is check $i.\" " - "\t\tsudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop" - "\t\tsleep 20" - "\t\t# check db state again" - "\t\tHANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList`" - "\t\t((i++))" - "\t\tif [ \"$i\" = 10 ]" - "\t\t\tthen" - "\t\t\t\techo \"Error: retried $i-Times. Couldnt stop DB. Exiting Script.\"" - "\t\t\t\techo \"Script aborts with Error 0\"" - "\t\t\t\texit 10" - "\t\tfi" - "\tdone" - "\tif [[ \"$HANADBSTATUS\" =~ \"GRAY\" ]]" - ' then' - ' echo "Hana DB is stopped."' - ' exit 0' - "\tfi" - fi
单引号被移除的原因
这是AWS SSM的YAML解析器自动标准化处理导致的:
- 对于不包含shell特殊语法(如变量引用
$xxx、条件判断[[ ... ]]、命令替换...等)的行,SSM认为单引号是不必要的字符串包裹,会自动移除。比如echo "Stopping Hana DB"、sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop这类简单命令行,本身不需要单引号也能被shell正确执行,因此SSM会去除单引号。 - 对于包含shell特殊语法的行,SSM会保留单引号,避免YAML解析时破坏脚本逻辑。比如
if [[ "$HANADBSTATUS" =~ "GRAY" ]]这类条件判断行,单引号的存在能确保整个条件表达式作为一个完整的字符串传递给shell执行。 - 另外,YAML格式本身允许纯字符串不用引号包裹,SSM在处理时会将带单引号的简单字符串转换为无引号的标准格式,这也是单引号被移除的原因之一。
内容的提问来源于stack exchange,提问作者Cédric Jäggi
相关产品推荐
相关产品推荐

