将wget输出(流式shell脚本)传递给bash,但带有额外的参数

将wget输出(流式shell脚本)传递给bash,但带有额外的参数,bash,wget,Bash,Wget,我想通过wget下载特定文件,将其作为bash脚本传递,并一次性提供参数 在我的例子中,脚本存储在:https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh 我试过: wget -O - https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh | bash 但它的结尾是: Error: you ne

我想通过
wget
下载特定文件,将其作为
bash
脚本传递,并一次性提供参数

在我的例子中,脚本存储在:
https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh

我试过:

wget -O - https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh | bash
但它的结尾是:

Error: you need to provide a host and port to test.
Usage:
    bash host:port [-s] [-t timeout] [-- command args]
    -h HOST | --host=HOST       Host or IP under test
    -p PORT | --port=PORT       TCP port under test
                            Alternatively, you specify the host and port as host:port
    -s | --strict               Only execute subcommand if the test succeeds
    -q | --quiet                Don't output any status messages
    -t TIMEOUT | --timeout=TIMEOUT
                            Timeout in seconds, zero for no timeout
    -- COMMAND ARGS             Execute command with args after the test finishes
因为我还需要将参数传递给这个
bash
脚本(在我的特定情况下要检查的主机名和端口),也就是说,我需要运行如下操作:

wait-for-it.sh localhost:8181
更新: 我希望解决方案不带本地保存(=>管道到
bash
请仅限)

对于非递归脚本,它很简单:

 # pipe source code to `bash`, run code with args *foo* and *bar*
 <stream with source code> | bash -s - foo bar 
此问题的用法:

f='https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh'
wget -O - "$f" | strm2fnct ${f##*/} | bash -s - 'localhost:8181'
输出:

--2017-05-21 21:21:49--  https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.36.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.36.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4070 (4.0K) [text/plain]
Saving to: ‘STDOUT’

-   100%[===========================================>]   3.97K  --.-KB/s    in 0.1s    

2017-05-21 21:21:50 (29.8 KB/s) - written to stdout [4070/4070]

wait-for-it.sh: waiting 15 seconds for localhost:8181
wait-for-it.sh: timeout occurred after waiting 15 seconds for localhost:8181
self6196() {
echo hello world
} ; export -f self6196; self6196 "$@"

方法。

尽管
bash
不保存流数据,但它会记住函数。因此
strm2fnct

  • 将整个流(注释和所有内容)包装在一个临时文件中 壳函数;例如:

    strm2fnct <<< "echo hello world"
    
  • 此特殊函数默认情况下会获得一个准随机名称(实际上是字符串“self”后跟PID),或者可以传递一个名称,例如
    strm2fnct foobar
    命名特殊函数
    foobar()

  • 用特殊名称替换
    $0
    的每个实例,但
  • 中的这些
    timeout
    命令等待它。sh
    需要进一步 编辑:

    …因为
    超时
    看不到shell 函数,因此需要导出即席函数,并由
    bash-c
    调用,并且需要引用其参数。通过运行以下命令查看哪些更改:

    diff wait-for-it.sh <( strm2fnct wait-for-it.sh < wait-for-it.sh )
    

    diff wait-it.sh
    wgethttps://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh &&bash wait-for-it.sh localhost:8181
    @Leon OK,下载并执行works,但我更喜欢不使用本地savewell,close,但返回:
    bash:--child:无效选项用法:bash[GNU long option][option]。。。bash[GNU long option][option]脚本文件。。。GNU长选项:-调试--调试器--转储po字符串--转储字符串--帮助--初始化文件--登录--noediting--noprofile--norc--posix--受保护--rcfile--rpm要求--受限--详细--版本Shell选项:-irsD或-c命令或-O shopt_选项(仅限调用)-abefhkmnptuvxBCHP或-o选项bash:localhost:8181等待15秒后发生超时
    @PeterButkovic,请参阅修订后的答案以了解错误。
    grep -n '^ *timeout' wait-for-it.sh 
    56:        timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    58:        timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    
    diff wait-for-it.sh <( strm2fnct wait-for-it.sh < wait-for-it.sh )