【9.16】shell 基础(上)
8.1 shell介绍
-
什么是shell?
-
shell是一个命令解释器,提供用户和机器之间的交互
-
支持特定语法,比如逻辑判断、循环
-
每个用户都可以有自己特定的shell
-
CentOS7默认shell为bash(Bourne Agin Shell)
-
还有zsh、ksh等
8.2 命令历史
方向键 ↑ 可以查看之前用过的命令,命令存放在家目录 ~/.bash_history
- history 查看之前使用过的所有命令
[root@arslinux-01 ~]# history
1 curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
2 python get-pip.py
3 pip install shadowsocks
4 vi /etc/shadowsocks.json
5 vi /etc/systemd/system/shadowsocks.service
6 systemctl enable shadowsocks
7 systemctl start shadowsocks
8 systemctl status shadowsocks
9 yum -y install wget
10 history
- 命令历史最大可保存1000条,由环境变量 $HISTSIZE 定义
[root@arslinux-01 ~]# echo $HISTSIZE
1000
-
执行的命令,并不是实时写入到 bash_history 中,而是暂时存放在内存中,当退出终端时,才存入
-
清空当前内存命令历史 history -c,但是无法清空配置文件bash_history
-
/etc/profile 中定义环境变量 $HISTSIZE
-
修改 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
[root@alexis ~]# history
1 2019/09/16 12:22:48
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
2 2019/09/16 12:22:55
python get-pip.py
3 2019/09/16 12:23:02
pip install shadowsocks
4 2019/09/16 12:23:09
vi /etc/shadowsocks.json
5 2019/09/16 12:24:32
vi /etc/systemd/system/shadowsocks.service
6 2019/09/16 12:24:49
systemctl enable shadowsocks
7 2019/09/16 12:24:54
systemctl start shadowsocks
8 2019/09/16 12:24:58
systemctl status shadowsocks
9 2019/09/16 13:00:02
yum -y install wget
10 2019/09/16 13:06:58
history
11 2019/09/16 13:10:02
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
12 2019/09/16 13:10:18
history
- 想要永久生效,可以写入到 /etc/profie 中去
[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,只能追加,不能删除
Tips:
运行上一条命令 !!
运行第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'
- 其余别名存放在 /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
olorg
- 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
[root@arslinux-01 ~]# lsss 234/ 2> a.txt
[root@arslinux-01 ~]# cat a.txt
-bash: lsss: 未找到命令
[root@arslinux-01 ~]# lsss 234/ 2>> a.txt
[root@arslinux-01 ~]# cat a.txt
-bash: lsss: 未找到命令
-bash: lsss: 未找到命令
[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
[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
[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
[1]+ 已停止 vim 1.txt
[root@arslinux-01 ~]# vim 2.txt
[2]+ 已停止 vim 2.txt
[root@arslinux-01 ~]# jobs
[1]- 已停止 vim 1.txt
[2]+ 已停止 vim 2.txt
[root@arslinux-01 ~]# fg
vim 2.txt
[2]+ 已停止 vim 2.txt
[root@arslinux-01 ~]# fg 1
vim 1.txt
[1]+ 已停止 vim 1.txt
[root@arslinux-01 ~]# fg 2
vim 2.txt
[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 &