bash如果elif语句总是打印第一个值

时间:2016-10-27 07:12:07

标签: linux bash

出于某种原因,始终打印第一个值。我无法弄清楚背后的原因。有什么想法吗?

#!/bin/bash
config="update_release"
if [[ "$config"=="update" ]]; then
   schema_list="main_schemas"
elif [[ "$config"=="update_release" ]] ; then
   schema_list="release_schemas"
elif [[ "$config"=="update_maintenance" ]]; then
   schema_list="maintenance_schemas"
fi
echo $schema_list

我尝试了很多东西,包括单个=,单个[]但似乎没有任何东西可以工作。

3 个答案:

答案 0 :(得分:2)

您需要在条件中添加空格:

#!/bin/bash
config="update_release"
if [ "$config" == "update" ]; then
   schema_list="main_schemas"
elif [ "$config" == "update_release" ] ; then
   schema_list="release_schemas"
elif [ "$config" == "update_maintenance" ]; then
   schema_list="maintenance_schemas"
fi
echo $schema_list

答案 1 :(得分:2)

此,

if [ "$config" == "update_release" ]    

的同义词
if [ "$config" = "update_release" ]

请注意构成=

的空白框架

我认为你应该重新考虑你想要做的事情的逻辑

[[ $config == update* ]]    # True if $config starts with an "update" (pattern matching).
[[ $config  == "update*" ]] # True if $config is equal to update* (literal matching).

[ $config  == update* ]     # File globbing and word splitting take place.
[ "$config" == "update*" ]  # True if $config is equal to update* (literal matching).

答案 2 :(得分:1)

或尝试使用单括号和单等号“=”

#!/bin/bash
config="update_release"
if [ "$config" = "update" ]; then
   schema_list="main_schemas"
elif [ "$config" = "update_release" ] ; then
   schema_list="release_schemas"
elif [ "$config" = "update_maintenance" ]; then
   schema_list="maintenance_schemas"
fi
echo $schema_list