Linuxでは条件判断を行って特定の行にコンテンツを挿入できますか

Linuxでは条件判断を行って特定の行にコンテンツを挿入できますか

Linux では条件判定を行って特定の行にコンテンツを挿入できますか?

たとえば、ユーザー「test」を /etc/sudoers に追加して、root に切り替えられるようにします。

  1 #
  2 # This file MUST be edited with the 'visudo' command as root.
  3 #
  4 # Please consider adding local content in /etc/sudoers.d/ instead of
  5 # directly modifying this file.
  6 #
  7 # See the man page for details on how to write a sudoers file.
  8 #
  9 Defaults        env_reset
 10 Defaults        mail_badpass
 11 Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
 12 
 13 # Host alias specification
 14 
 15 # User alias specification
 16 
 17 # Cmnd alias specification
 18 
 19 # User privilege specification
 20 root    ALL=(ALL:ALL) ALL
 21 
 22 # Members of the admin group may gain root privileges
 23 %admin ALL=(ALL) ALL
 24 
 25 # Allow members of group sudo to execute any command
 26 %sudo   ALL=(ALL:ALL) ALL
 27 
 28 # See sudoers(5) for more information on "#include" directives:
 29 
 30 #includedir /etc/sudoers.d

コマンドで行20(ルートALL=(ALL:ALL) ALLの場合)を見つけて、行21に以下のコンテンツを追加します。

test    ALL=(ALL:ALL) ALL

Linux コマンドでそれができるのでしょうか、それとも Perl や Python などのコーディングでしかできないのでしょうか?

私は Linux の初心者なので、どんな助けでも大歓迎です!

答え1

どこに行を追加するかは関係ありません。sudo は順序を気にしません。

最後に追加するだけです:

echo 'test    ALL=(ALL:ALL) ALL' | sudo tee -a /etc/sudoers

または、さらに良い方法として、include ディレクトリを使用します。

echo 'test    ALL=(ALL:ALL) ALL' | sudo tee /etc/sudoers.d/test

こうすることで、パッケージの更新中に sudoers ファイルが変更されても、手動で更新する必要がなくなります。

関連情報