shell介绍
shell是一个命令解释器,提供用户与机器之间的交互
CentOS 7 默认shell为bash(Bourne Agin Shell)
shell支持特定语法.如逻辑判断,循环等
每个用户都可以有自己特定的shell
其他shell还有zsh、ksh等
history 命令历史
history
打印用户曾经执行过的命令
[root@localhost ~]# history
1 ip addr
2 vi /etc/sysconfig/network-scripts/ifcfg-eno16777736
3 systemctl restart network.service
4 ifconfig
5 ip add
- 用户家命目录下的
.bash_history
:记录历史命令
[root@localhost ~]# head -n5 .bash_history
ip addr
vi /etc/sysconfig/network-scripts/ifcfg-eno16777736
systemctl restart network.service
ifconfig
ip add
正常退出终端,历史命令才能完整保存。
history
最多记录1000条历史命令,这个数字由变量$HISTSIZE
决定
[root@localhost ~]# echo $HISTSIZE
1000
history -c
清除当前内存中的命令history
的配置文件/etc/profile
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
:在使用history命令时显示命令执行的时间
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@localhost ~]# source /etc/profile
[root@localhost ~]# history
1 2017/07/16 05:04:13history
2 2017/07/16 05:04:47tail -n9 .bash_history
3 2017/07/16 05:08:52cat /etc/profile
4 2017/07/16 05:12:50HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
5 2017/07/16 05:13:33source /etc/profile
6 2017/07/16 05:13:48history
设置了变量
HISTTIMEFORMAT
后,需要用source /etc/profile
才能立即生效。将HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"写入/etc/profile
才能永久生效。
chattr +a /etc/profile
可以永久保存命令历史!!
:表示用户在当前终端执行的最后一条命令
[root@localhost ~]# ls
aaa anaconda-ks.cfg
[root@localhost ~]# !!
ls
aaa anaconda-ks.cfg
!n
:表示用户在当前终端执行的第n条命令(n代表数字)
[root@localhost ~]# !7
ls
aaa anaconda-ks.cfg
!word
:表示用户在当前终端执行的最后一条以word
开头的命令
[root@localhost ~]# mkdir z y x
[root@localhost ~]# ls
aaa anaconda-ks.cfg x y z
[root@localhost ~]# !mkdir
mkdir z y x
mkdir: 无法创建目录"z": 文件已存在
mkdir: 无法创建目录"y": 文件已存在
mkdir: 无法创建目录"x": 文件已存在
tab键的作用
补全命令、路径、参数(补全参数需要安装bash-completion后重启生效)
alias
- 语法`alias 别名='完整命令'
将比较常用而且输入比较麻烦的命令做一个别名,以方便使用
- alias 回车:查看当前用户所有别名(包括自定义的别名)
[root@localhost ~]# 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'
- alias配置文件的位置有两个地方
- 用户家目录下
.bashrc
[root@localhost ~]# cat .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
/etc/profile.d/
目录下
[root@localhost ~]# ls /etc/profile.d/
256term.csh colorgrep.csh colorls.csh lang.csh less.csh which2.csh
256term.sh colorgrep.sh colorls.sh lang.sh less.sh which2.sh
unalias 别名
:取消别名
通配符 *
*
代表0-n
个字符
[root@localhost ~]# ls
1.txt 1.txt.gz 2.txt 2.txt.xz 3.txt 3.txt.zip aaa anaconda-ks.cfg x y z
[root@localhost ~]# ls *.txt
1.txt 2.txt 3.txt
[root@localhost ~]# ls 1.txt*
1.txt 1.txt.gz
[root@localhost ~]# ls *txt*
1.txt 1.txt.gz 2.txt 2.txt.xz 3.txt 3.txt.zip
*
可以放在前面,也可以在后面,或者前后一起
通配符 ?
?
代表任意一个字符
[root@localhost ~]# ls
1.txt 1.txt.gz 2.txt 2.txt.xz 3.txt 3.txt.zip aaa aa.txt anaconda-ks.cfg a.txt x y z
[root@localhost ~]# ls ?.txt
1.txt 2.txt 3.txt a.txt
[0-9a-zA-Z]
:满足方括号内任意条件的一个字符(可以写范围,或者字符)
[root@localhost ~]# ls
1.txt 1.txt.gz 2.txt 2.txt.xz 3.txt 3.txt.zip aaa aa.txt anaconda-ks.cfg a.txt x y z
[root@localhost ~]# ls [0-2].txt
1.txt 2.txt
[root@localhost ~]# ls [a-k].txt
a.txt
[root@localhost ~]# ls [0-9a-z].txt
1.txt 2.txt 3.txt a.txt
[root@localhost ~]# ls [3a].txt
3.txt a.txt
{1,aa}
:满足花括号内任意条件(条件之间用逗号隔开)
[root@localhost ~]# ls
1.txt 1.txt.gz 2.txt 2.txt.xz 3.txt 3.txt.zip aaa aa.txt anaconda-ks.cfg a.txt x y z
[root@localhost ~]# ls {2,aa}.txt
2.txt aa.txt
>
: 将前面命令的输出重定向到后面的文件里(文件里的内容将会被覆盖)
[root@zyxlinux01 ~]# cat 1.txt
1111
[root@zyxlinux01 ~]# echo 2222 > 1.txt
[root@zyxlinux01 ~]# cat 1.txt
2222
>>
:将前面命令的输出追加输出重定向到后面的文件里(原内容会保留)
[root@zyxlinux01 ~]# echo 3333 >> 1.txt
[root@zyxlinux01 ~]# cat 1.txt
2222
3333
2>
: 将前面命令产生的错误信息输出重定向到后面的文件中
[root@zyxlinux01 ~]# xxx 1.txt 2> 2.txt
[root@zyxlinux01 ~]# cat 2.txt
-bash: xxx: 未找到命令
2>>
: 将前面命令产生的错误信息输出追加重定向到后面的文件中
[root@zyxlinux01 ~]# xxx 3.txt 2>> 2.txt
[root@zyxlinux01 ~]# cat 2.txt
-bash: xxx: 未找到命令
-bash: xxx: 未找到命令
> + 2> = &>
:将正确错误的信息输处重定向到同一个文件里
[root@zyxlinux01 ~]# ls
1.txt 1.txt.gz 2.txt 2.txt.xz 3.txt 3.txt.zip aa.txt anaconda-ks.cfg a.txt x y z
[root@zyxlinux01 ~]# ls [12].txt 4.txt &> 3.txt
[root@zyxlinux01 ~]# cat 3.txt
ls: 无法访问4.txt: 没有那个文件或目录
1.txt
2.txt
- 将正确错误的信息输输出重定向到两个不同文件里
[root@zyxlinux01 ~]# ls [12].txt 4.txt > 3.txt 2> a.txt
[root@zyxlinux01 ~]# cat 3.txt
1.txt
2.txt
[root@zyxlinux01 ~]# cat a.txt
ls: 无法访问4.txt: 没有那个文件或目录
<
: 将后面文件内容输入重定向到前面的命令中
[root@zyxlinux01 ~]# cat 1.txt
2222
3333
[root@zyxlinux01 ~]# wc -l < 1.txt
2
管道符 |
作用:将前面命令的结果交给后面的命令
- 比如:统计当前目录有多少个文件
[root@zyxlinux01 ~]# ls
1.txt 1.txt.gz 2.txt 2.txt.xz 3.txt 3.txt.zip aa.txt anaconda-ks.cfg a.txt x y z
[root@zyxlinux01 ~]# ls |wc -l
12
- 比如:列出文件中的关键词
[root@zyxlinux01 ~]# cat 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@zyxlinux01 ~]# cat 1.txt |grep 'sync'
sync:x:5:0:sync:/sbin:/bin/sync
作业控制
ctrl+z
: 暂停某个任务
例如:在编辑2.txt时,查看磁盘使用情况
[root@zyxlinux01 ~]# vim 2.txt
[1]+ 已停止 vim 2.txt
[root@zyxlinux01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 16G 1.3G 15G 8% /
devtmpfs 489M 0 489M 0% /dev
tmpfs 494M 0 494M 0% /dev/shm
tmpfs 494M 6.7M 487M 2% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/sda1 197M 75M 123M 38% /boot
jobs
: 查看当前所有的作业(只能查看当前终端的作业)
[root@zyxlinux01 ~]# jobs
[1]- 已停止 vim 2.txt
[2]+ 已停止 vim a.txt
fg
: 将在后台的作业调到前台bg
: 将作业放到后台并运行
[root@zyxlinux01 ~]# sleep 100
^Z
[1]+ 已停止 sleep 100
[root@zyxlinux01 ~]# bg 1
[1]+ sleep 100 &
[root@zyxlinux01 ~]# jobs
[1]+ 运行中 sleep 100 &
命令 &
: 直接将作业放到后台并运行
[root@zyxlinux01 ~]# sleep 200 &
[1] 2715
[root@zyxlinux01 ~]# jobs
[1]+ 运行中 sleep 200 &
shell变量
- env : 查看系统中的变量
[root@zyxlinux01 ~]# env
XDG_SESSION_ID=1
HOSTNAME=zyxlinux01
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.101.1 49807 22
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/0
USER=root
......
- set:同样可以查看系统变量,包括自定义的变量
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="45" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.45(1)-release'
......
- 自定义变量,例如:a=1(等号左边为变量名,右边为变量值)
[root@zyxlinux01 ~]# a=1
[root@zyxlinux01 ~]# echo $a
1
变量名规则:可以有字母、数字、下划线,首位不能是数字
- 当变量值中包括特殊字符时,值需要用单引号括起来
[root@zyxlinux01 ~]# b=xc$b
[root@zyxlinux01 ~]# echo $b
xc
[root@zyxlinux01 ~]# c='xc$b'
[root@zyxlinux01 ~]# echo $c
xc$b
- 变量的叠加,多个变量之间用双引号括起来
[root@zyxlinux01 ~]# echo $a $b
1 xc
[root@zyxlinux01 ~]# d=$a$b
[root@zyxlinux01 ~]# echo $d
1xc
[root@zyxlinux01 ~]# e=t$ag
[root@zyxlinux01 ~]# echo $e
t
[root@zyxlinux01 ~]# unset e
[root@zyxlinux01 ~]# e=t"$a"g
[root@zyxlinux01 ~]# echo $e
t1g
- 全局变量 export a=1 :在当前shell及其向下生效
[root@zyxlinux01 ~]# a=1
[root@zyxlinux01 ~]# echo $a
1
[root@zyxlinux01 ~]# bash
[root@zyxlinux01 ~]# echo $a
[root@zyxlinux01 ~]# exit
[root@zyxlinux01 ~]# export a=1
[root@zyxlinux01 ~]# bash
[root@zyxlinux01 ~]# echo $a
1
- pstree :以树状图显示进程之间的关系
[root@zyxlinux01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
├─auditd───{auditd}
├─avahi-daemon───avahi-daemon
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─iprdump
├─iprinit
├─iprupdate
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
- 取消变量
[root@zyxlinux01 ~]# echo $a
1
[root@zyxlinux01 ~]# unset a
[root@zyxlinux01 ~]# echo $a
[root@zyxlinux01 ~]#
环境变量配置文件
系统层面变量配置文件
/etc/profile :用户环境变量,登陆、交互时才执行
/etc/.bashrc :用户不需登陆,执行shell就生效
/etc/.bashrc 中 PS1 :定义命令左侧的字符串(用户名、主机名、目录)
用户层面变量配置文件
~/.bashrc
~/.bash_profile
~/.bash_logout : 定义用户退出时需要做的一些操作
shell特殊符号 上
*
: 代表任意0-n个字符?
: 代表任意一个字符#
: 注释字符,解释说明,无实际意义$
: 变量前缀\
: 脱义符,使后面的特符号失去其原来的意义!$
: 在正则中代表行尾;
: 用于分隔开在同一行的多条命令~
: 用户家目录,在正则表达式中表示匹配符&
: 放在命令后面,将命令放到后台||
: 用于两条命令之间,如果前面命令执行成功就不执行后面的命令;前面命令执行失败则执行后面的命令&&
: 用于两条命令之间,只有当前面的命令执行成功才执行后面的命令>
: 正确输出重定向>>
: 正确输出追加重定向2>
: 错误输出重定向2>>
: 错误输出追加重定向&>
: 正确错误输出重定向[]
: 表示指定字符中的任意一个字符,如:[0-9]表示任意数字;[a-zA-Z]表示任意字母;[a3m]表示a、3、m中任意一个
|
: 管道符,将前面命令的结果输出到后面的命令中,以下为一些与管道符有关的命令
cut 分隔
[root@zyxlinux01 ~]# head -n3 /etc/passwd |cut -d ":" -f 1,3
root:0
bin:1
daemon:2
[root@zyxlinux01 ~]# head -n3 /etc/passwd |cut -d ":" -f 1-4
root:x:0:0
bin:x:1:1
daemon:x:2:2
[root@zyxlinux01 ~]# head -n3 /etc/passwd |cut -c 2
o
i
a
[root@zyxlinux01 ~]# head -n3 /etc/passwd |cut -c 2-6
oot:x
in:x:
aemon
[root@zyxlinux01 ~]# head -n3 /etc/passwd |cut -c 2,4,5,7
ot::
i:x1
amo:
-d
: 指定分隔符号,符号需要加上双引号
-f
: 指定段号
-c
: 指定第几个字符
sort 排序
[root@zyxlinux01 x]# cat passwd |sort
111:11:a:aaa:rredsd:sd
11:111n:nmlkjfd:sds:nn
2:dfsd:ee:ssd:vdfD:2ee
67h:sdf:sdfef:sfc:eet:jkklll
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
sort默认以首字符按ASCII码排序
[root@zyxlinux01 x]# cat passwd |sort -n
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
-n
按数字大小排序,数字以外的字符被当做0看待
[root@zyxlinux01 x]# cat passwd |sort -nr
111:11:a:aaa:rredsd:sd
67h:sdf:sdfef:sfc:eet:jkklll
11:111n:nmlkjfd:sds:nn
2:dfsd:ee:ssd:vdfD:2ee
sync:x:5:0:sync:/sbin:/bin/sync
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
-r
反向排序
-t
分隔符,针对某一段或几段排序
wc 统计
[root@zyxlinux01 x]# cat passwd |wc -l
13
[root@zyxlinux01 x]# cat passwd |wc -w
17
[root@zyxlinux01 x]# cat passwd |wc -m
491
[root@zyxlinux01 x]# cat passwd |wc
13 17 491
-l
: 统计行数
-w
: 统计词数(词是以空格空白字符位分隔符)
-m
: 统计字符数
wc
: 不加参数,相当于wc -lwm
uniq 去重
通常和sort一起使用,先排序再去重
[root@zyxlinux01 x]# cat yx.txt
111
111
111
89765676
ddd
ddd
eocmlewlerjljd
osudfhosadf
xnhdhoseoh
28273672
[root@zyxlinux01 x]# cat yx.txt |uniq
111
89765676
ddd
eocmlewlerjljd
osudfhosadf
xnhdhoseoh
28273672
[root@zyxlinux01 x]# cat yx.txt |uniq -c
3 111
1 89765676
2 ddd
1 eocmlewlerjljd
1 osudfhosadf
1 xnhdhoseoh
1 28273672
uniq
去掉重复的行
uniq -c
去掉重复的行,并且统计重复的行数
tee
tee
类似于>
,重定向并将重定向的内容打印在屏幕上
[root@zyxlinux01 x]# sort yx.txt |uniq -c |tee yx01.txt
3 111
1 28273672
1 89765676
2 ddd
1 eocmlewlerjljd
1 osudfhosadf
1 xnhdhoseoh
[root@zyxlinux01 x]# cat yx01.txt
3 111
1 28273672
1 89765676
2 ddd
1 eocmlewlerjljd
1 osudfhosadf
1 xnhdhoseoh
tee -a
类似于>>
,追加重定向并将追加重定向的内容打印在屏幕上
[root@zyxlinux01 x]# head -n1 yx.txt |tee -a yx01.txt
111
[root@zyxlinux01 x]# cat yx01.txt
3 111
1 28273672
1 89765676
2 ddd
1 eocmlewlerjljd
1 osudfhosadf
1 xnhdhoseoh
111
tr 替换字符
语法:tr "a" "b"
;支持多个字符
[root@zyxlinux01 x]# echo "beniicong" |tr 'b' 'B'
Beniicong
[root@zyxlinux01 x]# echo "beniicong" |tr '[bic]' '[BIC]'
BenIICong
[root@zyxlinux01 x]# echo "beniicong" |tr '[a-z]' '[A-Z]'
BENIICONG
[root@zyxlinux01 x]# echo "beniicong" |tr '[a-z]' '1'
111111111
split 切割
将一个文件切割为多个文件
[root@zyxlinux01 x]# du -sh yx02.txt ; wc -l yx02.txt
128K yx02.txt
3567 yx02.txt
[root@zyxlinux01 x]# split -b 50k yx02.txt nb
[root@zyxlinux01 x]# ls
nbaa nbab nbac yx02.txt
[root@zyxlinux01 x]# rm -f nb*
[root@zyxlinux01 x]# split -l 1000 yx02.txt nb
[root@zyxlinux01 x]# ls
nbaa nbab nbac nbad yx02.txt
-b
: 按指定大小切割,默认单位是 KB
-l
: 按指定行数切割
可以自定义切割后的文件名开头的字符,默认以
x
开头