2017-07-22 6 views
0

私の人生の中では、2番目の機能が動作しない理由を理解できません。 elseとelifを使用しようとしましたが、構文エラーが表示されたり、2番目の関数が表示されません。これは単純なbashスクリプトです。私が間違っていることを知る必要があります。機能以外で動作している場合

function yes() { 
echo "Good boy" 
} 

function no() { 
echo "Bad Boy" 
} 

echo " Did you eat this pillow? [y,n]" ; tput sgr0 
read $answer 

if [ "$answer" != "y" ]; 
then 
yes 

elif [ "$answer" != "n" ]; 
then 
no 

else 
exit 

fi 
+1

'read $ answer'を' read answer'に変更します。 – codeforester

+0

'read'のパラメータは変数名でなければなりません。 'read answer'では、「answer」は名前です。 'read $ answer'において、' $ answer'は変数 "answer"の* value *です。 – janos

+0

私はそのような単純なことを逃したと信じていない – Red5tar

答えて

1

最初の「回答」変数の$記号を削除する必要があります。さらに、readのサポートでは、文字を入力する前にプロンプ​​トを表示するので、スクリプトを次のように変更する必要があります。

#!/bin/bash 
function yes() { 
    echo "Good boy" 
} 

function no() { 
    echo "Bad Boy" 
} 

read -p " Did you eat this pillow? [y,n]" answer 

if [ "$answer" != "y" ] 
then 
    yes 
elif [ "$answer" != "n" ] 
then 
    no 
else 
    exit 
fi 
+0

助けてくれてありがとう – Red5tar

+0

@ Red5tarあなたが役に立ったら私の答えを受け入れてください。ありがとうございました。 – NeilWang

関連する問題