8.6、管道符和作业控制
- cat 1.txt |wc -l cat 1.txt |grep 'aaa'
- 管道符的作用就是把前面的命令输出的结果,交给后面的命令处理
1. [root@localhost ~]# touch 1.txt
2. [root@localhost ~]# echo "123123123123123123" >>1.txt
3. [root@localhost ~]# echo "123123123123123123" >>1.txt
4. [root@localhost ~]# echo "123123123123123123" >>1.txt
5. [root@localhost ~]# echo "123123123123123123" >>1.txt
6. [root@localhost ~]# echo "123123123123123123" >>1.txt
7. [root@localhost ~]# echo "123123123123123123" >>1.txt
8. [root@localhost ~]# echo "123123123123123123" >>1.txt
9. [root@localhost ~]# echo "123123123123123123" >>1.txt
10. [root@localhost ~]# echo "123123123123123123" >>1.txt
11. [root@localhost ~]# echo "123123123123123123" >>1.txt
12. [root@localhost ~]# echo "123123123123123123" >>1.txt
13. [root@localhost ~]# echo "123123123123123123" >>1.txt
14. [root@localhost ~]# echo "123123123123123123" >>1.txt
15. [root@localhost ~]# cat 1.txt |wc -l
16. 13
17. [root@localhost ~]# cat 1.txt |grep '123'
18. 123123123123123123
19. 123123123123123123
20. 123123123123123123
21. 123123123123123123
22. 123123123123123123
23. 123123123123123123
24. 123123123123123123
25. 123123123123123123
26. 123123123123123123
27. 123123123123123123
28. 123123123123123123
29. 123123123123123123
30. 123123123123123123
31. [root@localhost ~]#
- 作业控制 Ctrl + c、Ctrl +z、bg、fg
1. [root@localhost ~]# vi 1.txt
2.
3. [1]+ 已停止 vi 1.txt
4. [root@localhost ~]# bg
5. [1]+ vi 1.txt &
6.
7. [1]+ 已停止 vi 1.txt
8. [root@localhost ~]# fg
9. vi 1.txt
10.
11. [1]+ 已停止 vi 1.txt
12. [root@localhost ~]# jobs
13. [1]+ 已停止 vi 1.txt
14. [root@localhost ~]# fg 1
15. vi 1.txt
16.
17. [1]+ 已停止 vi 1.txt
18. [root@localhost ~]# bg 1
19. [1]+ vi 1.txt &
20. [root@localhost ~]#
- jobs查看当前后台运行的命令
- sleep 1000
1. [root@localhost ~]# sleep 1000
2. jobs
3. ^K^Z
4. [2]+ 已停止 sleep 1000
5. [root@localhost ~]# jobs
6. [1]- 已停止 vi 1.txt
7. [2]+ 已停止 sleep 1000
8. [root@localhost ~]# bg 2
9. [2]+ sleep 1000 &
10. [root@localhost ~]# fg
11. vi 1.txt
12.
13. [1]+ 已停止 vi 1.txt
14. [root@localhost ~]# jobs
15. [1]+ 已停止 vi 1.txt
16. [2]- 运行中 sleep 1000 &
17. [root@localhost ~]# fg 2
18. sleep 1000
- jobs只能在当前终端下查看后台运行,可以使使用ps aux |grep sleep查看进程
8.7、shell变量(上)
-
变量
-
PATH,HOME,PWD,LOGNAME
-
env命令
-
set命令多了很多变量,并且包括用户自定义的变量
-
自定义变量a=1
-
变量名规则:字母、数字、下划线,首位不能为数字
-
变量值有特殊符号时需要用单引号括起来
-
变量的累加
-
全局变量export b=2
-
unset变量
-
env查看系统的环境变量
1. [root@localhost ~]# env
2. XDG_SESSION_ID=1
3. HOSTNAME=localhost.localdomain
4. SELINUX_ROLE_REQUESTED=
5. TERM=xterm
6. SHELL=/bin/bash
7. HISTSIZE=5000
8. SSH_CLIENT=192.168.153.1 50636 22
9. SELINUX_USE_CURRENT_RANGE=
10. SSH_TTY=/dev/pts/0
11. USER=root
12. LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
13. MAIL=/var/spool/mail/root
14. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
15. PWD=/root
16. LANG=zh_CN.UTF-8
17. SELINUX_LEVEL_REQUESTED=
18. HISTCONTROL=ignoredups
19. SHLVL=1
20. HOME=/root
21. LOGNAME=root
22. SSH_CONNECTION=192.168.153.1 50636 192.168.153.128 22
23. LESSOPEN=||/usr/bin/lesspipe.sh %s
24. XDG_RUNTIME_DIR=/run/user/0
25. _=/usr/bin/env
26. [root@localhost ~]#
- set查看所有的环境变量,也可自定义环境变量
1. [root@localhost ~]# a=123
2. [root@localhost ~]# echo $a
3. 123
-
可以使用set |grep 123查看环境变量,也可以使用set |less 然后/搜索a=1
-
变量名规则:字母、数字、下划线,首位不能为数字
1. [root@localhost ~]# a_1=1
2. [root@localhost ~]# echo $a_1
3. 1
4. [root@localhost ~]# a1=2
5. [root@localhost ~]# echo $a1
6. 2
7. [root@localhost ~]# _1a=3
8. [root@localhost ~]# echo $_1a
9. 3
10. [root@localhost ~]# b2_=a_1+a1
11. [root@localhost ~]# echo $b2
12.
13. [root@localhost ~]# echo $b2_
14. a_1+a1
15. [root@localhost ~]#
- 变量值有特殊符号时需要用单引号括起来
1. [root@localhost ~]# a2='a b c'
2. [root@localhost ~]# echo $a2
3. a b c
4. [root@localhost ~]# a='a$bc'
5. [root@localhost ~]# echo $a
6. a$bc
7. [root@localhost ~]#
8.8、shell变量(下)
-
全局变量export b=2
-
w查看当前系统有哪些用户在登录,使用echo $SSH_TTY查看当前用户属于哪个TTY
1. [root@localhost ~]# w
2. 21:43:06 up 56 min, 2 users, load average: 0.00, 0.01, 0.05
3. USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
4. root pts/0 192.168.153.1 20:46 2.00s 1.37s 0.06s w
5. root pts/1 192.168.153.1 21:43 3.00s 0.03s 0.03s -bash
6. [root@localhost ~]# echo $SSH_TTY
7. /dev/pts/0
8. [root@localhost ~]#
- 在当前终端1里面定义变量
1. [root@localhost ~]# liyang=linux
2. [root@localhost ~]# echo $liyang
3. linux
4. [root@localhost ~]#
- 在终端2里面是不生效的
- yum install -y *pstree
- bash 打开一个新的bash,然后运行pstree(进程树)
1. [root@localhost ~]# bash
2. [root@localhost ~]# pstree
3. systemd─┬─NetworkManager───2*[{NetworkManager}]
4. ├─agetty
5. ├─auditd───{auditd}
6. ├─chronyd
7. ├─crond
8. ├─dbus-daemon───{dbus-daemon}
9. ├─firewalld───{firewalld}
10. ├─irqbalance
11. ├─master─┬─pickup
12. │ └─qmgr
13. ├─polkitd───5*[{polkitd}]
14. ├─rsyslogd───2*[{rsyslogd}]
15. ├─sshd─┬─sshd───bash─┬─bash───bash───pstree // bash打开一个新的bash后(子shell)
16. │ │ ├─sleep
17. │ │ └─vi
18. │ └─sshd───bash───bash
19. ├─systemd-journal
20. ├─systemd-logind
21. ├─systemd-udevd
22. ├─tuned───4*[{tuned}]
23. └─vmtoolsd───{vmtoolsd}
24. [root@localhost ~]# echo $liyang
25.
26. [root@localhost ~]# exit
27. exit
28. [root@localhost ~]# echo $liyang
29.
30. [root@localhost ~]# exit
31. exit
32. [root@localhost ~]# echo $liyang
33. linux
34. [root@localhost ~]#
-
exit两次才回到sshd下的shell,输入echo $liyang返回值为linux,说明定义的变量只在当前shell下生效(非全局变量或者本地变量)
-
定义全局变量 export ,向下生效(只对当前终端生效)
1. [root@localhost ~]# export liyang=123
2. [root@localhost ~]# echo $liyang
3. 123
4. [root@localhost ~]# bash
5. [root@localhost ~]# echo $liyang
6. 123
7. [root@localhost ~]# pstree
8. systemd─┬─NetworkManager───2*[{NetworkManager}]
9. ├─agetty
10. ├─auditd───{auditd}
11. ├─chronyd
12. ├─crond
13. ├─dbus-daemon───{dbus-daemon}
14. ├─firewalld───{firewalld}
15. ├─irqbalance
16. ├─master───qmgr
17. ├─polkitd───5*[{polkitd}]
18. ├─rsyslogd───2*[{rsyslogd}]
19. ├─sshd─┬─sshd───bash─┬─bash───pstree
20. │ │ ├─sleep
21. │ │ └─vi
22. │ └─sshd───bash───bash
23. ├─systemd-journal
24. ├─systemd-logind
25. ├─systemd-udevd
26. ├─tuned───4*[{tuned}]
27. └─vmtoolsd───{vmtoolsd}
28. [root@localhost ~]#
- unset liyang取消变量
1. [root@localhost ~]# echo $liyang
2. 123
3. [root@localhost ~]# unset liyang
4. [root@localhost ~]# echo $liyang
5.
6. [root@localhost ~]#
8.9、环境变量配置文件
- /etc/profile用户环境变量,交互,登录才能执行(系统层次)
- /etc/bashrc用户不用登录,执行shell就生效(系统层次)
- ~/.bashrc(用户层次)
- ~/.bash_profile(用户层次)
- ~/.bash_history(用户层次)
- ~/.bash_logout(用户层次)
- source==.命令相似,加载更改后的配置文件
1. [root@localhost ~]# vim .bashrc
2.
3. # .bashrc
4.
5. # User specific aliases and functions
6.
7. alias rm='rm -i'
8. alias cp='cp -i'
9. alias mv='mv -i'
10.
11. # Source global definitions
12. if [ -f /etc/bashrc ]; then
13. . /etc/bashrc // 此处的.和source相同
14. fi
- ~/.bash_logout用户退出时需要做的操作
- PS1
1. [root@localhost ~]# vim /etc/bashrc
2.
3.
4. # System wide functions and aliases
5. # Environment stuff goes in /etc/profile
6. # will prevent the need for merging in future updates.
7.
8. # are we an interactive shell?
9. if [ "$PS1" ]; then
10. if [ -z "$PROMPT_COMMAND" ]; then
11. case $TERM in
12. xterm*|vte*)
13. if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
14. PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
15. elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
16. PROMPT_COMMAND="__vte_prompt_command"
17. else
18. fi
19. ;;
20. screen*)
21. if [ -e /etc/sysconfig/bash-prompt-screen ]; then
22. fi
23. ;;
24. *)
25. ;;
26. # Turn on parallel history
27. shopt -s histappend
28. history -a
29. # Turn on checkwinsize
30. shopt -s checkwinsize
31. [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ " // 定义登录的用户名、主机名、当前所在目录
32. # and console windows
33. # If you want to do so, just add e.g.
34. # if [ "$PS1" ]; then
35. # PS1="[\u@\h:\l \W]\\$ "
36. # fi
37. fi
38.
39. if ! shopt -q login_shell ; then # We're not a login shell
40. # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
41. pathmunge () {
42. case ":${PATH}:" in
43. *:"$1":*)
44. ;;
45. *)
46. if [ "$2" = "after" ] ; then
47. PATH=$PATH:$1
48. else
49. PATH=$1:$PATH
50. fi
51. esac
52. }
53.
54. # By default, we want umask to get set. This sets it for non-login shell.
55. # Current threshold for system reserved uid/gids is 200
56. # You could check uidgid reservation validity in
57. # /usr/share/doc/setup-*/uidgid file
58. if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
59. umask 002
60. else
61. umask 022
62. fi
63.
64. SHELL=/bin/bash
65. # Only display echos from profile.d scripts if we are no login shell
66. # and interactive - otherwise just process them to set envvars
67. for i in /etc/profile.d/*.sh; do
68. if [ -r "$i" ]; then
69. if [ "$PS1" ]; then
70. . "$i"
71. else
72. . "$i" >/dev/null
73. fi
74. fi
75. done
76.
77. unset i
78. unset -f pathmunge
79. fi
80. # vim:ts=4:sw=4
81. ~
- 使用echo $PS1查看定义
1. [root@localhost ~]# echo $PS1
2. [\u@\h \W]\$
3. [root@localhost ~]#
4. [root@localhost network-scripts]#PS1='[\u@\h \w]\$' //自定义PS1
5. [root@localhost /etc/sysconfig/network-scripts]# //把W改为w后,由相对路径变为绝对路径
6. [root@localhost /etc/sysconfig/network-scripts]#cd
7. [root@localhost ~]#cd /etc/
8. [root@localhost /etc]#cd /etc/sysconfig/
9. [root@localhost /etc/sysconfig]#
- PS2
1. [root@localhost ~]#PS2='#'
2. [root@localhost ~]#\
3. #
4. [root@localhost ~]#
5. [root@localhost ~]#