• bash内部命令-2


    http://www.gnu.org/software/bash/

    http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/

    [root@250-shiyan ~]# rpm -qa|grep bash
    bash-4.1.2-15.el6_4.x86_64
    type-.-source-let-(())-history-declare

    [root@250-shiyan prog]# help type
    type: type [-afptP] name [name ...]
        Display information about command type.
    
        For each NAME, indicate how it would be interpreted if used as a
        command name.
    
        Options:
          -a        display all locations containing an executable named NAME;
            includes aliases, builtins, and functions, if and only if
            the `-p' option is not also used
          -f        suppress shell function lookup
          -P        force a PATH search for each NAME, even if it is an alias,
            builtin, or function, and returns the name of the disk file
            that would be executed
          -p        returns either the name of the disk file that would be executed,
            or nothing if `type -t NAME' would not return `file'.
    ####要么返回可能被执行的磁盘文件名,要么假如type -t NAME不返回file则什么也不做。
    -t output a single word which is one of `alias', `keyword', `function', `builtin', `file' or `', if NAME is an alias, shell reserved word, shell function, shell builtin, disk file, or not found, respectively ####如果NAME是一个别名,shell保留字,shell函数,shell内部命令,磁盘file,或者没找到,那么分别对应输出alias,keyword,function,builtin,file,或者空的其中一个单词。 Arguments: NAME Command name to be interpreted. ####被解读的命令名 Exit Status: Returns success if all of the NAMEs are found; fails if any are not found.
    ####假如所有的NAMEs都找到返回成功;假如任何一个未找到就返回失败。 typeset: typeset [
    -aAfFilrtux] [-p] name[=value] ... Set variable values and attributes. Obsolete. See `help declare'. [root@250-shiyan prog]# type -a fdisk for cd fdisk is /sbin/fdisk for is a shell keyword cd is a shell builtin [root@250-shiyan prog]# type -t fdisk for cd ls __udisks file keyword builtin
    alias
    function [root@
    250-shiyan prog]# type -p dir fdisk /usr/bin/dir /sbin/fdisk
    [root@250-shiyan prog]# declare -f|more  先查看函数名,再查看类型
    [root@250-shiyan prog]# type __udisks
    __udisks is a function

    [root@250-shiyan frag]# type -a : [[ {
    : is a shell builtin
    [[ is a shell keyword
    { is a shell keyword
    [root@250-shiyan frag]# type -a [
    [ is a shell builtin
    [ is /usr/bin/[
    [root@250-shiyan frag]# help :
    :: :
        Null command.
    
        No effect; the command does nothing.
    
        Exit Status:
        Always succeeds.
    
    http://blog.csdn.net/ysdaniel/article/details/7905818    Shell中[和[[的异同
    [root@250-shiyan frag]# help [
    [: [ arg... ]
        Evaluate conditional expression.
    
        This is a synonym for the "test" builtin, but the last argument must
        be a literal `]', to match the opening `['.
    ####是内置test的同义词,但最后一个参数必须是一个字符],来匹配开始的[
    [[ ... ]]: [[ expression ]]
        Execute conditional command.
    
        Returns a status of 0 or 1 depending on the evaluation of the conditional
        expression EXPRESSION.  Expressions are composed of the same primaries used
        by the `test' builtin, and may be combined using the following operators:
    
          ( EXPRESSION )    Returns the value of EXPRESSION
          ! EXPRESSION              True if EXPRESSION is false; else false
          EXPR1 && EXPR2    True if both EXPR1 and EXPR2 are true; else false
          EXPR1 || EXPR2    True if either EXPR1 or EXPR2 is true; else false
    
        When the `==' and `!=' operators are used, the string to the right of
        the operator is used as a pattern and pattern matching is performed.
        When the `=~' operator is used, the string to the right of the operator
        is matched as a regular expression.
    
        The && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to
        determine the expression's value.
    
        Exit Status:
        0 or 1 depending on value of EXPRESSION.
    ####.与source是一样的,就不再述。
    [root@250-shiyan frag]# help .
    .: . filename [arguments]
        Execute commands from a file in the current shell.
    ####在当前shell中,从一个文件中执行命令。
        Read and execute commands from FILENAME in the current shell.  The
        entries in $PATH are used to find the directory containing FILENAME.
        If any ARGUMENTS are supplied, they become the positional parameters
        when FILENAME is executed.
    ####在当前shell中,从FILENAME中读取并执行命令。在$PATH中的项目是被用作查找包括FILENAME的目录。当FILENAME被执行时,如果有参数供应,他们将成为位置参数。
        Exit Status:
        Returns the status of the last command executed in FILENAME; fails if
        FILENAME cannot be read.
    
    [root@84-monitor ~]# cat a
    #!/bin/bash
    ip_conns=`ssh $1 "netstat -ant| grep EST | wc -l"`
    echo $ip_conns
    [root@84-monitor ~]# . a 192.168.2.109
    862
    [root@84-monitor ~]# . a 192.168.2.2
    213
    [root@84-monitor ~]# source a 192.168.2.225
    22
    [root@84-monitor ~]# source a 192.168.2.2
    214
    [root@250-shiyan frag]# help let
    let: let arg [arg ...]
        Evaluate arithmetic expressions.
    
        Evaluate each ARG as an arithmetic expression.  Evaluation is done in
        fixed-width integers with no check for overflow, though division by 0
        is trapped and flagged as an error.  The following list of operators is
        grouped into levels of equal-precedence operators.  The levels are listed
        in order of decreasing precedence.
    ####按照优先级的降序列出
            id++, id--      variable post-increment, post-decrement
            ++id, --id      variable pre-increment, pre-decrement
            -, +            unary minus, plus
            !, ~            logical and bitwise negation
            **              exponentiation
            *, /, %         multiplication, division, remainder
            +, -            addition, subtraction
            <<, >>          left and right bitwise shifts
            <=, >=, <, >    comparison
            ==, !=          equality, inequality
            &               bitwise AND
            ^               bitwise XOR
            |               bitwise OR
            &&              logical AND
            ||              logical OR
            expr ? expr : expr
                            conditional operator
            =, *=, /=, %=,
            +=, -=, <<=, >>=,
            &=, ^=, |=      assignment
    
        Shell variables are allowed as operands.  The name of the variable
        is replaced by its value (coerced to a fixed-width integer) within
        an expression.  The variable need not have its integer attribute
        turned on to be used in an expression.
    
    ####shell变量被允许作为操作数。在一个表达式里,变量的名字被替换为它的值。
    
        Operators are evaluated in order of precedence.  Sub-expressions in
        parentheses are evaluated first and may override the precedence
        rules above.
    ####操作符按照优先顺序被求值,在小括号里的子表达式首先被求值,并且可以覆盖上文的优先规则。
    
        Exit Status:
        If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise..
    
    [root@250-shiyan frag]# let num=5+8/2;echo $num
    9
    [root@250-shiyan frag]# let num=(5+8)/2;echo $num
    6
    [root@250-shiyan frag]# let num=5*2;echo $num
    10
    [root@250-shiyan frag]# let num=5/2;echo $num
    2
    
    [root@250-shiyan frag]# help (
    (( ... )): (( expression ))
        Evaluate arithmetic expression.
    
        The EXPRESSION is evaluated according to the rules for arithmetic
        evaluation.  Equivalent to "let EXPRESSION".
    
        Exit Status:
        Returns 1 if EXPRESSION evaluates to 0; returns 0 otherwise.
    
    shell里的4种运算格式,只有bc与awk可以浮点运算,其它三个只能整数运算。
    [root@250-shiyan frag]# ((num=5+2));echo $num
    7
    [root@250-shiyan frag]# let "num=5+2";echo $num
    7
    [root@250-shiyan frag]# x=`expr 5 * 2`;echo $x
    10
    [root@250-shiyan frag]# echo "$(echo 'scale=2;40*100/90' | bc)%"
    44.44%
    while loop 的原理与 for loop 稍有不同:它不是逐次处理清单中的变量值,而是取决于 while 后面的命令行之 return value :
    * 若为 ture ,则执行 dodone 之间的命令,然后重新判断 while 后的 return value 。
    * 若为 false ,则不再执行 dodone 之间的命令而结束循环。 
    * 若 while 的测试结果永远为 true 的话,那循环将一直永久执行下去:
    一旦你能够理解 while loop 的话,那就能理解 until loop :
    * 与 while 相反,until 是在 return value 为 false 时进入循环,否则结束。
    在结束本shell之前,再跟大家补充两个与 loop 有关的命令:
    * break
    * continue
    这两个命令常用在复合式循环里,也就是在do ... done之间又有更进一层的 loop,当然,用在单一循环中也未尝不可啦... ^_^
    break 是用来打断循环,也就是"强迫结束" 循环。若 break 后面指定一个数值 n 的话,则"从里向外"打断第 n 个循环,默认值为 break 1 ,也就是打断当前的循环。
    在使用 break 时需要注意的是,它与 return 及 exit 是不同的:
    * break 是结束 loop
    * return 是结束 function
    * exit 是结束 script/shell
    而 continue 则与 break 相反:强迫进入下一次循环动作。若你理解不来的话,那你可简单的看成:在 continue 到 done 之间的句子略过而返回循环顶端...与 break 相同的是:continue 后面也可指定一个数值 n ,以决定继续哪一层(从里向外计算)的循环,默认值为 continue 1 ,也就是继续当前的循环。
    
    continue
    恢复下一次迭代循环,本次就不执行了
    下面这个脚本说明,如果文件名中有while,则不输出,如果想要只输出while行,只需要将=改为!=即可。这就是continue的作用。
    #!/bin/bash
    for shname in `ls *.sh`
    do
      name=`echo "$shname" | awk -F. '{print $1}'`
      if [ $name = while ]
      then
      continue
      fi
      echo $name
    done
    符合条件的,结束本次循环 [root@
    250-shiyan frag]# bash con1 the number is:1 the number is:2 the number is:3 the number is:8 the number is:9 the number is:10 [root@250-shiyan frag]# cat con1 for i in `seq 10` do if [ $i -gt 3 ] && [ $i -lt 8 ] then continue fi echo "the number is:$i" done [root@250-shiyan frag]# bash con1 the number is:1 the number is:2 the number is:4 the number is:5 the number is:6 the number is:7 the number is:9 the number is:10 [root@250-shiyan frag]# cat con1 for i in `seq 10` do if [ $i -eq 3 ] || [ $i -eq 8 ] then continue fi echo "the number is:$i" done break 退出本层循环 [root@250-shiyan frag]# bash con1 please input:q [root@250-shiyan frag]# bash con1 please input:Q [root@250-shiyan frag]# bash con1 please input: the number is:1 the number is:2 the number is:3 the number is:4 the number is:5 the number is:6 the number is:7 the number is:8 the number is:9 the number is:10 [root@250-shiyan frag]# cat con1 echo -n "please input:" read num for i in `seq 10` do if [ "$num" = "q" ] || [ "$num" = "Q" ] then break fi echo "the number is:$i" done [root@250-shiyan frag]# bash con1 please input:q [root@250-shiyan frag]# bash con1 please input:Q [root@250-shiyan frag]#
    http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0706/2700_2.html
    
    echo "export HISTTIMEFORMAT='%F %T '" >> /etc/profile
    
    当你从命令行执行 history 命令后,通常只会显示已执行命令的序号和命令本身。如果你想要查看命令历史的时间戳,那么可以执行:
    # export HISTTIMEFORMAT='%F %T '
    [root@250-shiyan ~]# history
      984  2015-03-23 13:36:12 set +o history
      985  2015-03-23 13:36:12 history
      986  2015-03-23 13:36:12 echo $HISTSIZE
      987  2015-03-23 13:36:12 env
      988  2015-03-23 13:36:12 env|grep HIS
      989  2015-03-23 13:36:12 vi .bash_history
      990  2015-03-23 13:36:12 echo $HISTSIZE
      991  2015-03-23 13:36:12 vi /etc/profile
      992  2015-03-23 13:36:12 exit
      993  2015-03-23 11:15:38 history
      994  2015-03-23 11:16:54 free
      995  2015-03-23 11:16:57 vmstat 1
      996  2015-03-23 11:17:00 history
      997  2015-03-23 11:17:39 exit
      998  2015-03-23 11:54:12 history
      999  2015-03-23 11:57:47 shopt -p
     1000  2015-03-23 11:59:39 exit
     1001  2015-03-23 13:36:15 history
    [root@250-shiyan ~]# cat .bash_history
    free
    #1427080617
    vmstat 1
    #1427080620
    history
    #1427080659
    exit
    #1427082852
    history
    #1427083067
    shopt -p
    #1427083179
    exit
    
    [root@250-shiyan ~]# echo !yum:1
    echo install
    install
    注意:这个功能只能用在当 HISTTIMEFORMAT 这个环境变量被设置之后,之后的那些新执行的 bash 命令才会被打上正确的时间戳。在此之前的所有命令,都将会显示成设置 HISTTIMEFORMAT 变量的时间。
    shell变量仅供shell builtins,function,keyword,来使用。
    环境变量供shell以外的程序来使用。
    shell变量 HISTFILE HISTSIZE HISTFILESIZE HISTTIMEFORMAT HISTCONTROL HISTIGNORE shell选项 cmdhist lithist histappend declare或typeset内建命令(它们是完全相同的)可以用来限定变量的属性.这是在某些编程语言中使用的定义类型不严格的方式。命令declare是bash版本2之后才有的。命令typeset也可以在ksh脚本中运行。 功能说明:声明 shell 变量。 语  法:declare [
    +/-][rxi][变量名称=设置值] 或 declare -f 补充说明:declare为shell指令,在第一种语法中可用来声明变量并设置变量的属性([rix]即为变量的属性),在第二种语法中可用来显示shell函数。若不加上任何参数,则会显示全部的shell变量与函数(与执行set指令的效果相同)。 参  数:  +/-  "-"可用来指定变量的属性,"+"则是取消变量所设的属性。  -f  仅显示函数。  r  将变量设置为只读。  x  指定的变量会成为环境变量,可供shell以外的程序来使用。  i  [设置值]可以是数值,字符串或运算式。 declare/typeset 选项 -r 只读 declare -r var1 (declare -r var1与readonly var1作用相同) 这大致和C的const限定词相同.一个试图改变只读变量值的操作将会引起错误信息而失败.
    [root@localhost ~]# rpm -qf /etc/security/
    pam-1.1.1-13.el6.x86_64
    下面的几个文件是由pam生成的,它也会影响到资源限制。
    /etc/security/limits.conf
    /etc/security/limits.d
    /etc/security/limits.d/90-nproc.conf

    先查看块大小,得到单位为K。 [root@localhost
    ~]# dumpe2fs /dev/sda1 |grep "Block size" dumpe2fs 1.41.12 (17-May-2010) Block size: 1024 再做设置,限制为204M,加入到启动文件中。 [root@localhost ~]# grep ulimit .bash_profile ulimit -f 204800 然后重新登录,再查看 [root@localhost ~]# ulimit -a|grep "file size" core file size (blocks, -c) 0 file size (blocks, -f) 204800 [root@localhost ~]# dd if=/dev/zero of=4.img File size limit exceeded [root@localhost ~]# ll total 819256 -rw-r--r--. 1 root root 16 May 17 2015 123 -rw-r--r--. 1 root root 209715200 Feb 8 21:34 1.img -rw-r--r--. 1 root root 209715200 Feb 8 21:38 2.img -rw-r--r--. 1 root root 209715200 Feb 8 21:40 3.img -rw-r--r--. 1 root root 209715200 Feb 8 21:46 4.img 由此可知,ulimit -f不限制由此终端的shell的总文件大小,而是单次上限。

    pending signals  未处理信号,等待信号。

    删除除某个目录之外的所有
    只需要开启下面这个模式匹配功能,就可以用除*,?之外的通配符了
    shopt -s extglob
    rm -rf !(bbs)
    rm -rf !(bbs|cc)
    删除当前目录下除bbs目录外所有的文件,真方便
    也可以用下面的这条命令
    ls |grep -v bbs |xargs rm -f


    [root@cs-manage sh]# ffile=/dir1/dir2/dir3/my.file.txt
    [root@cs-manage sh]# echo ${ffile/dir/}
    /1/dir2/dir3/my.file.txt
    [root@cs-manage sh]# echo ${ffile//dir/}
    /1/2/3/my.file.txt
    [root@cs-manage sh]# echo ${ffile/#/oi}
    oi/dir1/dir2/dir3/my.file.txt
    [root@cs-manage sh]# echo ${ffile/%/oi}
    /dir1/dir2/dir3/my.file.txtoi
    [root@cs-manage sh]# echo ${ffile/%t/oi}
    /dir1/dir2/dir3/my.file.txoi

    Bash的=~正则表达式匹配
    http://zengrong.net/post/1563.htm

    简洁的bash编程技巧
    http://www.open-open.com/lib/view/open1352264103719.html

  • 相关阅读:
    论分治与归并思想
    关于缩短cin时间的方法
    【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)模版】
    getDomain(url)-我的JavaScript函数库-mazey.js
    jQuery-PHP跨域请求数据
    ASP-Server.Transfer-Response.Redirect
    jQuery获取相邻标签的值
    分界线<hr/>
    jQuery获取input复选框的值
    Bootstrap支持的JavaScript插件
  • 原文地址:https://www.cnblogs.com/createyuan/p/4269383.html
Copyright © 2020-2023  润新知