ysl2 / .dotfiles

My dotfiles
2 stars 0 forks source link

关于~/.profile和[login shell/non-login shell]的问题 #18

Open ysl2 opened 11 months ago

ysl2 commented 11 months ago

Ref:

Difference between login shell and non-login shell.

https://linux.vbird.org/linux_basic/centos7/0320bash.php#settings_bashrc

ysl2 commented 11 months ago
-rw-------  1 yusongli yusongli   5826 Oct 24 22:49 .bash_history
-rw-r--r--  1 yusongli yusongli    220 Aug  4  2020 .bash_logout
lrwxrwxrwx  1 yusongli yusongli     15 Aug  7 13:33 .bashrc -> .dotlib/.bashrc
-rw-r--r--  1 yusongli yusongli   1203 Oct 24 23:00 .bashrc.localhost.post
lrwxrwxrwx  1 yusongli yusongli     16 Oct 24 18:59 .profile -> .dotlib/.profile
-rw-r--r--  1 yusongli yusongli     19 Oct 24 22:59 .profile.localhost

localhost文件被相应的主文件调用。每个文件的最后一行是写入log。

首先采用常规lightdm登录deepin-de,然后直接用文件浏览器看log

image

然后再打开alacritty进入bash,再次看log

image

sudo systemctl disable lightdm,采用自动exec startx的方式登录dwm,然后什么都不做直接关机,用livecd看log

image

然后删掉log,重新登录dwm,用alacritty打开bash,查看log

image

ysl2 commented 11 months ago

补充实验:

dwm登录后打开bash (auto startx)

image

然后关了bash再开一次

image

用lightdm登录deepin-de,直接用文件管理器看log (注意此时忘记清上面的log,因此log的前几行是上次的内容)

image

然后打开bash

image

ysl2 commented 11 months ago

image

ysl2 commented 11 months ago

总结:

对于deepin使用lightdm登录无论是deepin-de还是dwm,都会加载~/.profile

使用exec startx登录,也会加载~/.profile

关键在于,startx一旦exec,整个文件后面的所有内容全都检测不到。

另外,在登录的时候,会加载/etc/profile~/.profile,不会主动加载~/.bashrc除非在~/.profile中强制触发它。

在登录完成之后,如果再打开新的bash,此时只会加载~/.bashrc,不会加载/etc/profile~/.profile

ysl2 commented 11 months ago

但是注意,关于键盘改建,如果想一登录就自动改键位:

首先,对于exec startx方式,如果放在~/.profile的startx之前改键,会提示不在x11中,报错。如果放在~/.profle的startx之后,此时因为exec又检测不到。说明这种方式无法实现,需要依赖其他方法比如dwm的autorun补丁,在进入dwm之后再运行改键脚本。

其次,对于lightdm方式,~/.profile是被从头到尾加载一遍的。之前的改键失效可能是由于没有使用正确的语法,导致未加载~/.profile.localhost

ysl2 commented 11 months ago

一个细节,在~/.profile中,关于bashrc的强制加载,是仅限于当打开bash的时候,因为它会检测$BASH_VERSION变量。

因此,

对于lightdm找不到自定义的$PATH,是因为lightdm直接跳过了bash,也就没加载bashrc直接进的系统。

对于exec startx如果把加载bashrc放在它的前面能找到,是因为通过exec startx实际上也是先进入的bash (此时有$BASH_VERSION变量),再exec startx。就从密码正确进入bash,到exec之前,这个短暂的时间差,就已经加载到了bashrc。

ysl2 commented 11 months ago

初步解决方案(需进一步查明正确性):

exec startx应该放在~/.profile的末尾。

改键的脚本不应该在~/.profile中出现,而是在桌面环境进入之后再执行。比如dwm的autorun补丁,进入dwm之后执行改键。或者是放在deepin-de的~/.config/autostart,进入桌面之后自启动改键。

自定义的PATH应该优先往~/.profile里面放,但也要考虑non-login shell是否每次都能加载到。

考虑自动加载tmux的是不是也应该优先往~/.profile里面放。

ysl2 commented 11 months ago

tmux每打开一个pane,都是只加载~/.profile,然后通过~/.profile去加载~/.bashrc

ysl2 commented 11 months ago

下面这个图有错误,但是意思大体是这样的

                  │                                                                       │
                  │                                                                       │
                  │                                        ┌──────────┐                   │
                  │                                        │~/.profile│                   │
                  │                                        └────┬─────┘                   │
                  │                                             │                         │
                  │                         ┌───────────────────┴───────────────────┐     │
                  │         2.              ▼                                       ▼     │
                  │       ┌──────┐     ┌──────────┐                              ┌──────┐ │
                  │       │startx│     │~/.bashrc │                           1. │startx│ │
                  │       └──────┘     └────┬─────┘                              └──────┘ │
                  │           │             │                                             │
                  │    ┌──────┼───────┬─────┴─────┬───────────────┐                       │
                  │    │      │       │           │               │                       │
                  │    ▼      ▼       ▼           ▼               ▼                       │
                  │ ┌───┐ (check   ┌────┐       ┌─────┐       ┌──────┐                    │
                  │ │ENV│  return) │TMUX│       │conda│       │others│                    │
                  │ └───┘          └────┘       └─────┘       └──────┘                    │
──────────────────┼───────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────►
      lightdm     │   Y  $- != *i*   -             -             -                  -     │ ~/.profile + ~/.bashrc
      startx      │   Y  -z "$DISP"  -             -             -                  Y     │ ~/.profile + ~/.bashrc
      bash tmux   │   Y  checktmux   Y             -             Y                  -     │ ~/.profile + ~/.bashrc
      bash tm co  │   Y  checktmux   Y  checkconda Y             Y                  -     │ ~/.profile + ~/.bashrc
      bash conda  │   Y              -  checkconda Y             Y                  -     │ ~/.bashrc
      bash pure   │   Y              -             -             Y                  -     │ ~/.bashrc
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  │                                                                       │
                  ▼                                                                       ▼