V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
workwonder
V2EX  ›  Bash

这是什么 shell 语法?

  •  
  •   workwonder · 2017-06-21 15:37:03 +08:00 · 1799 次点击
    这是一个创建于 2473 天前的主题,其中的信息可能已经有所发展或是发生改变。

    针对某些程序(比如 proxychains )的文件参数仅支持传入文件名(-f config_file),也没有读标准输入的方式,我想实现直接在命令行写 inline 的配置文件,应该怎么搞呢。根据我的经验,胡乱试了下,写出了这样的语法:

    $ echo <(<<<"hello world")
    /proc/self/fd/11
    
    $ ls -l <(<<<"hello world")
    lr-x------ 1 wonder wonder 64 6 月  21 15:23 /proc/self/fd/11 -> pipe:[71926512]
    
    $ cat <(<<<"hello world") 
    hello world
    

    我的实际目标是通过命令行动态设置 proxychains 所用的代理,而不用写多个配置文件,由于上面那种搞法似乎不能断行,我最终这么写的:

    proxychains4 -f <(/bin/echo -e "[ProxyList]\nsocks5 127.0.0.1 1082") ssh ubuntu@my-cloud-host
    

    不知道这种语法怎么称呼,哪里有参考呢?

    11 条回复    2017-06-22 15:13:55 +08:00
    Jaylee
        1
    Jaylee  
       2017-06-21 15:42:26 +08:00
    不就一个重定向么?
    workwonder
        2
    workwonder  
    OP
       2017-06-21 15:50:58 +08:00
    @Jaylee 不是传统那种,还搞了临时的匿名文件。
    introom
        3
    introom  
       2017-06-21 15:51:29 +08:00 via Android
    @Jaylee 不是,,,,
    introom
        4
    introom  
       2017-06-21 15:53:34 +08:00 via Android
    process substitution 还是什么,我也忘了具体名字。bash 的 manpage 一共才 4000 多行,在 expansion 那块找找,很快就能找到。
    jkeylu
        5
    jkeylu  
       2017-06-21 15:55:37 +08:00
    workwonder
        6
    workwonder  
    OP
       2017-06-21 16:06:47 +08:00
    我需要根据访问的目标环境使用不同的代理,使用 proxychains 非要搞一个配置文件,好烦。根据上面的写法,我写了一个小脚本,没那么痛苦了: https://gist.github.com/wonderbeyond/6ef3cdc191490e02a6b12162deab4fd7
    workwonder
        7
    workwonder  
    OP
       2017-06-21 16:10:32 +08:00
    @introom 以前知道三个小于号 `<<<` 是把 HERE 文档变成前面命令的标准输入,原来还可以跟 `<()` 结合在一起,文档里面都没有这样的用例。
    araraloren
        8
    araraloren  
       2017-06-21 16:13:13 +08:00
    mark
    之前遇到过,把这东西忘了~~
    Jaylee
        9
    Jaylee  
       2017-06-21 20:36:32 +08:00
    @workwonder 涨姿势了 居然还有这种操作
    mononite
        10
    mononite  
       2017-06-22 09:54:29 +08:00
    <() process substitution
    <<< here string
    quinoa42
        11
    quinoa42  
       2017-06-22 15:13:55 +08:00   ❤️ 1
    查了一下,herestring (或者 heredoc 以及其他 redirection )和 process substitution 直接结合在一起的用法实际上是 zsh 支持的功能(至少 bash 4.2.46 是不支持的):
    http://zsh.sourceforge.net/Doc/Release/Redirection.html#Redirections-with-no-command
    > ...if the parameter NULLCMD is set, its value will be used as a command with the given redirections. ...The default for NULLCMD is ‘ cat ’...
    所以 `<<<"hello"` 默认等价于 `cat <<< "hello"`,因此 `cat <(<<<"hello")` 会输出 `hello`。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2834 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:53 · PVG 22:53 · LAX 07:53 · JFK 10:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.