8

「teamspeak.com」のような IP/ドメインが /etc/hosts に存在するかどうかを確認する .sh スクリプトを作成しようとしています。存在しない場合は、ホスト ファイルに何かを追加します。

今、私はこれを試しています:

if ! grep -q "teamspeak.com == teamspeak.com" /etc/hosts; then
    echo "Exists"
else
    echo "Add to etc host ting here" >> /etc/hosts;

fi
4

1 に答える 1

13
grep -q 

一致が見つかった場合は 0 で終了し、それ以外の場合は 1 で終了するため、! を削除する必要があります。および == 比較:

if grep -q "teamspeak.com" /etc/hosts; then
    echo "Exists"
else
    echo "Add to etc host ting here" >> /etc/hosts;
fi

これは単語ベースの検索ではないため、myteamspeak.com または teamspeak.com.us も検索されることに注意してください。ホスト名全体を取得するには、区切り文字を指定して cut コマンドを使用する必要があります。

新しいホストを追加するには:

echo "127.0.0.1 teamspeak.com" >> /etc/hosts
于 2015-03-16T17:03:28.853 に答える