Bash script failing with unknown option due to space in argument

混江龙づ霸主 提交于 2019-12-24 03:29:06

问题


I am trying to run aws create lambda function. It goes as follows -

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)

All the variables come from command line. It is failing for env_variables. This gets constructed as -

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

where DAYS is actually "20 days"

How can I avoid this space and pass my command successfully.


回答1:


The value needs to be wrapped in quotes, try this:

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



回答2:


Finally following worked -

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

Important points -

  1. Quotes in env_variables
  2. Use of eval
  3. Single quote in command string i.e $env_variables

Reference - https://gist.github.com/andywirv/f312d561c9702522f6d4ede1fe2750bd

Complete working code :https://gist.github.com/aniket91/19492b32f570ece202718153661b1823



来源:https://stackoverflow.com/questions/47111075/bash-script-failing-with-unknown-option-due-to-space-in-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!