Shell学习——Shell分类:登录shell和非登陆shell 交互shell和非交互shell

1、从两个不同维度来划分,是否交互式,是否登录
2、交互式shell和非交互式shell
  • 交互式模式:在终端上执行,shell等待你的输入,并且立即执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、退出。当你退出后,shell也终止了。
  • 非交互式模式:以shell script(非交互)方式执行。在这种模式 下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾EOF,shell也就终止了。
  • 可以通过打印“$-”变量的值(代表着当前shell的选项标志),查看其中的“i”选项(表示interactive shell)来区分交互式与非交互式shell。
[ root@client02 ~]# echo $-
himBH
[ root@client02 ~]# 
 
[ root@client02 ~]# cat test.sh 
echo $-
[ root@client02 ~]# sh test.sh 
hB
[ root@client02 ~]# 
3、登录shell和非登陆shell
  • 登录shell:需要用户名、密码登录后才能进入的shell(或者通过--login”选项生成的shell)。
  • 非登录shell:不需要输入用户名和密码即可打开的Shell,例如:直接命令“bash”就是打开一个新的非登录shell;在Gnome或KDE中打开一个“终端”(terminal)窗口程序也是一个非登录shell。
  • 退出一个登录shell:exit或者logout;退出一个非登录shell:只能exit。
[ root@client02 ~]#  su - zhuwan
Last login: Mon Sep  3 18:26:24 CST 2018 on pts/0
[ zhuwan@client02 ~]$ exit
logout
[ root@client02 ~]#  su - zhuwan
Last login: Mon Sep  3 19:14:45 CST 2018 on pts/0
[ zhuwan@client02 ~]$ logout
[ root@client02 ~]#  su zhuwan
[ zhuwan@client02 root]$ exit
exit
[ root@client02 ~]#  su zhuwan
[ zhuwan@client02 root]$ logout
bash: logout: not login shell: use `exit'
  • 登录shell 时,其bash进程名为”-bash“;非登陆shell时,bash进程名为”bash”
[ root@client02 ~]# su - zhuwan
Last login: Mon Sep  3 19:15:09 CST 2018 on pts/0
[ zhuwan@client02 ~]$ echo $0
-bash
[ zhuwan@client02 ~]$ logout
[ root@client02 ~]# su zhuwan
[ zhuwan@client02 root]$ echo $0
bash
4、登录shell和非登陆shell读取配置文件的区别
  • 登录shell:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) ->( ~/.bashrc -> /etc/bashrc) -> ~/.bash_logout
    • .profile(由Bourne Shell和Korn Shell使用)和.bash_login(由C Shell使用)两个文件是.bash_profile的同义词,目的是为了兼容其它Shell
    • .bash_profile中一般会执行.bashrc
  • 交互式非登陆shell: ~/.bashrc  -> /etc/bashrc 
6、crontab中shell的特点
    crontab脚本,既不是交互式shell,也不是登录shell,不会执行上述的配置文件,有如下两种处理方法:
  • 可以把shebang改为#!/bin/bash -l让脚本用登录Shell来解释执行,这个时候,执行脚本要采用路径执行的方式
  • 调用Bash解释器,加-l参数,即 /bin/bash -l script

猜你喜欢

转载自www.cnblogs.com/pigwan7/p/9593540.html