Linux学习-管理文件(4)

使用命令行扩展

Bash shell有多种扩展命令行的方式,包括模式匹配、主目录扩展、字符串扩展和变量替换。

元字符和匹配项表

 

模式 匹配项
* 由零个或更多字符组成的任意字符串
任何一个字符
[abc...] 括起的类(位于两个方括号之间)中的任何一个字符
[!abc...] 不在括起的类中的任何一个字符
[^abc...] 不在括起的类中的任何一个字符
[[:alpha:]] 任何字母字符
[[:lower:]] 任何小写字符
[[:upper:]] 任何大写字符
[[:alnum:]] 任何字母字符或数字

 

[[:punct:]] 除空格和字母数字以外的任何可打印字符
[[:digit:]] 从0到9的任何单个数字
[[:space:]] 任何一个空白字符。

示例:

简单模式匹配:

通过星号和问号字符以及一类字符来匹配其中一些文件名

[jumpserver@192 bin]$ ls a*
abrt-action-analyze-backtrace             abrt-dump-xorg                   ar
abrt-action-analyze-c                     abrt-handle-upload               arch
abrt-action-analyze-ccpp-local            abrt-merge-pstoreoops            arecord
[jumpserver@192 bin]$ ls *a*
abrt-action-analyze-backtrace             nl-cls-add
abrt-action-analyze-c                     nl-link-enslave
abrt-action-analyze-ccpp-local            nl-link-ifindex2name
abrt-action-analyze-core                  nl-link-name2ifindex
jumpserver@192 bin]$ ls [ac]*
abrt-action-analyze-backtrace             analog             catman              clear
abrt-action-analyze-c                     aplay              cc                  clevis
abrt-action-analyze-ccpp-local            aplaymidi          cd                  clevis-decrypt
abrt-action-analyze-core                  appstream-compose  cd-convert          clevis-decrypt-sss
[jumpserver@192 bin]$ ls ???
atq  c99  col  dig  env  ftp  gjs  jjs  lua  mdu  ptx  red  scp  sum  tee  top  who  zip
[jumpserver@192 bin]$ ls ?????
alias  chacl  crash  fgrep  guild  lscpu  mmcli  nproc  pchrt  pslog  sg_dd  tload  vlock  xmlwf
amidi  chage  dconf  flock  guile  lsiio  mmove  nroff  pgrep  quota  shred  totem  watch  xzcat

波形符扩展:

波形符(~)可匹配当前用户的主目录

[jumpserver@192 /]$ ls ~jumpserver
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

大括号扩展:

1)大括号扩展用于生成任意字符串

2)大括号包含字符串的逗号分隔列表或顺序表达式

3)双句点语法(..)可扩展成一个序列,使得{m..p}扩展成m n o p

[jumpserver@192 test]$ echo {Sunday,Monday,Tuesday,Wednesday}.log
Sunday.log Monday.log Tuesday.log Wednesday.log
[jumpserver@192 test]$ echo file{1..3}.txt
file1.txt file2.txt file3.txt
[jumpserver@192 test]$ echo file{a..e}.txt
filea.txt fileb.txt filec.txt filed.txt filee.txt
[jumpserver@192 test]$ echo file{a,b}{1,2}.txt
filea1.txt filea2.txt fileb1.txt fileb2.txt
[jumpserver@192 test]$ echo file{a{1,2},b,c}.txt
filea1.txt filea2.txt fileb.txt filec.txt
[jumpserver@192 test]$ mkdir rhel{6,7,8}
[jumpserver@192 test]$ ls R*
ls: cannot access 'R*': No such file or directory
[jumpserver@192 test]$ ls r*
rhel6:
rhel7:
rhel8:

变量扩展:

变量的作用类似于可以在内存中存储值的命名容器

通过变量,可以从命令行或在shell脚本内轻松访问和修改存储的数据

[jumpserver@192 test]$ USERNAME=operator
[jumpserver@192 test]$ echo $USERNAME
operator
为了避免因其他shell扩展而引起的错误,可将变量的名称放在大括号中
[jumpserver@192 test]$ echo ${USERNAME}
operator

命令替换:

1)命令替换允许命令的输出替换命令行上的命令本身

2)当命令括在括号中并且前面有美元符号($)时,会发生命令替换

[jumpserver@192 test]$ echo Today is $(date)
Today is Sun Nov 15 23:24:08 PST 2020

防止参数被扩展:

反斜杠(\)是Bash shell中的转义字符。它可以防止紧随其后的字符被扩展

[jumpserver@192 /]$ echo the value is $HOME is your home directory
the value is /home/jumpserver is your home directory
[jumpserver@192 /]$ echo the value is \$HOME is your home directory
the value is $HOME is your home directory

如果需要保护较长的字符串,则使用单引号('')或双引号("")来括起字符串

单引号:阻止通配和shell扩展

双引号:阻止大部分shell扩展

使用双引号可以阻止通配和shell扩展,但依然允许命令和变量替换
[jumpserver@192 /]$ 
echo "---------hostname is ${myhost}------------"
---------hostname is 192------------
[jumpserver@192 /]$ echo "will varialbe $myhost evaluate to $(hostname -s)"
will varialbe 192 evaluate to 192
[jumpserver@192 /]$ echo 'will varialbe $myhost evaluate to $(hostname -s)'
will varialbe $myhost evaluate to $(hostname -s)
posted @ 2020-11-16 15:54  雾雨之森  阅读(130)  评论(0)    收藏  举报