0402课的预习任务+课堂笔记

8.1 shell介绍

·什么是shell?

- shell是一个命令解释器,提供用户和机器之间的交互

- 支持特定语法,比如逻辑判断、循环

- 每个用户都可以有自己特定的shell

- CentOS7默认shell为bash(Bourne Agin Shell)

- 还有zsh、ksh等


8.2 命令历史

方向键 ↑ 可以查看之前用过的命令,命令存放在家目录 ~/.bash_history


history 查看之前使用过的所有命令

[root@arslinux-01 ~]# history

1.png


命令历史最大可保存1000条,由环境变量 $HISTSIZE 定义

[root@arslinux-01 ~]# echo $HISTSIZE
1000

·执行的命令,并不是实时写入到 bash_history 中,而是暂时存放在内存中,当退出终端时,才存入


history -c        清空当前内存命令历史 ,但是无法清空配置文件bash_history

/etc/profile 中定义环境变量 $HISTSIZE

2.png

修改 HISTSIZE 的数值,可以更改命令历史最大保存的数量

想要 HISTSIZE 立刻生效,需要重新进一下终端,或者执行source /etc/profile

[root@arslinux-01 ~]# vim /etc/profile
[root@arslinux-01 ~]# echo $HISTSIZE
1000
[root@arslinux-01 ~]# source /etc/profile
[root@arslinux-01 ~]# echo $HISTSIZE
5000


·改变命令历史格式改变环境变量 HISTTIMEFORMAT

HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"

[root@arslinux-01 ~]# history

3.png

想要永久生效,可以写入到 /etc/profie 中去

4.png

[root@arslinux-01 ~]# vim /etc/profile
[root@arslinux-01 ~]# source /etc/profile
[root@arslinux-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S

永久保存 chattr +a ~/.bash_history,只能追加,不能删除


运行上一条命令                                             !!

运行第n条命令                                            !n

从最后倒着去找以word开头的命令                !word


8.3 命令补全和别名


Tab键,敲一下,敲两下

如果有一个相同开头,按一下就会出现;如果有多个相同开头,需要敲两下

CentOS6中只支持命令补全,参数无法补全;CentOS7中支持参数补全

默认不支持参数补全,需要安装bash-completion

[root@arslinux-01 ~]# yum install -y bash-completion

重启系统后生效


alias 别名=‘命令’ 别名 alias别名给命令重新起个名字

[root@arslinux-01 ~]# alias renet="systemctl restart network.service"
[root@arslinux-01 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias renet='systemctl restart network.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'


·每个用户都有自己配置别名的文件 ~/.bashrc

5.png


·其余别名存放在 /etc/profile.d/ 中

[root@arslinux-01 ~]# ls /etc/profile.d/
256term.csh  bash_completion.sh  colorgrep.sh  colorls.sh  lang.csh  less.csh  sh.local  vim.sh      which2.sh
256term.sh   colorgrep.csh       colorls.csh   csh.local   lang.sh   less.sh   vim.csh   which2.csh
[root@arslinux-01 ~]# vim /etc/profile.d/colorgrep.sh

6.png


unalias 别名 取消别名

[root@arslinux-01 ~]# unalias renet
[root@arslinux-01 ~]# renet
-bash: renet: 未找到命令
[root@arslinux-01 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'


8.4 通配符

*                                 任意个数的字符(包括 0 个)

?                                 任意一个字符

[0-9]                             数字的范围(括号中的字符只去一个)

[123]                             括号中的几个数字(任选其一,或的关系)

[a-z]                             字母范围

[A-Z]                             类似上

[0-9a-zA-Z]                 任意一个数字或字母

{1,2,3,a}                         具体字符

https://blog.csdn.net/karelcn/article/details/83052395


[root@arslinux-01 ~]# ls *.txt
11111.txt  123.txt  1_hard.txt  1_soft.txt  1.txt  222.txt
[root@arslinux-01 ~]# ls *txt*
11111.txt  123.txt  1_hard.txt  1_soft.txt  1.txt  2222.txt.bak  222.txt
[root@arslinux-01 ~]# ls 1*
11111.txt  123.txt  1_hard.txt  1_soft.txt  1.txt  1.txz~
111:
11.txt  222  ars3
123:
yum.log


[root@arslinux-01 ~]# touch 2.txt
[root@arslinux-01 ~]# touch 3.txt
[root@arslinux-01 ~]# touch a.txt
[root@arslinux-01 ~]# touch bb.txt
[root@arslinux-01 ~]# ls ?.txt
1.txt  2.txt  3.txt  a.txt


[root@arslinux-01 ~]# ls [0-2].txt
1.txt  2.txt
[root@arslinux-01 ~]# ls [13].txt
1.txt  3.txt


[root@arslinux-01 ~]# ls [a-f].txt
a.txt  A.txt  b.txt  B.txt  c.txt  C.txt  d.txt  D.txt  e.txt  E.txt  f.txt
[root@arslinux-01 ~]# ls [A-F].txt
A.txt  b.txt  B.txt  c.txt  C.txt  d.txt  D.txt  e.txt  E.txt  f.txt  F.txt


[root@arslinux-01 ~]# ls [0-9a-bA-D].txt
1.txt  2.txt  3.txt  a.txt  A.txt  b.txt  B.txt  c.txt  C.txt  d.txt  D.txt


[root@arslinux-01 ~]# ls {1,2,a,c}.txt
1.txt  2.txt  a.txt  c.txt

系统环境字母顺序是 aAbBcCdDeEfF...xXyYzZ


8.5 输入输出重定向

输出重定向:

>                                          前面正确的信息重定向到文本文件中                                        

>>                                       前面正确的信息追加重定向到文本文件中                                

2>                                        错误重定向到文本文件中

2>>                                     错误追加重定向到文本文件中

&>                                        正确错误都重定向到文本文件中  1>+2>

&>>                                     正确错误都追加重定向到文本文件中

> a.txt 2>b.txt                     正确的输出到a.txt,错误的输出到b.txt

command >1.txt 2>&1       正确的输出到1.txt,错误的输出到通道1,也就是1.txt

<                                          将右边文本中的信息输入重定向到前面的命令

https://www.cnblogs.com/divent/p/5773861.html


>                                   前面正确的信息重定向到文本文件中

[root@arslinux-01 ~]# ls 234/
ars  ars1  arslinux  arslinux1
[root@arslinux-01 ~]# ls 234/ > a.txt
[root@arslinux-01 ~]# cat a.txt
ars
ars1
arslinux
arslinux1

>>                                   前面正确的信息追加重定向到文本文件中

[root@arslinux-01 ~]# ls 234/ >> a.txt
[root@arslinux-01 ~]# cat a.txt
ars
ars1
arslinux
arslinux1
ars
ars1
arslinux
arslinux1

2>                                 错误重定向到文本文件中

[root@arslinux-01 ~]# lsss 234/ 2> a.txt
[root@arslinux-01 ~]# cat a.txt
-bash: lsss: 未找到命令

2>>                                 错误追加重定向到文本文件中

[root@arslinux-01 ~]# lsss 234/ 2>> a.txt
[root@arslinux-01 ~]# cat a.txt
-bash: lsss: 未找到命令
-bash: lsss: 未找到命令

&>                                 正确错误都重定向到文本文件中  1>+2>

[root@arslinux-01 ~]# ls 234/ 323/ &> a.txt
[root@arslinux-01 ~]# cat a.txt
ls: 无法访问323/: 没有那个文件或目录
234/:
ars
ars1
arslinux
arslinux1

&>>                                 正确错误都追加重定向到文本文件中

[root@arslinux-01 ~]# ls 234/ 323/ &>> a.txt
[root@arslinux-01 ~]# cat a.txt
ls: 无法访问323/: 没有那个文件或目录
234/:
ars
ars1
arslinux
arslinux1
ls: 无法访问323/: 没有那个文件或目录
234/:
ars
ars1
arslinux
arslinux1

> a.txt 2>b.txt                 正确的输出到a.txt,错误的输出到b.txt

[root@arslinux-01 ~]# ls 234/ 323/ >a.txt 2> b.txt
[root@arslinux-01 ~]# cat a.txt
234/:
ars
ars1
arslinux
arslinux1
[root@arslinux-01 ~]# cat b.txt
ls: 无法访问323/: 没有那个文件或目录

<                                      将右边文本中的信息输入重定向到前面的命令

[root@arslinux-01 ~]# wc -l a.txt
5 a.txt
[root@arslinux-01 ~]# wc -l < a.txt
5

同上

[root@arslinux-01 ~]# cat a.txt
234/:
ars
ars1
arslinux
arslinux1
[root@arslinux-01 ~]# cat > newfile < a.txt
[root@arslinux-01 ~]# cat newfile
234/:
ars
ars1
arslinux
arslinux1

这里的先将文件中的数据提取到了命令 cat 中 ,然后由 cat 写入到 newfile 中


8.6 管道符和作业控制

管道:将前面命令输出的结果交给后面的命令,用 | 隔开

[root@arslinux-01 ~]# cat a.txt
234/:
ars
ars1
arslinux
arslinux1
[root@arslinux-01 ~]# cat a.txt | wc -l
5
[root@arslinux-01 ~]# cat a.txt | grep x
arslinux
arslinux1
[root@arslinux-01 ~]# ls | wc -l
27

ctrl z                                 暂停一个任务

jobs                                 查看后台的任务

bg [id]                             把任务调到后台 (不加 id 就是最近一次)

fg [id]                             把任务调到前台

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

[root@arslinux-01 ~]# vim 1.txt

11.png

[1]+  已停止               vim 1.txt
[root@arslinux-01 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        28G  2.1G   26G    8% /
devtmpfs        476M     0  476M    0% /dev
tmpfs           487M     0  487M    0% /dev/shm
tmpfs           487M  7.7M  479M    2% /run
tmpfs           487M     0  487M    0% /sys/fs/cgroup
/dev/sda1       197M  105M   93M   54% /boot
tmpfs            98M     0   98M    0% /run/user/0
[root@arslinux-01 ~]# fg
vim 1.txt

11.png

[1]+  已停止               vim 1.txt
[root@arslinux-01 ~]# vim 2.txt

22.png

[2]+  已停止               vim 2.txt
[root@arslinux-01 ~]# jobs
[1]-  已停止               vim 1.txt
[2]+  已停止               vim 2.txt
[root@arslinux-01 ~]# fg
vim 2.txt

22.png

[2]+  已停止               vim 2.txt
[root@arslinux-01 ~]# fg 1
vim 1.txt

11.png

[1]+  已停止               vim 1.txt
[root@arslinux-01 ~]# fg 2
vim 2.txt

22.png

[2]+  已停止               vim 2.txt


bg 就不演示了


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

[root@arslinux-01 ~]# sleep 100 &
[3] 7568
[root@arslinux-01 ~]# jobs
[1]-  已停止               vim 1.txt
[2]+  已停止               vmstat 1
[3]   运行中               sleep 100 &


8.7/8.8 shell变量

PATH环境变量,HOME,PWD,LOGNAME等变量


env 查看系统的变量

[root@arslinux-01 ~]# env
XDG_SESSION_ID=1
HOSTNAME=arslinux-01
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.194.1 9072 22
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/0
USER=root
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:
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/rootLANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.194.1 9072 192.168.194.130 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env


set 查看所有变量(不仅查看系统内置的环境变量,也会有用户自定义的变量)

·自定义变量

[root@arslinux-01 ~]# a=111
[root@arslinux-01 ~]# echo $a
111
[root@arslinux-01 ~]# set |grep 111
_=111
a=111

·变量名规则:字母、数字下划线,首位不能为数字

a1      √

a_1     √

_a1     √

1a      ×

·变量值有特殊符号时需要用单引号括起来

[root@arslinux-01 ~]# a='a b c'
[root@arslinux-01 ~]# echo $a
a b c
[root@arslinux-01 ~]# a=a b c
-bash: b: 未找到命令
[root@arslinux-01 ~]# echo $a
a b c

单引号有脱义的作用

[root@arslinux-01 ~]# a="a$bc"
[root@arslinux-01 ~]# echo $a
a
[root@arslinux-01 ~]# a='a$bc'
[root@arslinux-01 ~]# echo $a
a$bc

变量的累加

[root@arslinux-01 ~]# a=1
[root@arslinux-01 ~]# b=2
[root@arslinux-01 ~]# echo $a$b
12
[root@arslinux-01 ~]# a='a$bc'
[root@arslinux-01 ~]# echo $a$b
a$bc2
[root@arslinux-01 ~]# c="a$bc"
[root@arslinux-01 ~]# echo $c
a
[root@arslinux-01 ~]# c="a$b"c
[root@arslinux-01 ~]# echo $c
a2c
[root@arslinux-01 ~]# c=a"$b"c
[root@arslinux-01 ~]# echo $c
a2c


本地变量

查看自己在哪一个登录的终端

[root@arslinux-01 ~]# w
11:15:07 up  1:18,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    09:57    3.00s  0.14s  0.03s w
root     pts/1    192.168.194.1    11:15    2.00s  0.04s  0.04s -bash
[root@arslinux-01 ~]# echo $SSH_TTY
/dev/pts/0


定义一个变量,在子bash中,自定义的变量没有生效

[root@arslinux-01 ~]# arslinux=ars111
[root@arslinux-01 ~]# echo $arslinux
ars111
[root@arslinux-01 ~]# bash
[root@arslinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
         ├─VGAuthService
         ├─agetty
         ├─auditd───{auditd}
         ├─chronyd
         ├─crond
         ├─dbus-daemon───{dbus-daemon}
         ├─firewalld───{firewalld}
         ├─lvmetad
         ├─master─┬─pickup
         │          └─qmgr
         ├─polkitd───6*[{polkitd}]
         ├─rsyslogd───2*[{rsyslogd}]
         ├─sshd─┬─sshd───bash───bash───pstree##当前位置
         │        └─sshd───bash
         ├─systemd-journal
         ├─systemd-logind
         ├─systemd-udevd
         ├─tuned───4*[{tuned}]
         └─vmtoolsd───{vmtoolsd}
[root@arslinux-01 ~]# echo $arslinux

[root@arslinux-01 ~]#

退回到原bash,自定义变量又生效

[

root@arslinux-01 ~]# exit
exit
[root@arslinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
         ├─VGAuthService
         ├─agetty
         ├─auditd───{auditd}
         ├─chronyd
         ├─crond
         ├─dbus-daemon───{dbus-daemon}
         ├─firewalld───{firewalld}
         ├─lvmetad
         ├─master─┬─pickup
         │          └─qmgr
         ├─polkitd───6*[{polkitd}]
         ├─rsyslogd───2*[{rsyslogd}]
         ├─sshd─┬─sshd───bash───pstree##目前位置
         │        └─sshd───bash
         ├─systemd-journal
         ├─systemd-logind
         ├─systemd-udevd
         ├─tuned───4*[{tuned}]
         └─vmtoolsd───{vmtoolsd}
[root@arslinux-01 ~]# echo $arslinux
ars111

这种仅仅在当前终端下生效的变量叫本地变量


全局变量

export 变量=值 定义全局变量(全局变量只在它和它的子shell中生效)

[root@arslinux-01 ~]# export arslinux=ars1101
[root@arslinux-01 ~]# echo $arslinux
ars1101
[root@arslinux-01 ~]# bash
[root@arslinux-01 ~]# echo $arslinux
ars1101
[root@arslinux-01 ~]# bash
[root@arslinux-01 ~]# echo $arslinux
ars1101

定义的全局在其他终端下不生效


在子shell中定义的全局变量,无法在上一层shell中生效

[root@arslinux-01 ~]# export b=ars12345
[root@arslinux-01 ~]# echo $b
ars12345
[root@arslinux-01 ~]# exit
exit
[root@arslinux-01 ~]# echo $b

[root@arslinux-01 ~]#


unset 变量名        取消变量

[root@arslinux-01 ~]# echo $arslinux
ars1101
[root@arslinux-01 ~]# unset arslinux
[root@arslinux-01 ~]# echo $arslinux

[root@arslinux-01 ~]#


8.9 环境变量配置文件


系统的环境变量配置文件有两个维度:系统层次、用户层次


系统层次:/etc/profile    用户环境变量,交互,登录才执行

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

profile和bashrc的区别是,profile是用户登录时加载执行的,而bashrc是执行shell脚本时生效的

(以上两个目录请勿乱动,平时编辑用户家目录下的即可,系统层次的可以全局生效,而用户层次只针对该用户生效)


配置文件被修改后,可以用source 文件或者 . 文件来重新加载

例如:source /etc/profile 或 . /etc/profile


户层次:~/.bashrc                    用户命令别名等个性化

                ~/.bash_profile            用户设置环境信息

                ~/.bash_history             存放用户命令历史

                ~/.bash_logout             用来定义用户退出时需要做的一些操作

如果想让用户每次退出时都删除命令历史,那么可以用把删除命令历史的命令放到 .bash_logout 中去


变量PS1定义提示符,在 /etc/bashrc 下修改

7.png

[root@arslinux-01 ~]# PS1='[\u@\h \w]\$'
[root@arslinux-01 ~]#echo $PS1
[\u@\h \w]\$
[root@arslinux-01 ~]#cd /etc/sysconfig/network-scripts/
[root@arslinux-01 /etc/sysconfig/network-scripts]#PS1='\u@\h \w\$'
root@arslinux-01 /etc/sysconfig/network-scripts#

将 $PS1 中的 W 改为 w,路径变为了绝对路径,去掉方括号的话,同样提示符方括号也会去掉

$PS1 的 [\u@\h \w]\$$ ,普通用户是 $ ,root 用户是 #


可以设置带颜色显示

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


PS2 是用在另一种环境里的,例如mysql中会使用到

[root@arslinux-01 ~]#echo $PS2
>
[root@arslinux-01 ~]#for i in `seq 1 10`
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10



扩展

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





0402课堂笔记


bashrc和bash_profile

http://ask.apelearn.com/question/7719

http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

==> /etc/profile

==> ~/.bash_profile

~/.bash_login

~/.profile

==> ~/.bashrc

==> /etc/bashrc

==> ~/.bash_logout


关于PROMPT_COMMAND环境变量的含义

http://www.linuxnote.org/prompt_command-environment-variables.html


PROMPT_COMMAND='ECHO -NE "\033[31mAming\033[0m\033[33m teach\033[0m\033[32m Linux\033[0m"'


echo 颜色:http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html


source和exec的区别

http://alsww.blog.51cto.com/2001924/1113112


cat 2.sh
#!/bin/bash
echo "children script PID is $$"
echo "now to children script."
export a=2
echo "in children, a=$a"


cat source.sh
#!/bin/bash
echo "fater script PID is $$"
echo "fater script, set a is 1"
export a=1
echo "it will source ./2.sh"
source ./2.sh
echo "now to father script."
echo "fater script, a is $a"


cat exec.sh
#!/bin/bash
echo "fater script PID is $$"
echo "fater script, set a is 1"
export a=1
echo "it will exec ./2.sh"
exec ./2.sh
echo "fater script, a is $a"


exec:执行子级的命令后,不再执行父级命令

source:执行子级命令后继续执行父级命令


找具体哪个包安装:yum provides "/*/*bin/vim"



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章