if 文を正しく実行できません。

if 文を正しく実行できません。

ファイルが存在するかどうかを確認し、存在しない場合はユーザーにファイルを生成するかどうかを尋ねたいと思います。ユーザーがYを入力してもNを入力しても、「Whatever you say」だけが画面に表示されます。

#!/bin/bash

#This is testing if a file (myFile) exists

if [ -f ~/myFile  ]
then 
        echo "The file exists!"
else
        echo "The file does not exist. Would you like to create it? (Y/N)"
        read ANSWER
fi

if [ "$ANSWER"="N" ]
then
        echo "Whatever you say!"
else
        touch myFile
        echo "The file has been created!"
fi

ベストアンサー1

=比較演算子を使用する場合はスペースを使用してください。[ ]シェル組み込み関数です。したがって、各パラメータをスペースと一緒に渡す必要があります。だからあなたはこれをしなければなりません:

if [ "$ANSWER" = "N" ]

源泉:http://www.tldp.org/LDP/abs/html/comparison-ops.html

おすすめ記事