8.1、shell介绍
-
Linux shell基础
-
什么是shell
- shell是一个命令解释器,提供用户和机器之间的交互
- 支持特定语法,比如逻辑判断、循环
- 每个用户都可以有自己特定的shell
- centos7默认shell为bash(bourne agin shell)
- 还有zsh、ksh等
1. [root@localhost ~]# yum list |grep zsh
2. autojump-zsh.noarch 22.3.0-3.el7 epel
3. zsh.x86_64 5.0.2-28.el7 base
4. zsh-html.x86_64 5.0.2-28.el7 base
5. zsh-lovers.noarch 0.9.0-1.el7 epel
6. [root@localhost ~]# yum list |grep ksh
7. ksh.x86_64 20120801-34.el7 base
8. mksh.x86_64 46-5.el7 base
9. python-XStatic-Rickshaw.noarch 1.5.0.0-4.el7 epel
10. python-moksha-common.noarch 1.2.3-2.el7 epel
11. python-moksha-hub.noarch 1.5.3-2.el7 epel
12. python-moksha-wsgi.noarch 1.2.2-2.el7 epel
13. [root@localhost ~]#
8.2、命令历史
- history命令
- .bash_history
- 最大1000条命令
- 变量HISTSIZE
- /etc/profile中修改
- HISTTIMEFORMAT="%Y%m%d %H:%M:%S"
- 永久保存chattr +a ~/.bash_history
- !!
- !n
- !word
1. [root@localhost ~]# echo $HISTSIZE
2. 1000
3. [root@localhost ~]#
- history -c只清空内存中的命令,不会删除存命令的配置文件
- 当前输入的命令是不会被直接存放在配置文件中,只有退出当前命令终端后,才会被写入配置文件;
- 环境变量HISTSIZE配置存放在/etc/profile里
1. [root@localhost ~]# vim /etc/profile
2.
3. # /etc/profile
4.
5. # System wide environment and startup programs, for login setup
6. # Functions and aliases go in /etc/bashrc
7.
8. # It's NOT a good idea to change this file unless you know what you
9. # are doing. It's much better to create a custom.sh shell script in
10. # /etc/profile.d/ to make custom changes to your environment, as this
11. # will prevent the need for merging in future updates.
12.
13. pathmunge () {
14. case ":${PATH}:" in
15. *:"$1":*)
16. ;;
17. *)
18. if [ "$2" = "after" ] ; then
19. PATH=$PATH:$1
20. else
21. PATH=$1:$PATH
22. fi
23. esac
24. }
25.
26.
27. if [ -x /usr/bin/id ]; then
28. if [ -z "$EUID" ]; then
29. # ksh workaround
30. EUID=`/usr/bin/id -u`
31. UID=`/usr/bin/id -ru`
32. fi
33. USER="`/usr/bin/id -un`"
34. LOGNAME=$USER
35. MAIL="/var/spool/mail/$USER"
36. fi
37.
38. # Path manipulation
39. if [ "$EUID" = "0" ]; then
40. pathmunge /usr/sbin
41. pathmunge /usr/local/sbin
42. else
43. pathmunge /usr/local/sbin after
44. pathmunge /usr/sbin after
45. fi
46.
47. HOSTNAME=`/usr/bin/hostname 2>/dev/null`
48. HISTSIZE=2000 //此处可以更改history的最大记录数量
49. if [ "$HISTCONTROL" = "ignorespace" ] ; then
50. export HISTCONTROL=ignoreboth
51. else
52. export HISTCONTROL=ignoredups
53. fi
54.
55. export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
56.
57. # By default, we want umask to get set. This sets it for login shell
58. # Current threshold for system reserved uid/gids is 200
59. # You could check uidgid reservation validity in
60. # /usr/share/doc/setup-*/uidgid file
61. if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
62. umask 002
63. else
64. umask 022
65. fi
66.
67. for i in /etc/profile.d/*.sh ; do
68. if [ -r "$i" ]; then
69. if [ "${-#*i}" != "$-" ]; then
70. . "$i"
71. else
72. . "$i" >/dev/null
73. fi
74. fi
75. done
76.
77. unset i
78. unset -f pathmunge
79. [root@localhost ~]# echo $HISTSIZE
80. 2000
81. [root@localhost ~]#
- source /etc/profile可以直接是命令生效(HISTSIZE=5000)
1. [root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" //临时生效
2. [root@localhost ~]# echo $HISTTIMEFORMAT
3. %Y/%m/%d %H:%M:%S
4. [root@localhost ~]#
5. [root@localhost ~]# history
6. 1 2017/11/15 21:14:28history
7. 2 2017/11/15 21:14:28vim /etc/profile
8. 3 2017/11/15 21:14:50echo $HISTSIZE
9. 4 2017/11/15 21:16:20vi /etc/profile
10. 5 2017/11/15 21:16:49echo HISTSIZE
11. 6 2017/11/15 21:16:56echo $HISTSIZE
12. 7 2017/11/15 21:17:06source /etc/profile
13. 8 2017/11/15 21:17:09@e
14. 9 2017/11/15 21:17:13echo $HISTSIZE
15. 10 2017/11/15 21:18:55history
16. 11 2017/11/15 21:19:15echo $HISTSIZE
17. 12 2017/11/15 21:21:06HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
18. 13 2017/11/15 21:21:22echo HISTTIMEFORMAT
19. 14 2017/11/15 21:21:34echo $HISTTIMEFORMAT
20. 15 2017/11/15 21:23:09history
21. [root@localhost ~]#
-
历史记录有时间显示了。
-
如果需要永久生效,只要在环境变量/etc/profile里面加入 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 即可
-
永久保存历史命令不被删除,chattr +a ~/.bash_history //直接关闭命令窗口则不被记录为历史命令
-
!!执行上一条命令
-
!n(n是指数字)
-
!echo,从后面往前找,第一个echo开头的命令
8.3、命令补全和别名
- tab键,敲一下,敲两下
- 参数补全,安装bash-completion
- alias别名给命令重新起名字
- 各用户都有自己配置别名的文件~/.bashrc
- ls /etc/profile.d/
- 自定义的alias放到~/.bashrc
- 安装命令补全:yum install -y bash-completion就可用tab补全后面的命令参数
1. [root@localhost ~]# alias restartnet='systemctl restart network.service'
- 取消别名:unalias restartnet
1. [root@localhost ~]# vi .bashrc
2. [root@localhost ~]# cd /etc/profile.d/
3. [root@localhost profile.d]# ls
4. 256term.csh bash_completion.sh colorgrep.sh colorls.sh lang.sh less.sh vim.sh which2.sh
5. 256term.sh colorgrep.csh colorls.csh lang.csh less.csh vim.csh which2.csh
6. [root@localhost profile.d]# cat colorgrep.sh
7. # color-grep initialization
8. /usr/libexec/grepconf.sh -c || return
9. alias grep='grep --color=auto' 2>/dev/null
10. alias egrep='egrep --color=auto' 2>/dev/null
11. alias fgrep='fgrep --color=auto' 2>/dev/null
12. [root@localhost profile.d]# cat colorls.sh
13. # color-ls initialization
14. # Skip all for noninteractive shells.
15. [ ! -t 0 ] && return
16. #when USER_LS_COLORS defined do not override user LS_COLORS, but use them.
17. if [ -z "$USER_LS_COLORS" ]; then
18. alias ll='ls -l' 2>/dev/null
19. alias l.='ls -d .*' 2>/dev/null
20. INCLUDE=
21. COLORS=
22. for colors in "$HOME/.dir_colors.$TERM" "$HOME/.dircolors.$TERM" \
23. "$HOME/.dir_colors" "$HOME/.dircolors"; do
24. [ -e "$colors" ] && COLORS="$colors" && \
25. INCLUDE="`/usr/bin/cat "$COLORS" | /usr/bin/grep '^INCLUDE' | /usr/bin/cut -d ' ' -f2-`" && \
26. break
27. done
28. [ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.$TERM" ] && \
29. COLORS="/etc/DIR_COLORS.$TERM"
30.
31. [ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.256color" ] && \
32. [ "x`/usr/bin/tty -s && /usr/bin/tput colors 2>/dev/null`" = "x256" ] && \
33. COLORS="/etc/DIR_COLORS.256color"
34. [ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS" ] && \
35. COLORS="/etc/DIR_COLORS"
36. # Existence of $COLORS already checked above.
37. [ -n "$COLORS" ] || return
38. if [ -e "$INCLUDE" ];
39. then
40. TMP="`/usr/bin/mktemp .colorlsXXX -q --tmpdir=/tmp`"
41. [ -z "$TMP" ] && return
42. /usr/bin/cat "$INCLUDE" >> $TMP
43. /usr/bin/grep -v '^INCLUDE' "$COLORS" >> $TMP
44. eval "`/usr/bin/dircolors --sh $TMP 2>/dev/null`"
45. /usr/bin/rm -f $TMP
46. else
47. eval "`/usr/bin/dircolors --sh $COLORS 2>/dev/null`"
48. fi
49. [ -z "$LS_COLORS" ] && return
50. /usr/bin/grep -qi "^COLOR.*none" $COLORS >/dev/null 2>/dev/null && return
51. fi
52. unset TMP COLORS INCLUDE
53. alias ll='ls -l --color=auto' 2>/dev/null
54. alias l.='ls -d .* --color=auto' 2>/dev/null
55. alias ls='ls --color=auto' 2>/dev/null
56. [root@localhost profile.d]#
8.4、通配符
- ls *.txt
- ls ?.txt
- ls [0-9].txt
- ls [1,2].txt
- cat 1.txt >2.txt
- cat 1.txt >>2.txt
- ls aaa.txt 2>err
- ls aaa.txt 2>>err
- wc -l < 1.txt
- command > 1.txt 2>&1
1. [root@centos7-01 ~]# ls *.txt
2. 1_heard.txt 1_sorft.txt 1.txt passwd.txt 学习计划安排.txt
8.5、输入输出重定向
- cat 1.txt >2.txt //输出
- cat 1.txt >>2.txt //追加重定向
- cat 1.txt 2>2.txt //错误重定向
- cat 1.txt 2>>2.txt //错误追加重定向
- 错误和正确都输出>+2> == &>
1. [root@centos7-01 ~]# ls *.txt 999.sh &> 11.txt
2. [root@centos7-01 ~]# cat 11.txt
3. ls: 无法访问999.sh: 没有那个文件或目录
4. 1_heard.txt
5. 1_sorft.txt
6. 1.txt
7. a.txt
8. passwd.txt
9. 学习计划安排.txt
10. [root@centos7-01 ~]#
1. [root@centos7-01 ~]# ls *.txt 123.sh > b.txt 2>c.txt
2. [root@centos7-01 ~]# cat b.txt
3. 11.txt
4. 1_heard.txt
5. 1_sorft.txt
6. 1.txt
7. a.txt
8. passwd.txt
9. 学习计划安排.txt
10. [root@centos7-01 ~]# cat c.txt
11. ls: 无法访问123.sh: 没有那个文件或目录
12. [root@centos7-01 ~]#
- 输入重定向
- wc -l < 1.txt
1. [root@centos7-01 ~]# wc -l < a.txt
2. 2
3. [root@centos7-01 ~]# less -n2 < 1.txt
4. [root@centos7-01 ~]#