自動的に取得したシェルスクリプトを/etc/profileに書き込む方法

自動的に取得したシェルスクリプトを/etc/profileに書き込む方法

ログイン時にbashが/etc/profileにあるファイルを自動的にインポートするというニュースを聞きました。

/etc/profileに簡単な内容を書いてみました。

 echo "echo 'foo'" > /etc/profile/foo.sh

ところで、次のような奇妙なエラーが発生します。

bash: /etc/profile/foo.sh: Not a directory

正しい方法がありますか?

ベストアンサー1

/etc/profileファイルです。したがって、生成しようとするとエラーが発生します/etc/profile/foo.sh

/etc/profile.dあなたが考慮しているディレクトリです。そこにあるスクリプトはログイン時に取得されます。あなたの例では/etc/profile.d/foo.sh

以下に、その背後にあるスクリプトロジックと導入方法を見ることができます。同様のコードが/etc/profile/etc/bashrc/etc/csh.cshrcあります/etc/csh.login

$ grep -A 8 ^for.*profile.d /etc/profile
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
$

これらのスクリプトを生成して呼び出す例:

# echo id >/etc/profile.d/foo.sh
# su - steve
Last login: Sat Jun 23 21:44:41 UTC 2018 on pts/0
uid=1000(steve) gid=1001(steve) groups=1001(steve),4(adm),39(video),1000(google-sudoers) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
$

より多くの情報が必要な場合は訪問してください/etc/profile.dのスクリプトは何をしますか?

おすすめ記事