五周第四次课(1月11日)

8.6 管道符和作业控制

8.7/8.8 shell变量

8.9 环境变量配置文件

扩展 bashrc和bash_profile的区别 http://ask.apelearn.com/question/7719

简易审计系统: http://www.68idc.cn/help/server/linux/2014042190951.html

关于PROMPT_COMMAND环境变量的含义 http://www.linuxnote.org/prompt_command-environment-variables.html

1 管道符、作业控制
  • 管道符|,将前一个指令的输出作为后一个指令的输入
[root@localhost ~]# ls
1.txt  anaconda-ks.cfg  httpd-2.4.29         initial-setup-ks.cfg
2.txt  a.txt            httpd-2.4.29.tar.gz  [root@localhost
[root@localhost ~]# ls |wc -l
8
[root@localhost ~]# find ./ -type f |wc -l
2831
  • 作业控制 进程运行时,Ctrl+Z组合,可以使它暂停,让后用fg命令恢复
[root@localhost ~]# vim 1.txt

[1]+  已停止               vim 1.txt
[root@localhost ~]# fg
vim 1.txt

1111
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                         
  • bg,使它到后台运行
  • jobs命令,查看被暂停或者后台运行的任务
[root@localhost ~]# vi aaa.txt

[2]+  已停止               vi aaa.txt
[root@localhost ~]# jobs
[1]-  已停止               vim 1.txt
[2]+  已停止               vi aaa.txt
[root@localhost ~]# fg
vi aaa.txt

[2]+  已停止               vi aaa.txt
[root@localhost ~]# bg
[2]+ vi aaa.txt &  //&后台运行符号

[2]+  已停止               vi aaa.txt
  • wmstat 1 显示系统进程
[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 1430272    932 242072    0    0    10     1   21   30  0  0 100  0  0
 0  0      0 1430272    932 242072    0    0     0     0   46   57  0  0 10
  • Sleep
[root@localhost ~]# jobs
[1]   已停止               vim 1.txt
[2]   已停止               vi aaa.txt
[3]   已停止               vmstat 1
[4]-  已停止               vmstat 1
[5]+  已停止               vmstat 1
[root@localhost ~]# sleep 1000
^Z        
[6]+  已停止               sleep 1000
  • ps aux查看进程
[root@localhost ~]# ps aux |grep sleep
root       4450  0.0  0.0 107908   608 pts/1    T    22:08   0:00 sleep 1000
root       4531  0.0  0.0 107908   608 ?        S    22:17   0:00 sleep 60
root       4533  0.0  0.0 112676   976 pts/1    S+   22:18   0:00 grep --color=auto sleep
2 变量
  • env列出系统预设的全部系统变量
[root@localhost ~]# env
XDG_SESSION_ID=16
HOSTNAME=localhost.localdomain
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.72.1 54649 22
SSH_TTY=/dev/pts/1
USER=root
  • set命令多了很多变量,并且包括用户自定义的变量

  • 自定义变量 a=111

[root@localhost ~]# a=111
[root@localhost ~]# echo $a
111
  • [ ] 变量名规则:字母、数字下划线,首位不能为数字
[root@localhost ~]# a1=2
[root@localhost ~]# echo $a1
2
[root@localhost ~]# a_1=3
[root@localhost ~]# echo $a_1
3
  • [ ] 变量值有特殊符号时需要用单引号括起来
[root@localhost ~]# a="ab$cd"
[root@localhost ~]# echo $a
ab  //双引号会将特殊符号脱义处理
  • [ ] 变量的累加,这里用双引号
[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# echo $a$b
12
[root@localhost ~]# c=a"$b"c
[root@localhost ~]# echo $c
a2c
  • [ ] 全局变量export b=2

[root@localhost ~]# w
 23:07:18 up  3:54,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.72.1     23:07    5.00s  0.06s  0.06s -bash
root     pts/1    192.168.72.1     21:16    6.00s  0.53s  0.00s w
[root@localhost ~]# echo $SSH_TTY
/dev/pts/1
  • 本地变量xavier,只能作用在当前系统
[root@localhost ~]# xavier=linux
[root@localhost ~]# echo $xavier
linux
[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─abrt-dbus───2*[{abrt-dbus}]
        ├─2*[abrt-watch-log]
  • bash是一个shell mark
  • [ ] unset变量
[root@localhost ~]# unset xavi
[root@localhost ~]# echo $xavi
3 环境变量配置文件
  • [ ] /etc/profile 用户环境变量,交互,登录才执行
  • [ ] /etc/bashrc 用户不用登录,执行shell就生效
  • [ ] ~/.bashrc

mark

  • [ ] ~/.bash_profile mark

  • [ ] ~/.bash_history

  • [ ] ~/.bash_logout

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

mark

mark

mark

[root@localhost ~]# vim /etc/bashrc
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# echo $PS1
[\u@\h \W]\$