1 linux shell base

[TOC]

A, DAY1

1. What is the shell

  • A shell is a command interpreter, providing interaction between the user and the machine (shell script is a manifestation of the shell)

  • Support specific syntax, such as logic, loop (if, for, while)
  • Each user can have their own specific shell
    • root:x:0:0:root:/root:/bin/bash
  • CentOS7 default shell is bash (Bourne Agin Shell)
  • There zsh, ksh, etc.

2. Command History

  • history command
  • .bash_history Record store command files, if non-terminal exit, logout normal exit, command incomplete documentation.
  • Maximum 1000, 1000 by default, can be modified
  • Variable HISTSIZE, see echo $HISTSIZEif the display of more than 1000, because the command is temporarily stored in memory, not written into the file, history-cbut does not clear the memory may clear the records .bash_historyin the record
  • /etc/profileModify, after the changes take effect immediately to re-enter the terminal or execution source /etc/profile
  • HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " Increasing this variable execution time, the command corresponding to the record can be, for example: 1005 2019/11/12 10:29:47 w
  • Stored permanently chattr +a ~/.bash_historyeven over 1000 did not have permission to delete
  • On a command !!
  • ! Nn is a number, execute the command history recorded in the corresponding number of
  • !word

3. Command completion and aliases

  • tab key, knock, knock twice
  • centos7 support parameter completion, the installation yum install -y bash-completionwill require a reboot to take effectsystemctl restart network
  • alias alias to command a name again alias restartnet="systemctl restart network"to cancel alias:unalias restartnet
  • Each user has its own configuration file aliases ~/.bashrc
  • ls /etc/profile.d/
  • alias into custom~/.bashrc

    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias restartnet="systemctl restart network"
    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi

4. wildcard, input and output redirection

  • ls *.txt
  • ls ?.txt
  • ls [0-9].txt
  • ls {1,2}.txt
  • cat 1.txt >2.txt
  • cat 1.txt >> 2.txt
  • ls aaa.txt 2> err // 2> represents the error redirection
  • ls aaa.txt 2 >> err // 2 >> indicates an error appending redirection
  • &> Combination of right and wrong redirection
  • wc -l < 1.txt
  • command >1.txt 2>&1

Two, DAY2

The job control pipe, and

  • cat 1.txt |wc -l ; cat 1.txt |grep 'aaa'
  • The results back to the front of the command
  • ctrl z suspend a task
  • jobs View background tasks
  • bg [id] transferred back to the task
  • fg [id] the task to the foreground.
  • Plus direct throw after the command & background sleep 100 &

6.shell variable

  • PATH, HOME, PWD, LOGNAME often wind of some of the system variables
  • env command can be used to view the system variables, the system is generally capitalized variable name, a variable value may be a value, or a string
  • set command a lot more variables, including variables and user-defined
  • Custom Variables a = 1
  • Variable name rule: letters, numbers underlined, the first number can not be a1 = 4 a_1 = 4 but not 1a = 4
  • Variable values ​​will need enclosed in single quotes special symbols
    • a='a b c' Inside the spaces use single quotes
      [root@mydb1 ~]# a="a$bc"
      [root@mydb1 ~]# echo $a
      a
      [root@mydb1 ~]# a='a$bc'
      [root@mydb1 ~]# echo $a
      a$bc
      在取值时,单引号和双引号结果不一样,所以在取值时要用单引号
  • Use double quotes accumulation variable, the variable accumulation
    [root@mydb1 ~]# a=1
    [root@mydb1 ~]# b=2
    [root@mydb1 ~]# echo $a$b
    12
    [root@mydb1 ~]# a='a$bc'
    [root@mydb1 ~]# echo $a$b
    a$bc2
    [root@mydb1 ~]# c="a$bc"
    [root@mydb1 ~]# echo $c
    a
    [root@mydb1 ~]# c="a$b"c
    [root@mydb1 ~]# echo $c
    a2c
  • Global variables export b = 2

    [root@mydb1 ~]# w
     14:44:15 up 24 days, 23:49,  2 users,  load average: 0.07, 0.04, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.1.182    10:18    7:11   0.37s  0.37s -bash
    root     pts/1    192.168.1.182    14:44    0.00s  0.03s  0.01s w
    [root@mydb1 ~]# echo $SSH_TTY  查看当前终端的tty
    /dev/pts/1
    [root@localhost ~]# yum -y install psmisc
    [root@localhost ~]# pstree
    systemd─┬─NetworkManager───2*[{NetworkManager}]
            ├─VGAuthService
            ├─agetty
            ├─auditd───{auditd}
            ├─crond
            ├─dbus-daemon───{dbus-daemon}
            ├─firewalld───{firewalld}
            ├─master─┬─pickup
            │        └─qmgr
            ├─polkitd───6*[{polkitd}]
            ├─rsyslogd───2*[{rsyslogd}]
            ├─sshd───sshd───bash───pstree
            ├─systemd-journal
            ├─systemd-logind
            ├─systemd-udevd
            ├─tuned───4*[{tuned}]
            └─vmtoolsd
    [root@localhost ~]# 
    [root@localhost ~]# 
    [root@localhost ~]# bash  //bash命令进入到子shell
    [root@localhost ~]# pstree
    systemd─┬─NetworkManager───2*[{NetworkManager}]
            ├─VGAuthService
            ├─agetty
            ├─auditd───{auditd}
            ├─crond
            ├─dbus-daemon───{dbus-daemon}
            ├─firewalld───{firewalld}
            ├─master─┬─pickup
            │        └─qmgr
            ├─polkitd───6*[{polkitd}]
            ├─rsyslogd───2*[{rsyslogd}]
            ├─sshd───sshd───bash───bash───pstree  //这里可以看出在子shell上执行了pstree命令
            ├─systemd-journal
            ├─systemd-logind
            ├─systemd-udevd
            ├─tuned───4*[{tuned}]
            └─vmtoolsd
    [root@localhost ~]# a=linux  
    [root@localhost ~]# echo $a
    linux
    [root@localhost ~]# bash
    [root@localhost ~]# echo $a    非全局变量下,进入子shell,无法获取父shell中a=linux的值,这是因为在非全局变量下,当前定义的变量只在当前shell中生效,而不能在子shell生效
    
    [root@localhost ~]# export b=123  父shell下定义全局变量 b=123,这样定义全局变量只在当前终端有效,重新打开新的终端无效。
    [root@localhost ~]# echo $b      
    123
    [root@localhost ~]# bash         进入子shell,仍然可以获得父shell定义的b=123的值
    [root@localhost ~]# echo $b
    123
    [root@localhost ~]# export c=456  在子shell中定义c=456
    [root@localhost ~]# echo $c
    456
    [root@localhost ~]# exit    退出子shell进入父shell
    exit
    [root@localhost ~]# echo $c  在父shell中无法获得在子shell中定义c=456的值,父shell与子shell的关系是从上到下的,而不能从下到上
    
  • unset variables, cancel the assignment, unset b unset the variable name

7. environment variable configuration file

  • System-level environment variables, configuration files etc, takes effect globally.
    • /etc/profile User environment variables, interactive, logged in execution, enter a user name, ip, port, password will automatically load, then the profile will automatically call bashrc.
    • /etc/bashrc Users do not log in, execute the shell will take effect, just run the shell script, which is called bashrc configuration
  • User-level environment variable, in each user's home directory
    • ~/.bashrc
    • ~/.bash_profile . .bash_profile或者source .bash_profile
    • ~/.bash_history
    • ~/.bash_logout Some actions need to be made when a user-defined exit, such as: delete the command history when you exit, you can delete the command history command, on the inside .bash_logout
    • PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
      • vim /etc/bashrc In the definition

        Three, DAY3

8. special symbols

  • * 任意个任意字符
  • ? 任意一个字符
  • # 注释字符
  • \ 脱义字符 For c = '$ a $ b' result of $ a $ b in addition to single quotes, may also be used off signifier c = \ $ a \ $ b.
  • | 管道符

9. Several pipelines and related commands

  • cut 分割, -D -f delimiter specified block number of the specified number of characters -c
    • Demo:
      [root@localhost ~]# cat /etc/passwd |head -2
      root:x:0:0:root:/root:/bin/bash
      bin:x:1:1:bin:/bin:/sbin/nologin
      [root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
      root
      bin
      [root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
      root:x
      bin:x
      [root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,5
      root:root
      bin:bin
      [root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-5
      root:x:0:0:root
      bin:x:1:1:bin
      [root@localhost ~]# cat /etc/passwd |head -2 |cut -c 4
      t
      :
  • sort 排序, -N -r reverse order to sort digital -t separator -kn1 / -kn1, n2
    • Demo:
      [root@localhost ~]# cat /etc/passwd |head -4 |sort
      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
      root:x:0:0:root:/root:/bin/bash
      [root@localhost ~]# cat /etc/passwd |head -4 |sort -n
      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
      root:x:0:0:root:/root:/bin/bash
      [root@localhost ~]# cat /etc/passwd |head -4 |sort -nr
      root:x:0:0:root:/root:/bin/bash
      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
  • wc -l 统计行数 -w -m character counts statistics word

    • Show
      
      [root@localhost ~]# cat 1.txt 内容有6个字符
      123
      abc
      [root@localhost ~]# cat -A 1.txt 
      123$
      abc$
      [root@localhost ~]# wc -m 1.txt  
      8 1.txt   统计有8个字符,不要忘记隐藏的换行符

    [root @ localhost ~] # CAT 1.txt
    123
    abc, F23
    [root @ localhost ~] # 1.txt WC -w number of words statistics, separated by a space
    3 1.txt

  • uniq 去重, -C count the number of rows

    • Show
      
      [root@localhost ~]# uniq 1.txt  直接uniq没有去重
      123
      abc  ,f23
      123
      abc
      1
      2
      1

    [root @ localhost ~] # sort 1.txt | uniq deduplication first sort, uniq typically used with Sort
    . 1
    123
    2
    ABC
    ABC, F23

    [root @ localhost ~] # sort 1.txt | uniq -c Sort go first and then re-count the number of rows
    2 1
    2 123
    1 2
    1 abc
    1 abc, F23

  • tee 和 &gt; 类似, While also redirect screen display

    • Show
      
      [root@localhost ~]# cat 1.txt 
      123
      abc  ,f23
      123
      abc
      1
      2
      1

    [root @ localhost ~] # sort 1.txt | uniq -c | tee a.txt | tee acts like>, but it will redirect the contents shown on the display screen
    2. 1
    2 123
    . 1 2
    . 1 ABC
    . 1 abc, f23

    [root@localhost ~]# sort 1.txt |uniq -c |tee -a a.txt |tee -a 相当于>>
    2 1
    2 123
    1 2
    1 abc
    1 abc ,f23

    [root@localhost ~]# cat a.txt
    2 1
    2 123
    1 2
    1 abc
    1 abc ,f23
    2 1
    2 123
    1 2
    1 abc
    1 abc ,f23

  • tr 替换字符, Tr 'a' 'b', replacing case tr '[az]' '[AZ]'

    • Show
      [root@localhost ~]# echo "aminglinux" |tr 'a' 'A'   替换单个
      Aminglinux
      [root@localhost ~]# echo "aminglinux" |tr '[al]' '[AL]'  替换多个
      AmingLinux
      [root@localhost ~]# echo "aminglinux" |tr '[a-z]' '[A-Z]'  替换指定范围
      AMINGLINUX
      [root@localhost ~]# echo "aminglinux" |tr '[a-z]' '1'  也可以替换成数字
      1111111111
  • split 切割, -B size (in bytes default), - l rows, -d numeric suffix is ​​added

    • Show
      [root@localhost test]# split -b 2M 2.txt 指定切割后每个文件的大小
      [root@localhost test]# ls
      2.txt  xaa  xab  xac  xad  xae
      [root@localhost test]# ll -h
      总用量 20M
      -rw-r--r--. 1 root root  10M 2月  24 08:57 2.txt
      -rw-r--r--. 1 root root 2.0M 2月  24 09:04 xaa
      -rw-r--r--. 1 root root 2.0M 2月  24 09:04 xab
      -rw-r--r--. 1 root root 2.0M 2月  24 09:04 xac
      -rw-r--r--. 1 root root 2.0M 2月  24 09:04 xad
      -rw-r--r--. 1 root root 2.0M 2月  24 09:04 xae
      [root@localhost test]# split -b 2M 2.txt 2019.  指定切割后每个文件的大小以及文件的前缀
      [root@localhost test]# ls
      2019.aa  2019.ab  2019.ac  2019.ad  2019.ae  2.txt  xaa  xab  xac  xad  xae
      [root@localhost test]# split -l 10000 2.txt 201911.   指定切割文件的行数以及文件的前缀
      [root@localhost test]# ls
      201911.aa  201911.ab  201911.ac  201911.ad  201911.ae  201911.af  201911.ag  201911.ah  2.txt

      10.shell special symbols

  • $ Variable prefix! $ Combination, which represents the regular end of the line
  • ; Multiple commands written line, separated by a semicolon
    • Show
      [root@localhost test]# for i in `seq 1 10`
      > do
      > echo $i
      > done
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      [root@localhost test]# for i in `seq 1 10`; do echo $i; done
      [root@localhost test]# ls 2.txt ; wc -l 2.txt 
      2.txt
      77517 2.txt
  • ~ User home behind a regular expression represented matcher
  • & Back into the command, the command will throw the background
  • &gt; &gt;&gt; 2&gt; 2&gt;&gt; &&gt;
  • [ ] Of a specified character, [0-9], [a-zA-Z], [abc]
  • && and ||, between a command

    • Show
      
      [root@localhost test]# ls 2.txt || wc -l 2.txt 
      2.txt
      [root@localhost test]# ls 2a.txt || wc -l 2.txt 
      ls: 无法访问2a.txt: 没有那个文件或目录
      77517 2.txt
      || 如果前面执行成功,就不执行后面。如果前面执行不成功,才会执行后面

    [root @ localhost the Test] # LS 2a.txt the -l && WC 2.txt
    LS: can not access 2a.txt: No such file or directory
    [root @ localhost the Test] # LS 2.txt the -l && WC 2.txt
    2 .txt
    77517 2.txt
    front unsuccessful implementation, will not be back to perform, execute successfully before, will be back to perform

    [root @ localhost the Test] # [-d aminglinux] || mkdir aminglinux
    [root @ localhost the Test] # LS
    201911.aa 201911.ac 201911.ae 201911.ag 2.txt
    201911.ab 201911.ad 201911.af 201 911. aminglinux AH
    [root @ localhost the Test] # [-d aminglinux] && mkdir aminglinux
    mkdir: can not create directory "aminglinux": file already exists

Guess you like

Origin blog.51cto.com/chenshengsheng/2452039