五周第四次课

8.6 管道符和做业控制 linux

8.7/8.8 shell变量shell

8.9 环境变量配置文件 vim

扩展 bashrc和bash_profile的区别   http://ask.apelbash

8.6 管道符和做业控制 session

管道符|ssh

管道符|用于将前一个指令的输出做为后一个指令的输入。spa

统计1.txt的行数。.net

[root@localhost ~]# cat 1.txt | wc -l
2dns

统计/root目录下的文件和目录(不包括隐藏的)个数。
[root@localhost ~]# ls | wc -l

11
列出当前目录下的文件(包括隐藏文件)个数进程

[root@localhost ~]# find ./ -type f | wc -l
19
[root@localhost ~]#

做业控制

当进行进程时,你可使它暂停(按Ctrl+Z组合键),而后使用fg(foreground的简写)命令恢复它,或是利用bg(background的简写)命令使它到后台运行。此外,你也可使它终止(按Ctrl+C组合键)。

使用vi命令编辑test1.txt,随便输入一些内容,按Esc键后,使用Ctrl+Z组合键暂停任务。此时提示test1.txt已经中止了,而后使用fg命令恢复它,此时又进入刚才的vi窗口了。再次使其暂停,而后输入jobs,能够看到被暂停或者在后台运行的任务。若是想把暂停的任务放在后台从新运行,就是用bg命令。多个被暂停的任务会有编号,使用jobs命令能够看到两个任务,使用bg命令或者fg命令时,则须要在后面加上编号。

如何关掉在后台运行的任务呢?若是你没有退出刚才的shell,那么应该先使用命令fg编号把任务调到前台,而后Ctrl+C组合键结束任务。另外一种状况,关闭当前的shell,再次打开另外一个shell时,使用jobs命令并不会显示在后台运行或者被暂停的任务。要想关闭这些任务,则须要先知道它们的pid。使用&把任务放到后台运行时,会显示pid信息。若是忘掉这个pid,还可使用ps aux命令找到那个进程。若是想结束该进程,须要使用kill命令。kill命令很简单,直接在后面加pid便可。若是遇到结束不了的进程,能够在kill后面加一个选项,即kill -9 [pid]。

ctrl z 临时暂停一个任务,而后就能够退回到命令窗口作其余事情。

[root@localhost ~]# vim 1.txt

[1]+  Stopped                 vim 1.txt
[root@localhost ~]#

fg[id]把任务调到前台,支持多个任务

[root@localhost ~]# vim 2.txt

[2]+  Stopped                 vim 2.txt
[root@localhost ~]# fg 1
vim 1.txt

[1]+  Stopped                 vim 1.txt

 jobs查看后台的任务,能够把中止的或者已经丢到后台的任务列出来

[root@localhost ~]# jobs
[1]+  Stopped                 vim 1.txt
[2]-  Stopped                 vim 2.txt

bg [id]把任务调到后台,就是在后台运行起来。

[root@localhost ~]# bg 2
[2]- vim 2.txt &

[2]+  Stopped                 vim 2.txt

[root@localhost ~]# jobs
[1]-  Stopped                 vim 1.txt
[2]+  Stopped                 vim 2.txt
[root@localhost ~]# fg 2
vim 2.txt

保存退出。
[root@localhost ~]# jobs
[1]+  Stopped                 vim 1.txt

vmstat命令查看系统的运行情况
[root@localhost ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1193296   2084 373572    0    0    85     7   79   84  0  0 98  1  0
 0  0      0 1193296   2084 373604    0    0     0     0   73   76  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     6  126  134  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  149  147  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  113  112  0  1 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  121  125  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  120  121  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  123  127  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  108  105  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  118  126  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  120  119  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  121  124  0  0 100  0  0
 0  0      0 1193296   2084 373604    0    0     0     0  110  109  0  1 100  0  0
 0  0      0 1193172   2084 373604    0    0     0     0  180  165  0  0 100  0  0
 0  0      0 1193172   2084 373604    0    0     0     0  124  125  0  0 100  0  0
 0  0      0 1193172   2084 373604    0    0     0     0  120  124  0  0 100  0  0
 0  0      0 1193172   2084 373604    0    0     0     0  113  115  0  0 100  0  0
 0  0      0 1193172   2084 373604    0    0     0     0  108  113  0  0 100  0  0
^Z
[2]+  Stopped                 vmstat 1

bg将命令调回后台,fg将命令调回前台,ctrl c终止一个命令。

命令sleep +n表示中止n秒。

[root@localhost ~]# sleep 1000
^Z
[1]+  Stopped                 sleep 1000
[root@localhost ~]# jobs
[1]+  Stopped                 sleep 1000

[root@localhost ~]# sleep 200
^Z
[2]+  Stopped                 sleep 200
[root@localhost ~]# jobs
[1]-  Stopped                 sleep 1000
[2]+  Stopped                 sleep 200

fg不加数字,表明调最近的一个暂停的程序到前台。

[root@localhost ~]# fg
sleep 200
^Z
[2]+  Stopped                 sleep 200

bg 加数字,表明将这个程序丢到后台并运行。

[root@localhost ~]# bg 1
[1]- sleep 1000 &
[root@localhost ~]# jobs
[1]-  Running                 sleep 1000 &
[2]+  Stopped                 sleep 200

[root@localhost ~]# fg 1
sleep 1000
^C

[root@localhost ~]# jobs
[2]+  Stopped                 sleep 200
[root@localhost ~]# fg
sleep 200
[root@localhost ~]# jobs

[root@localhost ~]# 

命令后面加&直接丢到后台

[root@localhost ~]# sleep 100 &
[1] 3359
[root@localhost ~]# jobs
[1]+  Running                 sleep 100 &
[root@localhost ~]#

ps aux命令用于查看进程。

[root@localhost ~]# ps aux | grep sleep
root       3359  0.0  0.0 107904   612 pts/0    S    19:50   0:00 sleep 100
root       3367  0.0  0.0 107904   608 ?        S    19:51   0:00 sleep 60
root       3369  0.0  0.0 112660   968 pts/0    S+   19:52   0:00 grep --color=auto sleep
[root@localhost ~]# 

8.7/8.8 shell变量

环境变量PATH,它是shell预设的一个变量。一般,shell预设的变量都是大写的。变量就是使用一个较简单的字符串来代替某些具备特殊意义的设定以及数据。就拿PATH来说,这个PATH就代替了全部经常使用命令的绝对路径的设定。有了PATH这个变量,咱们运行某个命令时,就不须要再输入全局路径,直接输入命令名便可。变量有PATH,HOME,PWD,LOGNAME,能够经过env命令来获取到这些变量。

使用env命令,可列出系统预设的所有系统变量。

登录不一样的用户,这些环境变量的值也不一样。

HOSTNAME:表示主机名称

SHELL:当前用户的shell类型

HISTSIZE:历史记录数

MAIL:当前用户的邮件存放目录

PATH:该变量决定了shell将到哪些目录中寻找命令或者程序

PWD:当前目录

LANG:这是与语言相关的环境变量,多语言环境能够修改此环境变量

HOME:当前用户的家目录

LOGNAME:当前用户的登陆名

env命令显示的变量只是环境变量,系统预设的变量还有不少,可使用set命令把系统预设的所有变量都显示出来。

set命令多了不少变量,而且包括用户自定义的变量

set命令不只能够显示系统预设的变量,也能够显示用户自定义的变量。

[root@localhost ~]# a=111
[root@localhost ~]# echo $a

111
[root@localhost ~]#

这个变量和系统变量不太同样,是用户自定义的,不是系统内置的。

虽然能够自定义变量,可是该变量只能在当前shell中生效。使用bash命令能够再打开一个shell,此时先前设置的变量已经不存在了,退出当前的shell回到原来的bash,设定的变量还在。

想让设置的变量环境一直有效的两种状况以下:

状况一:

容许系统内全部用户登陆后都能使用该变量。具体方法:在/etc/profile文件的最后一行加入export myname=Aming,而后运行source /etc/profile就能够生效了。此时再运行bash或者切换到其余帐户就能够看到效果了。

状况二:

仅容许当前用户使用该变量。具体方法:再用户主目录下的.bashrc文件的最后一行加入

export myname=Aming,而后运行source .bashrc就能够生效了。此时再运行bash就能够看到效果了。

这里source的做用是将目前设定的配置刷新,即不用注销再登录也能生效。

env和set命令不用过多纠结。

变量的定义规则

在linux下自定义设置变量的规则以下:

一、设定变量的格式为a=b,其中a为变量名,b为变量的内容,等号两边不能有空格。

二、变量名只能由字母、数字以及下划线组成,并且不能以数字开头。

三、当变量内容带有特殊符号(如空格)时,须要加上单引号。

四、当变量内容中自己带有单引号时,此时就须要加双引号了。

五、当变量内容须要用到其余命令时,运行结果则可使用反引号。

[root@localhost ~]# a1=2
[root@localhost ~]# echo $a1

2
[root@localhost ~]# a_1=3
[root@localhost ~]# echo $a_1

3
[root@localhost ~]# _a1=4
[root@localhost ~]# echo $_a1

4

[root@localhost ~]# 1aa=2
bash: 1aa=2: command not found...

[root@localhost ~]# a='a b c'
[root@localhost ~]# echo $a

a b c

[root@localhost ~]# a="a b c"
[root@localhost ~]# echo $a

a b c
[root@localhost ~]#

建议用单引号,双引号没法保证正确性。

[root@localhost ~]# a="a$bc"
[root@localhost ~]# echo $a

a
[root@localhost ~]# a='a$bc'
[root@localhost ~]# echo $a

a$bc
[root@localhost ~]# 

变量内容能够累加其余变量的内容,但须要加上双引号。若是把双引号错加为单引号,则不能获得想要的内容。

[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# echo $a$b

12
[root@localhost ~]# a='a$bc'
[root@localhost ~]# echo $a$b

a$bc2 

[root@localhost ~]# c="a$b"c
[root@localhost ~]# echo $c

a2c

[root@localhost ~]# c='a$b'c
[root@localhost ~]# echo $c

a$bc
[root@localhost ~]# 

双引号和单引号的区别:使用双引号时,不会取消双引号中特殊符号自己的做用;而使用单引号时,里面的特殊字符将所有失去其自己的做用。

如在当前shell中运行bash命令,则会进入一个新的shell,这个shell就是原来shell的子shell。用pstree命令能够查看,若是pstree命令没有安装,能够经过yum install –y psmisc命令安装。pstree会把linux系统中的全部进程以树形结构显示出来。在父shell中设定变量后,进入子shell时,该变量是不会生效的。若是想让这个变量在子shell中生效,则要用到export指令。

其实export的做用就是声明一下这个变量,让该shell的子shell也知道该变量的值。设置变量以后,若是想取消这个变量,只要输入unset 变量名便可。

怎么查看本身在哪一个终端下呢?

[root@localhost ~]# w
 21:25:16 up  3:13,  1 user,  load average: 0.14, 0.05, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    18:13    4.00s  0.19s  0.04s w
[root@localhost ~]# echo $SSH_TTY
/dev/pts/0

打开另外一个终端,查看具体在哪一个终端下。

[root@localhost ~]# w
 21:28:46 up  3:16,  2 users,  load average: 0.46, 0.20, 0.11
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    18:13    2:46   0.15s  0.15s -bash
root     pts/2    192.168.231.1    21:28    6.00s  0.05s  0.01s w
[root@localhost ~]# echo $SSH_TTY
/dev/pts/2
[root@localhost ~]# 

在终端1定义aming=linux,在终端2上是查看不到的。

[root@localhost ~]# aming=linux
[root@localhost ~]# echo $aming

linux
[root@localhost ~]# 

[root@localhost ~]# echo $aming    #终端2下没法查看

[root@localhost ~]# 

在终端1中,再进入一个shell,也就是子shell。

[root@localhost ~]# bash
[root@localhost ~]# w

 21:33:22 up  3:21,  2 users,  load average: 0.08, 0.09, 0.08
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    18:13    2.00s  0.19s  0.00s w
root     pts/2    192.168.231.1    21:28    2:10   0.04s  0.04s -bash
[root@localhost ~]# 

[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─abrt-dbus───2*[{abrt-dbus}]
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─agetty
        ├─alsactl
        ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───2*[{at-spi2-registr}]
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─bluetoothd
        ├─chronyd
        ├─colord───2*[{colord}]
        ├─crond
        ├─cupsd
        ├─2*[dbus-daemon───{dbus-daemon}]
        ├─dbus-launch
        ├─dnsmasq───dnsmasq
        ├─firewalld───{firewalld}
        ├─gdm─┬─X───3*[{X}]
        │     ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}]
        │     │                 │                 ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │     │                 │                 │             │             ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        │     │                 │                 │             │             └─2*[{ibus-daemon}]
        │     │                 │                 │             └─10*[{gnome-shell}]
        │     │                 │                 └─3*[{gnome-session-b}]
        │     │                 └─2*[{gdm-session-wor}]
        │     └─3*[{gdm}]
        ├─gssproxy───5*[{gssproxy}]
        ├─ibus-x11───2*[{ibus-x11}]
        ├─irqbalance
        ├─ksmtuned───sleep
        ├─libvirtd───15*[{libvirtd}]
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mcelog
        ├─packagekitd───2*[{packagekitd}]
        ├─pcscd───2*[{pcscd}]
        ├─polkitd───5*[{polkitd}]
        ├─pulseaudio───2*[{pulseaudio}]
        ├─rngd
        ├─rsyslogd───2*[{rsyslogd}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─smartd
        ├─sshd───sshd─┬─bash───bash───pstree        #咱们在这里
        │             └─bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        ├─upowerd───2*[{upowerd}]
        ├─vmtoolsd───{vmtoolsd}
        ├─wpa_supplicant
        └─xdg-permission-───2*[{xdg-permission-}]
[root@localhost ~]# 

在子shell中查看变量aming,也是没有生效的。

[root@localhost ~]# echo $aming

[root@localhost ~]# 

这个变量aming只在上面那个shell中生效,如今退出子shell。

[root@localhost ~]# exit
exit

[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─agetty
        ├─alsactl
        ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───2*[{at-spi2-registr}]
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─bluetoothd
        ├─chronyd
        ├─colord───2*[{colord}]
        ├─crond
        ├─cupsd
        ├─2*[dbus-daemon───{dbus-daemon}]
        ├─dbus-launch
        ├─dnsmasq───dnsmasq
        ├─firewalld───{firewalld}
        ├─gdm─┬─X───3*[{X}]
        │     ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}]
        │     │                 │                 ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │     │                 │                 │             │             ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        │     │                 │                 │             │             └─2*[{ibus-daemon}]
        │     │                 │                 │             └─10*[{gnome-shell}]
        │     │                 │                 └─3*[{gnome-session-b}]
        │     │                 └─2*[{gdm-session-wor}]
        │     └─3*[{gdm}]
        ├─gssproxy───5*[{gssproxy}]
        ├─ibus-x11───2*[{ibus-x11}]
        ├─irqbalance
        ├─ksmtuned───sleep
        ├─libvirtd───15*[{libvirtd}]
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mcelog
        ├─packagekitd───2*[{packagekitd}]
        ├─pcscd───2*[{pcscd}]
        ├─polkitd───5*[{polkitd}]
        ├─pulseaudio───2*[{pulseaudio}]
        ├─rngd
        ├─rsyslogd───2*[{rsyslogd}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─smartd
        ├─sshd───sshd─┬─bash───pstree        #如今在这里
        │             └─bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        ├─upowerd───2*[{upowerd}]
        ├─vmtoolsd───{vmtoolsd}
        ├─wpa_supplicant
        └─xdg-permission-───2*[{xdg-permission-}]
[root@localhost ~]# 

这种变量叫本地变量,也叫local,只在当前终端下生效,那如何变成全局变量呢?

[root@localhost ~]# export aming=linux
[root@localhost ~]# echo $aming

linux
[root@localhost ~]# bash
[root@localhost ~]# echo $aming

linux

[root@localhost ~]# bash
[root@localhost ~]# pstree

systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─abrt-dbus───2*[{abrt-dbus}]
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─alsactl
        ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───2*[{at-spi2-registr}]
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─bluetoothd
        ├─chronyd
        ├─colord───2*[{colord}]
        ├─crond
        ├─cupsd
        ├─2*[dbus-daemon───{dbus-daemon}]
        ├─dbus-launch
        ├─dnsmasq───dnsmasq
        ├─firewalld───{firewalld}
        ├─gdm─┬─X───3*[{X}]
        │     ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}]
        │     │                 │                 ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │     │                 │                 │             │             ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        │     │                 │                 │             │             └─2*[{ibus-daemon}]
        │     │                 │                 │             └─10*[{gnome-shell}]
        │     │                 │                 └─3*[{gnome-session-b}]
        │     │                 └─2*[{gdm-session-wor}]
        │     └─3*[{gdm}]
        ├─gssproxy───5*[{gssproxy}]
        ├─ibus-x11───2*[{ibus-x11}]
        ├─irqbalance
        ├─ksmtuned───sleep
        ├─libvirtd───15*[{libvirtd}]
        ├─login───bash
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mcelog
        ├─packagekitd───2*[{packagekitd}]
        ├─pcscd───2*[{pcscd}]
        ├─polkitd───5*[{polkitd}]
        ├─pulseaudio───2*[{pulseaudio}]
        ├─rngd
        ├─rsyslogd───2*[{rsyslogd}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─smartd
        ├─sshd───sshd───bash───bash───bash───pstree    #如今有两个子shell
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        ├─upowerd───2*[{upowerd}]
        ├─vmtoolsd───{vmtoolsd}
        ├─wpa_supplicant
        └─xdg-permission-───2*[{xdg-permission-}]
[root@localhost ~]# 

从新打开一个终端,发现以下状况:

[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─abrt-dbus───2*[{abrt-dbus}]
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─alsactl
        ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───2*[{at-spi2-registr}]
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─bluetoothd
        ├─chronyd
        ├─colord───2*[{colord}]
        ├─crond
        ├─cupsd
        ├─2*[dbus-daemon───{dbus-daemon}]
        ├─dbus-launch
        ├─dnsmasq───dnsmasq
        ├─firewalld───{firewalld}
        ├─gdm─┬─X───3*[{X}]
        │     ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}]
        │     │                 │                 ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │     │                 │                 │             │             ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        │     │                 │                 │             │             └─2*[{ibus-daemon}]
        │     │                 │                 │             └─10*[{gnome-shell}]
        │     │                 │                 └─3*[{gnome-session-b}]
        │     │                 └─2*[{gdm-session-wor}]
        │     └─3*[{gdm}]
        ├─gssproxy───5*[{gssproxy}]
        ├─ibus-x11───2*[{ibus-x11}]
        ├─irqbalance
        ├─ksmtuned───sleep
        ├─libvirtd───15*[{libvirtd}]
        ├─login───bash
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mcelog
        ├─packagekitd───2*[{packagekitd}]
        ├─pcscd───2*[{pcscd}]
        ├─polkitd───5*[{polkitd}]
        ├─pulseaudio───2*[{pulseaudio}]
        ├─rngd
        ├─rsyslogd───2*[{rsyslogd}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─smartd
        ├─sshd─┬─sshd───bash───bash───bash
        │      └─sshd───bash───pstree    #如今在这里
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        ├─upowerd───2*[{upowerd}]
        ├─vmtoolsd───{vmtoolsd}
        ├─wpa_supplicant
        └─xdg-permission-───2*[{xdg-permission-}]
[root@localhost ~]# 

在这个终端下,变量aming也不生效。

[root@localhost ~]# echo $aming

[root@localhost ~]# 

缘由在于这两个终端在两个sshd下面,没有冲突,没有交互。

在第一个终端下的子shell中设置全局变量b,看看在父shell中是否生效。

[root@localhost ~]# export b=123

[root@localhost ~]# echo $b

[root@localhost ~]# 123

[root@localhost ~]# exit

[root@localhost ~]# echo $b

[root@localhost ~]# 

子shell设置的全局变量在父shell中是不生效的,所谓的全局变量是向下的。在当前shell中设置全局变量,它的子shelll,子子shell,……等等都是生效的。

变量能够定义,也能够取消,使用unset命令。

[root@localhost ~]# echo $aming
linux
[root@localhost ~]# unset aming
[root@localhost ~]# echo $aming

[root@localhost ~]# 

上面讲的那么多,都是export设置全局变量的用法,在子shell中才能发挥做用。

8.9 环境变量配置文件

这部份内容有难度,可是在实际工做中用的不是不少。

•/etc/profile 用户环境变量,交互,登陆才执行

• /etc/bashrc 用户不用登陆,执行shell就生效

• ~/.bashrc

• ~/.bash_profile

• ~/.bash_history

• ~/.bash_logout

• PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '

上面的文件分两个维度,一个是系统层面,一个是用户层面的。

系统层面的就是?/etc下的文件,例如/etc/profile和/etc/bashrc。

用户层次的是用户家目录下的文件 ,例如.bash_profile、.bashrc、.bash_history、.bash_logout等等。

文件的名字很相似,能够把profile和bashrc各当作一种类型。这两种类型的区别是:profile是用户登陆的时候会加载到,而bashrc是用户或者系统执行一些shell脚本的时候。好比打开一个shell,就会去执行/etc/bashrc的变量或者配置等等。

系统的文件/etc/profile和/etc/bashrc通常不要去编辑,当遇到需求须要编辑的时候,能够去编辑用户家目录下的,也就是用户本身的,这是能够的。

[root@localhost ~]# vim .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

这里能够定义一下PATH,这里只是针对用户生效,若是想要全局生效,就放到/etc/profile下面去。

/etc/profile:这个文件预设了几个重要的变量,例如PATH、USER、LOGNAME、MAIL、INPUTRC、HOSTNAME、HISTSIZE、umask

 /etc/bashrc:这个文件主要预设umask以及PS1,这个PS1就是咱们在输入命令时前面的那串字符。

\u指用户,\h指主机名,\W指当前目录,\$指字符#(若是是普通目录,则显示$),上面两个是系统文件,各个用户的主目录下还有如下几个隐藏文件。

.bash_profile:该文件定义了用户的我的化路径与环境变量的文件名称。每一个用户均可以使用该文件输入专属于本身的shell信息,当用户登陆时,该文件仅仅执行一次。

.bashrc:该文件包含专属于本身的shell的bash信息,当登陆或每次打开新的shell时,该文件会被读取。

.bash_history:该文件用于记录命令历史。

.bash_logout:当退出shell时,会执行该文件。你能够将一些清理的工做放到这个文件中。

它定义用户退出的时候须要作的一些操做,好比说你想每次退出时都删除命令历史,你就能够把删除命令历史的命令放到.bash_logout里面去。

PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '

PS1是在/etc/bashrc里面定义的,在登录系统之后,会出现[root@localhost ~]# 。

最左侧是root,也就是用户登陆的名字。localhost是主机名,而后是你所在的目录最后一层级。

[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# 

[root@localhost network-scripts]# echo $PS1
[\u@\h \W]\$
[root@localhost network-scripts]# 

u是用户,h是主机名,W是目录的最后一个层级,这里的W是能够改为w的。

[root@localhost network-scripts]# PS1='[\u@\h \w]\$'
[root@localhost /etc/sysconfig/network-scripts]#

此时换成了绝对路径。

[root@localhost ~]#ls
123  1.txt  2.txt  3.txt  456  789  anaconda-ks.cfg.1  a.txt  bb.txt  initial-setup-ks.cfg  perl5
[root@localhost ~]#cd 123
[root@localhost ~/123]#

方括号不加也是能够的。

[root@localhost /tmp]#PS1='\u@\h \w\$'
root@localhost /tmp#

方括号换成尖括号也是能够的。

root@localhost /tmp#PS1='<\u@\h \w>\$'
<root@localhost /tmp>#

也可让前面的总体带颜色显示。

<root@localhost /tmp>#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
root@localhost:/tmp# 

那PS2是多少呢?

root@localhost:~# echo $PS2
>
root@localhost:~# for i in `seq 1 100`
> do
> echo $i
> done

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
root@localhost:~# 

root@localhost:~# PS2="#"
root@localhost:~# echo $PS2

#
root@localhost:~# for i in `seq 1 10`
#do 
#echo $i
#done
1
2
3
4
5
6
7
8
9
10
root@localhost:~# 

PS2通常不会动它,改回来便可。

友情连接:阿铭Linux

相关文章
相关标签/搜索