2016-10-27 13 views
0

何らかの理由で、最初の値が常に出力されます。私はそれの背後にある理由を理解することはできません。何か案は?elif文が常に最初の値を出力する場合

#!/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 

私はsingle =、single []を含めて多くのことを試しましたが、全く何も動作していないようです。

+0

は[shellcheck]試しにしようとしているかのロジックを再考すべきだと思う空白フレーミング(http://shellcheck.net) –

答えて

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

OR

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

は同義語である、 "="

#!/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 
2

これに署名し、単一のブラケットと、単一の等号で試してみてください

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). 
関連する問題