zoukankan      html  css  js  c++  java
  • 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总有一条命令会执行。

  • 相关阅读:
    分层图最短路(DP思想) BZOJ2662 [BeiJing wc2012]冻结
    动态规划 BZOJ1925 地精部落
    线性DP SPOJ Mobile Service
    线性DP codevs2185 最长公共上升子序列
    数位DP POJ3208 Apocalypse Someday
    线性DP POJ3666 Making the Grade
    杨氏矩阵 线性DP? POJ2279 Mr.Young's Picture Permutations
    tarjan强连通分量 洛谷P1262 间谍网络
    树链剖分 BZOJ3589 动态树
    二分图 BZOJ4554 [Tjoi2016&Heoi2016]游戏
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/9079764.html
Copyright © 2011-2022 走看看