由于参数中的空格,Bash 脚本失败,选项未知



我正在尝试运行 aws create lambda 函数。具体如下——

eval $(aws lambda create-function 
--function-name $FUNCTION_NAME 
--runtime $RUNTIME 
--role $ROLE 
--handler $HANDLER 
--region $REGION 
--zip-file $ZIP_FILE 
--profile $PROFILE 
--environment $env_variables)

所有变量都来自命令行。它env_variables失败了。这被构造为 -

env_variables="Variables={INPUT=${DAYS}}"

其中 DAYS 实际上是"20 days"

如何避免此空间并成功传递我的命令。

该值需要用引号括起来,请尝试以下操作:

env_variables="Variables="{INPUT=${DAYS}}""

最后工作 -

env_variables=""Variables":{"INPUT":"${DAYS}"}"
lambda_create_command="aws lambda create-function --function-name $FUNCTION_NAME --runtime $RUNTIME --role $ROLE --handler $HANDLER --region $REGION --zip-file $ZIP_FILE --profile $PROFILE --environment '$env_variables'"
echo "Executing command : $lambda_create_command"
eval $lambda_create_command

要点 -

  1. env_variables语录
  2. eval的使用
  3. 命令字符串中的单引号,即$env_variables

参考资料 - https://gist.github.com/andywirv/f312d561c9702522f6d4ede1fe2750bd

完整的工作代码:https://gist.github.com/aniket91/19492b32f570ece202718153661b1823

最新更新