unable to set variable in case statement bash

佐手、 提交于 2019-12-31 00:10:35

问题


I'm trying to set a variable based on a bunch of input conditions. Here's a small sample of the code:

#!/bin/bash
INSTANCE_SIZE=""
case "$1" in
   "micro")
     $INSTANCE_SIZE="t1.micro"
     ;;
   "small")
     $INSTANCE_SIZE="m1.small"

     ;;
esac
echo $INSTANCE_SIZE

When I run the script with the -ex switch and specify the proper argument:

+ case "$1" in
+ =m1.small
./provision: line 19: =m1.small: command not found

回答1:


You need to remove the $ sign in the assignments - INSTANCE_SIZE="m1.small". With the dollar sign, $INSTANCE_SIZE gets substituted with its value and no assignment takes place - bash rather tries to execute the command that resulted from the interpolation.



来源:https://stackoverflow.com/questions/6675879/unable-to-set-variable-in-case-statement-bash

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