美文网首页
shell知识点

shell知识点

作者: 一吻江山 | 来源:发表于2017-05-09 09:01 被阅读14次

    获得shell内置命令的帮助文档

    #使用help获取shell内置命令的帮助文档
    $ help set
    
    #man命令不能获取shell内置命令的帮助文档
    #Mac系统在zsh下没有help命令
    
    # 命令 << 分界符  [从标准输入中读入,直到遇见分界符才停止]
    

    shebang

    The shebang #! is an human readable instance of a magic number consisting of the byte string 0x23 0x21,
    which is used by the exec() family of functions to determine whether the file to be executed is a script or a binary.
    When the shebang is present,
    exec() will run the executable specified after the shebang instead.

    终端设置http/https/socks5代理

    # http/https代理
    ➜  ~ export http_proxy=http://proxyuser:proxypwd@proxy.server.com:8080
    ➜  ~ export https_proxy=https://proxyuser:proxypwd@proxy.server.com:8080
    
    # socks5代理 已打开ShadowsocksX-NG,这个软件socks5的监听地址为127.0.0.1:1086
    ➜  ~ export ALL_PROXY=socks5://127.0.0.1:1086 
    
    # 或不用打开ShadowsocksX-NG,只要找一个代理就可以
    ➜  ~ export ALL_PROXY=socks5://45.55.66.92:4725
    
    # 验证
    ➜  ~ curl ip.gs
    Current IP / 当前 IP: 154.85.35.104
    ISP / 运营商:  cloudinnovation.org
    City / 城市: Los Angeles California
    Country / 国家: United States
    

    这样配置完成后,所有命令行中的 HTTP 和 HTTPS 请求都会自动通过代理来访问了
    使用 http_proxy 和 https_proxy 只对 HTTP 和 HTTPS 请求有效,
    所以当你 ping www.google.com 的时候如果 ping 不通的话,也就没什么大惊小怪的了
    取消代理设置

    ➜  ~ unset http_proxy
    ➜  ~ unset https_proxy
    

    (cd $DIR; command): 进入到另一个目录执行一个命令,然后返回到旧的目录

    # (cd $DIR; command): 进入到另一个目录执行一个命令,然后返回到旧的目录
    
    # 建立测试命令效果的环境
    ➜  ~ mkdir test123
    
    # 测试命令效果
    ➜  ~ echo $PWD
    /Users/zhoujie
    ➜  ~ (cd test123; echo $PWD)
    /Users/zhoujie/test123
    ➜  ~ echo $PWD
    /Users/zhoujie
    
    

    动态作用域

    JavaScript深入之词法作用域和动态作用域

    临时修改变量值

    Placing an assignment in front of a command causes it to be local to that command and does not change its value elsewhere in the script

    比如:
    ➜ /tmp IFS=: read v1 v2 v3 v4 < test.txt

    ➜  echo 'a:b:c:d' > test.txt
    ➜  cat test.txt
    a:b:c:d
    ➜  IFS=: read v1 v2 v3 v4 < test.txt
    ➜  echo "$v1" "$v2" "$v3" "$v4"
    a b c d
    ➜  echo $IFS
    
    
    ➜  /tmp
    

    调试脚本

    bash -x script.sh
    

    tilde-prefix

    If the tilde-prefix is ‘~+’, the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is ‘~-’, the value of the shell variable OLDPWD, if it is set, is substituted.

    ➜  ~ echo ~-
    /Users/zhoujie
    

    Functions

    A function definition may be deleted using the -f option to the unset builtin

    the exit status of a function is the exit status of the last command executed in the body

    Variables local to the function may be declared with the local builtin. These variables are visible only to the function and the commands it invokes

    Function names and definitions may be listed with the -f option to the declare (typeset) builtin command

    bash-4.4$ declare -f
    zj_sa ()
    {
        pre=:;
        post=:;
        printf "$pre%s$post\n" "$@"
    }
    bash-4.4$
    

    Quoting

    Single Quotes:Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes
    Double Quotes:Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’,

    Process Substitution

    Process Substitution creates a temporary filename for a command or list of commands. You can use it anywhere is expected. The form <(command) makes the output of command available as a file name; >(command) is a file name that can be written to.

    $ sa <(ls -l) >(pr -Tn)
    :/dev/fd/63:
    :/dev/fd/62:
    

    When the filename on the command line is read, it produces the output of the command. Process Substitution can be used in place of a pipeline, allowing variables defined within a loop to be visible to the rest of the script.

    相关文章

      网友评论

          本文标题:shell知识点

          本文链接:https://www.haomeiwen.com/subject/ehlbtxtx.html