由于参数中的空格,Bash脚本因选项未知而失败 [英] Bash script failing with unknown option due to space in argument

查看:101
本文介绍了由于参数中的空格,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}\"}"

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. 中的行情
  3. 使用eval
  4. 命令字符串即$env_variables
  5. 中的单引号

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

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

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.

解决方案

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

这篇关于由于参数中的空格,Bash脚本因选项未知而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆