Shell基础知识

Shell介绍

Shell是一个命令解释器,提供用户和机器之间的交互,支持特定语法,比如逻辑判断、循环。每个用户都可以有自己特定的Shell。CentOS 7默认shell为bash(Bourne Again Shell,/bin/bash),它是sh(Bourne Shell)的增强版本,此外还有zsh、ksh等。

[root@centos-01 ~]# yum list |grep zsh
autojump-zsh.noarch                       22.3.0-3.el7                 epel     
zsh.x86_64                                5.0.2-28.el7                 base     
zsh-html.x86_64                           5.0.2-28.el7                 base     
zsh-lovers.noarch                         0.9.0-1.el7                  epel     
[root@centos-01 ~]# yum list |grep ksh
ksh.x86_64                                20120801-35.el7_4            updates  
mksh.x86_64                               46-5.el7                     base     
python-XStatic-Rickshaw.noarch            1.5.0.0-4.el7                epel     
python-moksha-common.noarch               1.2.3-2.el7                  epel     
python-moksha-wsgi.noarch                 1.2.2-2.el7                  epel     
python2-moksha-hub.noarch                 1.5.5-2.el7                  epel     

历史命令

https://blog.csdn.net/noob_f/article/details/80214204

命令补全和别名

https://blog.csdn.net/Noob_f/article/details/80384966

通配符

  • *匹配零个或多个字符
[root@centos-01 ~]# ls
anaconda-ks.cfg
[root@centos-01 ~]# cd /tmp/
[root@centos-01 tmp]# ls
1.txt
2.txt.gz
3.txt.bz2
4.txt.xz
mytest
systemd-private-59dabe73e76f4c919c49080f12695d35-chronyd.service-ocfQwu
systemd-private-59dabe73e76f4c919c49080f12695d35-vgauthd.service-NyBxv0
systemd-private-59dabe73e76f4c919c49080f12695d35-vmtoolsd.service-pdvc96
vim-enhanced-7.4.160-2.el7.x86_64.rpm
yum_save_tx.2018-05-05.19-44.not9LG.yumtx
yum_save_tx.2018-05-05.21-20.Iue83X.yumtx
[root@centos-01 tmp]# ls *.txt
1.txt
[root@centos-01 tmp]# ls *txt*
1.txt  2.txt.gz  3.txt.bz2  4.txt.xz
[root@centos-01 tmp]# ls 1*
1.txt
  • ?匹配一个字符
[root@centos-01 tmp]# ls ?.txt
1.txt
[root@centos-01 tmp]# touch abc.txt
[root@centos-01 tmp]# ls
1.txt      systemd-private-59dabe73e76f4c919c49080f12695d35-chronyd.service-ocfQwu
2.txt.gz   systemd-private-59dabe73e76f4c919c49080f12695d35-vgauthd.service-NyBxv0
3.txt.bz2  systemd-private-59dabe73e76f4c919c49080f12695d35-vmtoolsd.service-pdvc96
4.txt.xz   vim-enhanced-7.4.160-2.el7.x86_64.rpm
abc.txt    yum_save_tx.2018-05-05.19-44.not9LG.yumtx
mytest     yum_save_tx.2018-05-05.21-20.Iue83X.yumtx
[root@centos-01 tmp]# ls ?.txt
1.txt
[root@centos-01 tmp]# cd mytest/
[root@centos-01 mytest]# ls
1.txt  2.txt.zip  4.txt  yourtest      yourtest.tar.bz2  yourtest.tar.xz
2.txt  3.txt      test   yourtest.tar  yourtest.tar.gz   yourtest.zip
[root@centos-01 mytest]# ls [0-3].txt
1.txt  2.txt  3.txt
[root@centos-01 mytest]# ls [2-3].txt
2.txt  3.txt
[root@centos-01 mytest]# ls [13].txt
1.txt  3.txt
[root@centos-01 mytest]# touch abc.txt
[root@centos-01 mytest]# ls [0-9a-zA-Z].txt
1.txt  2.txt  3.txt  4.txt
[root@centos-01 mytest]# touch d.txt
[root@centos-01 mytest]# ls [0-9a-zA-Z].txt
1.txt  2.txt  3.txt  4.txt  d.txt
[root@centos-01 mytest]# ls {1,2}.txt
1.txt  2.txt

输入输出重定向

覆盖输出>、追加输出>>、错误重定向2>、错误追加重定向2>>

[root@centos-01 ~]# cd /tmp/mytest/
[root@centos-01 mytest]# ls
1.txt  2.txt.zip  4.txt    d.txt  yourtest      yourtest.tar.bz2  yourtest.tar.xz
2.txt  3.txt      abc.txt  test   yourtest.tar  yourtest.tar.gz   yourtest.zip
[root@centos-01 mytest]# ls abc.txt 
abc.txt
[root@centos-01 mytest]# ls abc.txt > a.txt
[root@centos-01 mytest]# cat a.txt 
abc.txt
[root@centos-01 mytest]# lss abc.txt 2> a.txt
[root@centos-01 mytest]# cat a.txt 
-bash: lss: 未找到命令

>+2>==&>

[root@centos-01 mytest]# ls [12].txt ab.txt
ls: 无法访问ab.txt: 没有那个文件或目录
1.txt  2.txt
[root@centos-01 mytest]# ls [12].txt ab.txt > a.txt 
ls: 无法访问ab.txt: 没有那个文件或目录
[root@centos-01 mytest]# cat a.txt 
1.txt
2.txt
[root@centos-01 mytest]# ls [12].txt ab.txt 2> a.txt 
1.txt  2.txt
[root@centos-01 mytest]# cat a.txt 
ls: 无法访问ab.txt: 没有那个文件或目录
[root@centos-01 mytest]# ls [12].txt ab.txt > 1.txt 2>a.txt
[root@centos-01 mytest]# cat 1.txt 
1.txt
2.txt
[root@centos-01 mytest]# cat a.txt 
ls: 无法访问ab.txt: 没有那个文件或目录
[root@centos-01 mytest]# ls [12].txt ab.txt &> a.txt 
[root@centos-01 mytest]# cat a.txt 
ls: 无法访问ab.txt: 没有那个文件或目录
1.txt
2.txt

<输入重定向

[root@centos-01 mytest]# wc -l < 1.txt 
2

管道符

管道符|,把前面的命令运行结果丢给后面的命令

[root@centos-01 ~]# cat /tmp/mytest/1.txt | wc -l
2
[root@centos-01 ~]# ls /tmp/mytest/
1.txt  2.txt.zip  4.txt    a.txt  test      yourtest.tar      yourtest.tar.gz  yourtest.zip
2.txt  3.txt      abc.txt  d.txt  yourtest  yourtest.tar.bz2  yourtest.tar.xz
[root@centos-01 ~]# ls /tmp/mytest/ |wc -l
15

作业控制

当运行一个进程时,可以使它暂停(按Ctrl+z),然后使用fg命令恢复它,利用bg命令使它到后台运行,也可以使它终止(按Ctrl+c)。jobs命令可以看到在被暂停或者在后台运行的任务。

bg [id]把任务调到后台,fg [id]把任务调到前台,命令后面加&直接丢到后台。

[root@centos-01 ~]# vim /tmp/mytest/1.txt 

[1]+  已停止               vim /tmp/mytest/1.txt
[root@centos-01 ~]# fg
vim /tmp/mytest/1.txt

[1]+  已停止               vim /tmp/mytest/1.txt
[root@centos-01 ~]# vim /tmp/mytest/2.txt

[2]+  已停止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# jobs
[1]-  已停止               vim /tmp/mytest/1.txt
[2]+  已停止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# fg 2
vim /tmp/mytest/2.txt

[2]+  已停止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# bg 2
[2]+ vim /tmp/mytest/2.txt &
[root@centos-01 ~]# jobs
[1]-  已停止               vim /tmp/mytest/1.txt
[2]+  已停止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 726156   2076 144068    0    0    79     5   81  111  0  1 98  2  0
 0  0      0 726156   2076 144100    0    0     0     0   47   43  0  1 99  0  0
^Z
[3]+  已停止               vmstat 1
[root@centos-01 ~]# bg 3
[3]+ vmstat 1 &
[root@centos-01 ~]#  2  0      0 726156   2076 144100    0    0     0     1  274  260  0  0 100  0  0
 0  0      0 726156   2076 144100    0    0     0     0   44   44  0  1 99  0  0
 0  0      0 726156   2076 144100    0    0     0     0   41   35  0  0 100  0  0
 0  0      0 726156   2076 144100    0    0     0     0   40   35  0  0 100  0  0
fg 0  0      0 726032   2076 144100    0    0     0     0   52   46  0  0 100  0  0
 3
vmstat 1
 0  0      0 726032   2076 144100    0    0     0     0   60   60  0  0 100  0  0
 0  0      0 726032   2076 144100    0    0     0     0   34   32  0  0 100  0  0
^Z
[3]+  已停止               vmstat 1
[root@centos-01 ~]# jobs
[1]   已停止               vim /tmp/mytest/1.txt
[2]-  已停止               vim /tmp/mytest/2.txt
[3]+  已停止               vmstat 1

打开新的终端,使用jobs命令并不会显示在后台运行或者被暂停的任务,要想停掉它的话,则需要先知道其pid,然后使用kill命令杀死那个进程。kill命令语法很简单,直接在后面加pid即可。如果遇到杀不死的进程时,可以在kill后面加一个选项:kill -9 [pid]

[root@centos-01 ~]# ps aux |grep vim
root       1084  0.0  0.5 151548  5080 pts/0    T    513   0:00 vim /tmp/mytest/1.txt
root       1093  0.0  0.6 153236  6792 pts/0    T    513   0:00 vim /tmp/mytest/2.txt
root       1119  0.0  0.0 112676   980 pts/0    R+   00:19   0:00 grep --color=auto vim
[root@centos-01 ~]# kill 1084
[root@centos-01 ~]# kill 1093
[root@centos-01 ~]# jobs
[1]   已停止               vim /tmp/mytest/1.txt
[2]-  已停止               vim /tmp/mytest/2.txt
[3]+  已停止               vmstat 1
[root@centos-01 ~]# kill -9 1093
[root@centos-01 ~]# jobs
[1]   已停止               vim /tmp/mytest/1.txt
[2]-  已杀死               vim /tmp/mytest/2.txt
[3]+  已停止               vmstat 1
[root@centos-01 ~]# kill -9 1084
[root@centos-01 ~]# jobs
[1]-  已杀死               vim /tmp/mytest/1.txt
[3]+  已停止               vmstat 1
[root@centos-01 ~]# ps aux |grep vmstat
root       1095  0.0  0.1 148316  1352 pts/0    T    513   0:00 vmstat 1
root       1123  0.0  0.0 112676   980 pts/0    R+   00:21   0:00 grep --color=auto vmstat
[root@centos-01 ~]# kill 1095
[root@centos-01 ~]# jobs
[3]+  已停止               vmstat 1
[root@centos-01 ~]# kill -9 1095
[root@centos-01 ~]# jobs
[3]+  已杀死               vmstat 1
[root@centos-01 ~]# jobs
[root@centos-01 ~]#

Shell变量

https://blog.csdn.net/Noob_f/article/details/80385002

环境变量配置文件

  • /etc/profile:这个文件预设了几个重要的变量,例如PATH、USER、LOGNAME、MAIL、INPUTRC、HOSTNAME、HISTSIZE等。
  • /etc/bashrc:这个文件主要预设umask以及PS1。这个PS1就是在敲命令时,前面那串字符了,例如我的Linux系统PS1就是‘[root@centos-01 ~]#’。
[root@centos-01 ~]# echo $PS1
[\u@\h \W]\$
[root@centos-01 ~]#

‘\u’是用户,‘\h’是主机名,‘\W’是当前目录(改为‘w’则是绝对路径);‘$’就是‘#’ ,如果是普通用户则显示为 ‘$’。

除了两个系统级别的配置文件外,每个用户的家目录下还有几个这样的隐藏文件:

  • .bash_profile:定义了用户的个人化路径与环境变量的文件名称。每个用户都可使用该文件输入专用于自己使用的shell信息。当用户登录时,该文件仅仅执行一次。
  • .bashrc:该文件包含专用于当前用户shell的bash信息。当登录时以及每次打开新的shell时,该该文件被读取。例如可以将用户自定义的alias或者自定义变量写到这个文件中。
  • .bash_history:记录命令历史。
  • .bash_logout:当退出shell时,会执行该文件。可以把一些清理的工作放到这个文件中。

Shell中的特殊符号

  • *:任意个任意字符
  • ?:任意一个字符
  • #:注释字符
  • \:脱义字符
[root@centos-01 ~]# a=1
[root@centos-01 ~]# b=2
[root@centos-01 ~]# c=$a$b
[root@centos-01 ~]# echo $c
12
[root@centos-01 ~]# c='$a$b'
[root@centos-01 ~]# echo $c
$a$b
[root@centos-01 ~]# c=\$a\$b
[root@centos-01 ~]# echo $c
$a$b
  • |:管道符

和管道符有关的命令

https://blog.csdn.net/Noob_f/article/details/80385303


  • $:变量前缀。除了用于变量前面的标识符外,还可以和‘!’结合起来使用。‘!$’表示上条命中中最后一个变量。假如上条命令是ls test.txt,最后是test.txt,那么在当前命令下输入!$则代表test.txt。
  • ;:在一行中运行两个或两个以上的命令,需要在命令之间加一个”;”。
  • ~:用户的家目录,如果是root则是/root,普通用户则是/home/username。正则表达式中表示匹配符。
  • &:把一条命令放到后台执行。
  • >:覆盖重定向
  • >>:追加重定向
  • 2>:错误覆盖重定向
  • 2>>:错误追加重定向
  • &>:正确信息和错误信息都输出
  • []:中括号,中间为字符组合,代表中间字符中的任意一个。

&&||

  1. command1 ; command2
  2. command1 && command2
  3. command1 || command2

使用”;”时,不管command1是否执行成功都会执行command2;

使用“&&”时,只有command1执行成功后,command2才会执行,否则command2不执行;

使用“||”时,command1执行成功后command2不执行,否则去执行command2,总之command1和command2总有一条命令会执行。

转载于:https://www.cnblogs.com/Genesis2018/p/9079764.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值