【9.17】shell 基础(下)

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=/root
LANG=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 查看所有变量(不仅查看系统内置的环境变量,也会有用户自定义的变量)

  • 8.7.1、自定义变量

[root@arslinux-01 ~]# a=111
[root@arslinux-01 ~]# echo $a
111
[root@arslinux-01 ~]# set |grep 111
_=111
a=111
  • 8.7.2、变量名规则

字母、数字下划线,首位不能为数字
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
  • 8.8.1、本地变量

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

[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
  • 这种仅仅在当前终端下生效的变量叫本地变量

  • 8.8.2、全局变量

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 环境变量配置文件

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

  • 8.9.1、系统层次

  • /etc/profile 用户环境变量,交互,登录才执行

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

  • profilebashrc 的区别是,profile是用户登录时加载执行的,而 bashrc 是执行shell脚本时生效的
    (以上两个目录请勿乱动,平时编辑用户家目录下的即可,系统层次的可以全局生效,而用户层次只针对该用户生效)

配置文件被修改后,可以用source 文件或者 . 文件来重新加载
例如:source /etc/profile 或 . /etc/profile

  • 8.9.2、用户层次

  • ~/.bashrc 用户命令别名等个性化

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

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

  • ~/.bash_logout 用来定义用户退出时需要做的一些操作
    如果想让用户每次退出时都删除命令历史,那么可以用把删除命令历史的命令放到 .bash_logout 中去

  • 变量 PS1 定义提示符,在 /etc/bashrc 下修改
    在这里插入图片描述

[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

8.10 shell 特殊符号 cut 命令

  • 特殊符号
符号含义
*任意个任意字符 ?.txt
?任意一个字符
#注释字符 不生效,说明文字
**脱义字符 用在符号前
|管道符
  • cut 截取字符串
常用选项含义
-d指定分隔符
-f指定选取第几段
-c指定选取第几个字符(-c 和 -d 是两种类型,不能共用)
[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -d : -f 1
root
bin
[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -d : -f 1,2
root:x
bin:x
[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -d : -f 1-3
root:x:0
bin:x:1
[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -c 4
t
:

8.11 sort_wc_uniq 命令

  • 8.11.1、sort

  • sort 文件名 排序(默认按照ACSII排序)
常用选项含义
-n依照数值的大小排序
-r以相反的顺序来排序
-t指定排序时所用的栏位分隔字符
-k是指定针对第几段排序 -nk 1,-nk 1,2
  • sort 默认用法
[root@arslinux-01 ~]# sort 1.txt
<
>
]
{
1.txt
222222aaaaaaaaa
22333333
22aaa
231312131
2.txt
4888888888888888888adslkfj;a
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
*slkdf
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
  • sort -n 以数字顺序排序,特殊符号和字母被视为0
    默认排序是按照 空字符 - 特殊符号 - 数字 - 字母
[root@arslinux-01 ~]# sort -n 1.txt
<
>
]
{
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
*slkdf
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
1.txt
2.txt
22aaa
222222aaaaaaaaa
22333333
231312131
4888888888888888888adslkfj;a
  • sort -r 反向排序(和 sort 相反)
[root@arslinux-01 ~]# sort -r 1.txt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
*slkdf
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
4888888888888888888adslkfj;a
2.txt
231312131
22aaa
22333333
222222aaaaaaaaa
1.txt
{
]
>
<
  • sort -t -k数字 指定分隔符,按第几段来排序
[root@arslinux-01 ~]# sort -t : -k2 1.txt
<
>
]
{
1.txt
222222aaaaaaaaa
22333333
22aaa
231312131
2.txt
4888888888888888888adslkfj;a
*slkdf
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
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
halt:x:7:0:halt:/sbin:/sbin/halt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
  • 8.11.2、wc

  • wc 统计
常用选项含义
-l统计行数
-m统计字符数(包括隐藏的换行符,cat -A 可查看全部)
-w统计词
[root@arslinux-01 ~]# wc -l 1.txt
20 1.txt
[root@arslinux-01 ~]# wc -m 1.txt
425 1.txt
[root@arslinux-01 ~]# wc -w 1.txt
21 1.txt
  • 8.11.3、uniq

  • uniq 去重(一般先排序在去重)
常用选项含义
-c统计重复的次数(在每列旁边显示该行重复出现的次数)
[root@arslinux-01 ~]# sort -n 2.txt 
abc
abc 1111,2222
1
1
2
123
123
[root@arslinux-01 ~]# sort -n 2.txt |uniq
abc
abc 1111,2222
1
2
123
[root@arslinux-01 ~]# sort -n 2.txt |uniq -c
      1 abc
      1 abc 1111,2222
      2 1
      1 2
      2 123

资料:http://man.linuxde.net/sort

8.12 tee_tr_split 命令

  • 8.12.1、tee

  • tee 和 > 类似,重定向的同时还在屏幕显示,而 > 不显示过程
[root@arslinux-01 ~]# sort 2.txt | uniq -c > a.txt
[root@arslinux-01 ~]# cat !$
cat a.txt
      2 1
      2 123
      1 2
      1 abc
      1 abc 1111,2222
[root@arslinux-01 ~]# sort 2.txt | uniq -c |tee b.txt
      2 1
      2 123
      1 2
      1 abc
      1 abc 1111,2222

>文件名 清空文件内容

[root@arslinux-01 ~]# >a.txt
[root@arslinux-01 ~]# cat a.txt
[root@arslinux-01 ~]# 
  • tee -a 和 >> 类似,追加的同时还在屏幕显示,而 >>不显示过程
[root@arslinux-01 ~]# sort 2.txt | uniq -c |tee -a a.txt 
      2 1
      2 123
      1 2
      1 abc
      1 abc 1111,2222
[root@arslinux-01 ~]# sort 2.txt | uniq -c |tee -a a.txt 
      2 1
      2 123
      1 2
      1 abc
      1 abc 1111,2222
[root@arslinux-01 ~]# cat a.txt
      2 1
      2 123
      1 2
      1 abc
      1 abc 1111,2222
      2 1
      2 123
      1 2
      1 abc
      1 abc 1111,2222
  • 8.12.2、tr

  • tr 替换字符:tr ‘a’ ‘b’ (tr=translate)
[root@arslinux-01 ~]# echo arslinux | tr '[al]' '[AL]'
ArsLinux
[root@arslinux-01 ~]# echo arslinux | tr 'al' 'AL'
ArsLinux
[root@arslinux-01 ~]# echo arslinux | tr '[a-z]' '[A-Z]'
ARSLINUX
[root@arslinux-01 ~]# echo arslinux | tr '[a-z]' '1'
11111111
[root@arslinux-01 ~]# echo arslinux | tr '[a-z]' '[1]'
1]]]]]]]

单个数字或字母不要用[ ]括起来,会出错

  • 8.12.3、split

  • split 切割 (文件过大,切割方便操作)
常用选项含义
-b按大小切割(默认单位字节,如果不写单位,就会按100B切割)
-l按行数切割
  • split -b 大小 文件名
[root@arslinux-01 ~]# find /etc/ -type f -name "*.conf" -exec cat {} >> a.txt \;
[root@arslinux-01 ~]# du -sh a.txt 
448K	a.txt
[root@arslinux-01 ~]# mkdir test/
[root@arslinux-01 ~]# mv a.txt test/
[root@arslinux-01 ~]# ll test/
总用量 448
-rw-r--r--. 1 root root 217745 4月   3 22:08 a.txt
[root@arslinux-01 ~]# cd test/
[root@arslinux-01 test]# split -b 1000 a.txt 
[root@arslinux-01 test]# ls
a.txt  xaj  xat  xbd  xbn  xbx  xch  xcr  xdb  xdl  xdv  xef  xep  xez  xfj  xft  xgd  xgn  xgx  xhh  xhr  xib
xaa    xak  xau  xbe  xbo  xby  xci  xcs  xdc  xdm  xdw  xeg  xeq  xfa  xfk  xfu  xge  xgo  xgy  xhi  xhs  xic
xab    xal  xav  xbf  xbp  xbz  xcj  xct  xdd  xdn  xdx  xeh  xer  xfb  xfl  xfv  xgf  xgp  xgz  xhj  xht  xid
xac    xam  xaw  xbg  xbq  xca  xck  xcu  xde  xdo  xdy  xei  xes  xfc  xfm  xfw  xgg  xgq  xha  xhk  xhu  xie
xad    xan  xax  xbh  xbr  xcb  xcl  xcv  xdf  xdp  xdz  xej  xet  xfd  xfn  xfx  xgh  xgr  xhb  xhl  xhv  xif
xae    xao  xay  xbi  xbs  xcc  xcm  xcw  xdg  xdq  xea  xek  xeu  xfe  xfo  xfy  xgi  xgs  xhc  xhm  xhw  xig
xaf    xap  xaz  xbj  xbt  xcd  xcn  xcx  xdh  xdr  xeb  xel  xev  xff  xfp  xfz  xgj  xgt  xhd  xhn  xhx  xih
xag    xaq  xba  xbk  xbu  xce  xco  xcy  xdi  xds  xec  xem  xew  xfg  xfq  xga  xgk  xgu  xhe  xho  xhy  xii
xah    xar  xbb  xbl  xbv  xcf  xcp  xcz  xdj  xdt  xed  xen  xex  xfh  xfr  xgb  xgl  xgv  xhf  xhp  xhz  xij
xai    xas  xbc  xbm  xbw  xcg  xcq  xda  xdk  xdu  xee  xeo  xey  xfi  xfs  xgc  xgm  xgw  xhg  xhq  xia	.
[root@arslinux-01 test]# du -sh
1.3M	.
[root@arslinux-01 test]# du -sb *
217745	a.txt
1000	xaa
1000	xab
1000	xac
1000	xad
1000	xae
1000	xaf
1000	xag
1000	xah
1000	xai
1000	xaj
1000	xak
1000	xal
1000	xam
1000	xan
1000	xao
1000	xap
1000	xaq
1000	xar
1000	xas
1000	xat
1000	xau
1000	xav
1000	xaw
1000	xax
1000	xay
1000	xaz
1000	xba
1000	xbb
1000	xbc
1000	xbd
1000	xbe
1000	xbf
1000	xbg
1000	xbh
1000	xbi
1000	xbj
1000	xbk
1000	xbl
1000	xbm
1000	xbn
1000	xbo
1000	xbp
1000	xbq
1000	xbr
1000	xbs
1000	xbt
1000	xbu
1000	xbv
1000	xbw
1000	xbx
1000	xby
1000	xbz
1000	xca
1000	xcb
1000	xcc
1000	xcd
1000	xce
1000	xcf
1000	xcg
1000	xch
1000	xci
1000	xcj
1000	xck
1000	xcl
1000	xcm
1000	xcn
1000	xco
1000	xcp
1000	xcq
1000	xcr
1000	xcs
1000	xct
1000	xcu
1000	xcv
1000	xcw
1000	xcx
1000	xcy
1000	xcz
1000	xda
1000	xdb
1000	xdc
1000	xdd
1000	xde
1000	xdf
1000	xdg
1000	xdh
1000	xdi
1000	xdj
1000	xdk
1000	xdl
1000	xdm
1000	xdn
1000	xdo
1000	xdp
1000	xdq
1000	xdr
1000	xds
1000	xdt
1000	xdu
1000	xdv
1000	xdw
1000	xdx
1000	xdy
1000	xdz
1000	xea
1000	xeb
1000	xec
1000	xed
1000	xee
1000	xef
1000	xeg
1000	xeh
1000	xei
1000	xej
1000	xek
1000	xel
1000	xem
1000	xen
1000	xeo
1000	xep
1000	xeq
1000	xer
1000	xes
1000	xet
1000	xeu
1000	xev
1000	xew
1000	xex
1000	xey
1000	xez
1000	xfa
1000	xfb
1000	xfc
1000	xfd
1000	xfe
1000	xff
1000	xfg
1000	xfh
1000	xfi
1000	xfj
1000	xfk
1000	xfl
1000	xfm
1000	xfn
1000	xfo
1000	xfp
1000	xfq
1000	xfr
1000	xfs
1000	xft
1000	xfu
1000	xfv
1000	xfw
1000	xfx
1000	xfy
1000	xfz
1000	xga
1000	xgb
1000	xgc
1000	xgd
1000	xge
1000	xgf
1000	xgg
1000	xgh
1000	xgi
1000	xgj
1000	xgk
1000	xgl
1000	xgm
1000	xgn
1000	xgo
1000	xgp
1000	xgq
1000	xgr
1000	xgs
1000	xgt
1000	xgu
1000	xgv
1000	xgw
1000	xgx
1000	xgy
1000	xgz
1000	xha
1000	xhb
1000	xhc
1000	xhd
1000	xhe
1000	xhf
1000	xhg
1000	xhh
1000	xhi
1000	xhj
1000	xhk
1000	xhl
1000	xhm
1000	xhn
1000	xho
1000	xhp
1000	xhq
1000	xhr
1000	xhs
1000	xht
1000	xhu
1000	xhv
1000	xhw
1000	xhx
1000	xhy
1000	xhz
1000	xia
1000	xib
1000	xic
1000	xid
1000	xie
1000	xif
1000	xig
1000	xih
1000	xii
745	xij
[root@arslinux-01 test]# rm -f x*
[root@arslinux-01 test]# ls
a.txt
  • 指定切割出文件的前缀 (切割后前缀为 abc 后面根据aa,ab…ba,bb,…排列)
[root@arslinux-01 test]# split -b 100k a.txt abc
[root@arslinux-01 test]# ls
abcaa  abcab  abcac  a.txt
[root@arslinux-01 test]# split -b 100k a.txt abc.
[root@arslinux-01 test]# ls
abcaa  abc.aa  abcab  abc.ab  abcac  abc.ac  a.txt
[root@arslinux-01 test]# rm -f abc*

切割命令后跟需要制定的前缀名

  • split -l 行数 文件名 按行来切割
[root@arslinux-01 test]# split -l 1000 a.txt
[root@arslinux-01 test]# ll
总用量 440
-rw-r--r--. 1 root root 217745 4月   3 22:08 a.txt
-rw-r--r--. 1 root root  33572 4月   3 22:19 xaa
-rw-r--r--. 1 root root  46174 4月   3 22:19 xab
-rw-r--r--. 1 root root  44813 4月   3 22:19 xac
-rw-r--r--. 1 root root  44010 4月   3 22:19 xad
-rw-r--r--. 1 root root  33681 4月   3 22:19 xae
-rw-r--r--. 1 root root  15495 4月   3 22:19 xaf
[root@arslinux-01 test]# wc -l *
  5491 a.txt
  1000 xaa
  1000 xab
  1000 xac
  1000 xad
  1000 xae
   491 xaf
 10982 总用量

8.13 shell 特殊符号

特殊符号含义
$变量前缀,!$ 组合,正则里面表示行尾
;多条命令写到一行,用分号分割
[root@arslinux-01 test]# for i in `seq 1 10`
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10
[root@arslinux-01 test]# for i in `seq 1 10`; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[root@arslinux-01 test]# ls a.txt;wc -l a.txt
a.txt
5491 a.txt
  • 特殊符号
特殊符号具体含义
~用户家目录,后面正则表达式表示匹配符
&放到命令后面,会把命令丢到后台
>正确重定向,覆盖源文件
>>追加正确重定向
2>错误重定向
2>>追加错误重定向
&>正确错误全部重定向到一个文件
[ ]指定字符中的一个,[0-9],[a-zA-Z],[abc]
|| 和 &&用于命令之间(或与和)判断

|| 第一条执行不成功就会执行第二条,第一条执行成功就不会执行第二条命令
&& 第一条执行成功才会执行第二条,第一条执行不成功就不会执行第二条

[root@arslinux-01 test]# ls la.txt || wc -l a.txt
ls: 无法访问la.txt: 没有那个文件或目录
5491 a.txt
[root@arslinux-01 test]# ls a.txt || wc -l a.txt
a.txt
[root@arslinux-01 test]# ls la.txt && wc -l a.txt
ls: 无法访问la.txt: 没有那个文件或目录
[root@arslinux-01 test]# ls a.txt && wc -l a.txt
a.txt
5491 a.txt
[root@arslinux-01 ~]# [ -d arslinux ] || mkdir arslinux
[root@arslinux-01 ~]# [ -d arslinux ] && mkdir arslinux 
mkdir: 无法创建目录"arslinux": 文件已存在

|| 存在则不执行,不存在则执行
&& 存在则执行,不存在则不执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值