gle*_*man 13
创建帐户时,创建一个名为~/.not_logged_in_yet. 它不必有任何内容,文件只需要存在:
touch ~user/.not_logged_in_yet # create the file
chown user ~user/.not_logged_in_yet # should be owned by the user
chmod u+rw ~user/.not_logged_in_yet # user must be able to delete it
Run Code Online (Sandbox Code Playgroud)
然后,创建一个名为的脚本/etc/profile.d/check_first_login.sh,其中包含
#!/bin/bash
# only do this for interactive shells
if [ "$-" != "${-#*i}" ]; then
if [ -f "$HOME/.not_logged_in_yet" ]; then
echo "Welcome, this is your first login"
rm "$HOME/.not_logged_in_yet"
fi
fi
Run Code Online (Sandbox Code Playgroud)
当您使用 bash 作为 shell 登录时,shell 将提供源代码/etc/profile(请参阅手册中的Bash 启动文件)。/etc/profile源中的所有*.sh文件/etc/profile.d/,因此在其中添加文件使其对您计算机上的所有用户可见。
如果会话是交互式的,您只想打印该消息。从您的个人资料中打印任何内容可能会破坏应该是非交互式的内容(例如ssh-copy-id- 尽管我不知道这是否会特别中断)。bash 变量$-包含一些使用set命令设置的设置:
$ bash -c 'echo $-; set -ex; echo $-'
hBc
+ echo ehxBc
ehxBc
Run Code Online (Sandbox Code Playgroud)
这种形式的参数扩展${-#*i}删除了以“i”结尾的前导子字符串。$-如果是交互式 bash 会话,则仅包含“i”,因此"$-","${i#*i}"如果会话是交互式的,则仅不等于。还有其他方法可以对此进行测试:请参阅此 shell 是否交互式?-- 我使用这种方法是因为 /etc/profile 就是这样做的。