shell的交互式和非交互式登陆

工做中常常遇见环境变量加载问题,归根结底就是配置文件的加载问题。shell

通常会有四种模式:交互式登录、非交互式登录、交互式非登录、非交互非登录。bash

交互式和非交互式对环境变量的加载:ssh

+----------------+--------+-----------+---------------+
|                | login  |interactive|non-interactive|
|                |        |non-login  |non-login      |
+----------------+--------+-----------+---------------+
|/etc/profile    |   A    |           |               |
+----------------+--------+-----------+---------------+
|/etc/bash.bashrc|        |    A      |               |
+----------------+--------+-----------+---------------+
|~/.bashrc       |        |    B      |               |
+----------------+--------+-----------+---------------+
|~/.bash_profile |   B1   |           |               |
+----------------+--------+-----------+---------------+
|~/.bash_login   |   B2   |           |               |
+----------------+--------+-----------+---------------+
|~/.profile      |   B3   |           |               |
+----------------+--------+-----------+---------------+
|BASH_ENV        |        |           |       A       |
+----------------+--------+-----------+---------------+

bash的每种模式会读取其所在列的内容,首先执行A,而后是B,C。而B1,B2和B3表示只会执行第一个存在的文件。spa


交互式登录:简单示例:code

      a.用户直接登录到机器得到的第一个shellblog

      b.用户使用  ssh user@remote 得到的shell进程

非交互式登录:ip

      bash -l  script.shrem

交互式非登录:terminal

      在已有的shell中运行bash,此时不须要登录

非交互式非登录:

      a. bash script.sh

      b. ssh user@remote command

 

为了更好的理清这几种模式,下面咱们对一些典型的启动方式各属于什么模式进行一个总结:

  • 登录机器后的第一个shell:login + interactive
  • 新启动一个shell进程,如运行bash:non-login + interactive
  • 执行脚本,如bash script.sh:non-login + non-interactive
  • 运行头部有如#!/usr/bin/env bash的可执行文件,如./executable:non-login + non-interactive
  • 经过ssh登录到远程主机:login + interactive
  • 远程执行脚本,如ssh user@remote script.sh:non-login + non-interactive
  • 远程执行脚本,同时请求控制台,如ssh user@remote -t 'echo $PWD':non-login + interactive
  • 在图形化界面中打开terminal:  Linux: non-login + interactive  、Mac OS X上: login + interactive

 

参考连接:http://feihu.me/blog/2014/env-problem-when-ssh-executing-command-on-remote/

相关文章
相关标签/搜索