ファイルの末尾に追加するには、「sed」を使用します。

ファイルの末尾に追加するには、「sed」を使用します。

私は現在sedfromを使用しています。呼び出すユーザーがファイルに書き込む権限がない bash スクリプトの制限を回避するために、stdinこのスクリプトを使用しています。sedecho "..." >> outputfile.conf

これは私の目的です。書くファイルとして:

echo "<VirtualHost *:80>
    ...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"

次は何をすべきですか?追加同じファイルに?

if $enable_ssl; then
    echo "<VirtualHost *:443>
        ...
    </VirtualHost>" | sudo sed <opions-to-sed-here>
fi

ベストアンサー1

>より高い権限または異なる権限を持つシェルの一般的な代替方法は次のとおりです。

echo "replace file content with this line" | sudo tee protectedFile >/dev/null

追加するには、以下を使用します-a

echo "append this line to file" | sudo tee -a protectedFile >/dev/null

おすすめ記事