zoukankan      html  css  js  c++  java
  • 004_basic_features_of_bash

    1. The command history
        1.1 history :Display or manipulate the history list.
            [root@promote tmp]# history    //Display the history list.
            -a: Append newly executed commands to the command history file(.bash_history).
                [root@bogon ~]# history -a
            -d: Delete specified command in the history list. (not in the command history file)
                [root@bogon ~]# history -d 2    //Delete the second command of the history list.
            -c: Clear the history list.(not the commands in the command history file) 
                [root@bogon ~]# history -c
            history # : Display the last N commands of the history list.
                [root@bogon ~]# history 5    //Display the last five commands of the history list.
        1.2 Environment variables related to command history.
            HISTSIZE : The number of entries in the command history.
            HISTFILE : Each user has his own command history file which is located in HOME_DIR and named .bash_history. 
                the full path of .bash_history was saved in HISTFILE.
            HISTFILESIZE : The number of entries in .bash_history.Experience carefully the difference between HISTSIZE and HISTFILESIZE.Here's the explanation
                When a user logs in to the shell,the shell will read the entries(条目) in .bash_history.
                When a user logs in to the shell,newly executed commands are only recorded in the cache and whill be appended to .bash_history when the user exits. 
        1.3 shortcuts
            !#: Executed Nth command of history list.
                [root@bogon ~]# !5  //Execute the fifth command in history list(not in .bash_history)
            !string:Execute the last command begins with "string";
            !!: Execute the previous command. 
                [root@bogon ~]# !!  //Execute the previous command
        1.4 Call the last parameter of the previous command.
            !$ : 
            ESC,. : Press the ESC key first,then release,then press the . key
        1.5 Control the way commands are recorded by setting the value of HISTCONTROL(a environment variables).
            HISTCONTROL
                ignoredups:Ignore repeated commands,continues and the same is repeated.
                ignorespace:Ignore commands begins with backspaces,so you can add a space before the command if you want the command not recorded
                ignoreboth:ignoredups, ignorespace;
            Changing the value of HISTCONTROL only works for the current shell process.The value will not change in a new terminnal.
                [root@bogon ~]# export HISTCONTROL="ignoredups"
    2.command completion
        2.1 command:Executing command is running programs in essence.
            Internal command : We don't need to find it.
            External command : The bash will find the file named after the given command name according to the paths from left to right in PATH . 
        2.2 compelete directly :press Tab. The string given by the user has only one corresponding command. 
            If the string given by the user has multiple corresponding commands,pressing TAB again will print all corresponding commands.    
    3.path compeletion :Consider the user-given string as the beginning of the path,and search the file named after the user-given string from the parent directory of the current directory.
        compelete directly : If the user-given string has only one corresponding file,the shell will compelete directly.    
        or pressing TAB again will print all the corresponding files. 
    4.command line expansion
        ~: refers to user's home directory
        ~USERNAME:refers to specified user's home directory
        {}:with a Comma-separated list in the Curly braces,and the list will expand into multiple paths.
            /tmp/{a,b} = /tmp/a, /tmp/b
        /tmp/{tom,jerry}/hi = /tmp/tom/hi, /tmp/jerry/hi
        (1) How create /tmp/x/y1, /tmp/x/y2, /tmp/x/y1/a, /tmp/x/y1/b, /tmp/x/y2/a, /tmp/x/y2/b
        mkdir /tmp/x/{y1,y2}/{a,b}        
        (2) How create x_m, y_m, x_n, y_n
        mkdir {x,y}_{m,n}
        (3) How create /tmp/bin, /tmp/sbin, /tmp/usr, /tmp/usr/bin, /tmp/usr/sbin
        mkdir -p /tmp/{bin,sbin,usr/{bin,sbin}}    

    5.The result of the execution of the command and the result of the execution state.
        5.1 the result of the execution state : sucess or fail
            bash saves the result of execution state of the last command in $? which is a specicial value.
                0 : refers to sucess.
                1 - 255 : refers to fail.
        5.2 the return value

    6.命令别名:通过alias命令来管理命令别名
        6.1 alias : Define or display aliases.
            [root@bogon ~]# alias   //显示当前所有别名
            [root@bogon ~]# alias cdnet='cd /etc/sysconfig/network-scripts'  //定义一个别名
        6.2 在命令行中定义的别名,仅对当前shell进程有效;如果想永久有效,要定义在配置文件中;
            仅对当前用户:~/.bashrc
            对所有用户有效:/etc/bashrc
            Note: 编辑配置给出的新配置不会立即生效,需要重新读取编辑的配置文件;
                bash进程重新读取配置文件:
                    source /path/to/config_file
                    .  /path/to/config_file
        6.3 撤销别名
            [root@bogon ~]# unalias   //撤销所有别名
            [root@bogon ~]# alias cdnet  //撤销cdnet
        
        6.4 Note: 如果别名同原命令的名称,则如果要执行原命令,可使用"COMMAND";
    7.glob(globing)
        7.1 * : 任意长度的任意字符
        7.2 ? : 任意单个字符
        7.3 [] : 匹配指定范围内的任意单个字符
            [0-9] : 0-9之间的任意一个
            [a-z] : 不区分字符大小写
            [A-Z] : 这里只通配大写,上面的不区分大小写
        7.4 [^] : 匹配指定范围外的任意单个字符
            [^0-9]
            [^0-9a-z] : 任意的特殊字符
        7.5 专用字符集合
            [:digit:]:任意数字,相当于0-9  [[:digit:]]相当于[0-9]
            [:lower:]:任意小写字母
            [:upper:]: 任意大写字母
            [:alpha:]: 任意大小写字母
            [:alnum:]:任意数字或字母
            [:space:]:空格字符
            [:punct:]:标点符号
        7.5 实例
            1)、显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录;
                # ls -d /var/l*[0-9]*[[:lower:]]

            2)、显示/etc目录下,以任意一位数字开头,且以非数字结尾的文件或目录;
                # ls -d /etc/[0-9]*[^0-9]

            3)、显示/etc/目录下,以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录;
                # ls /etc/[^[:alpha:]][[:alpha:]]*

            4)、复制/etc目录下,所有以m开头,以非数字结尾的文件或目录至/tmp/mageedu目录中;
                # cp -a /etc/m*[^0-9] /tmp/mageedu

            5)、复制/etc目录下,所有以.d结尾的文件或目录至/tmp/magedu.com目录中;
                # cp -a /etc/*.d /tmp/magedu.com

            6)、复制/etc目录下,所以有.conf结尾,且以m,n,r,p开头的文件或目录至/tmp/mageedu.com目录中;
                # cp -a /etc/[mnrp]*.conf  /tmp/mageedu.com

    8. Variable types:
        8.1 strong and weak type :
            strong type : you must to specify the type when you define a variable,and variables that participate in operations must meet the type requirement.
            weak type : you don't need to specify the type when you define a variable ,variable type is converted implicitly.    

            The difference between '' ""
                [root@bogon ~]# a=3
                [root@bogon ~]# b=4
                [root@bogon ~]# echo '$a+$b'
                $a+$b
                [root@bogon ~]# echo "$a+$b"
                3+4

                '$name' : Variables are not replaced by its value
                "$name" : Variables are replaced by its value
        8.2 Variety of variables
            classify variables according to the range in which the variable takes effect.
                Local variables(本地变量) : the range in which variables take effect is only the current shell process,and it does't work in other shell processes including the child processes.
                    Variable assignment : name='value'
                        1) : name="aaa"  /   name="aaa"  // name=aaa(recommanded if there is no spacekey in it)
                        2) : name="$name1"  //The value of name1 was assigned to name.
                        3) : name=`COMMAND`  /  name=$(COMMAND)    //Was called command reference
                            [root@bogon ~]# namefile=$(date)
                    Variable reference : ${name}  / $name(not recommanded)    
                Environment variables : the range in which variables take effect is the current shell process and its child shell process
                    [root@bogon ~]# declare -x name=Obama      //Define a environment variable
                    Variable assignment : export name=VALUE    /    declare -x name
                    Variable reference : ${name}  / $name(not recommanded)    
                    Show current environment variables : export / env /printenv
                    Destroy : unset name
                    export PATH="$PATH:/usr/local/bin"    //adds a path to PATH.
                Local variables(局部变量) : the range is a function.
                Location variables(位置变量) : Command line parameters, referenced by $1,$2,$3....
                    $1,$2,$3....
                    $0 : the command itself
                    $* / $@ : All of the parameters passed to a script
                    $# : The number of parameters passed to a script
                    shift : Used to remove paramter
                Special varibles(特殊变量) : $?(the status result of the last command),$0(the command itself),$#,$*,$@
                Readonly variables :
                    readonly name   / declare -r name
        8.3 set/unset
            set : View all of the variables of the current shell
            unset : destroy a variable

  • 相关阅读:
    记sql语句
    thinkPHP小记
    php操作目录和文件
    ubuntu搭配lamp
    phpStorm实用快捷键
    在wamp中www的文件夹(项目)打不开解决办法
    php正则表达式
    远程桌面,出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正
    discuz增加中文验证码
    react webpack 环境配置
  • 原文地址:https://www.cnblogs.com/liujun5319/p/9600800.html
Copyright © 2011-2022 走看看