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

Pet*_*vic 4 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
Run Code Online (Sandbox Code Playgroud)

但结尾为:

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
Run Code Online (Sandbox Code Playgroud)

因为我还需要向该bash脚本传递参数(主机名端口以检查我的具体情况),也就是说,我需要运行以下命令:

wait-for-it.sh localhost:8181
Run Code Online (Sandbox Code Playgroud)

更新: 我很想拥有无需本地保存的解决方案(=> bash仅管道)

agc*_*agc 5

对于递归的脚本,很简单:

 # pipe source code to `bash`, run code with args *foo* and *bar*
 <stream with source code> | bash -s - foo bar 
Run Code Online (Sandbox Code Playgroud)

但是该脚本wait-for-it.sh包含$0,并且具有一定的递归性(它自己调用),并且使其与流式传输不兼容,因为:

  1. 流中没有文件名,随机访问会起作用,
  2. 无法更改$0流的名称。

一个bash管道函数获取周围的:

strm2fnct(){ 
s=${1:-self$$}
sed "1i $s"'() {
s/\$0/'"$s"'/
/timeout/{s/'"$s"'[^&]*/bash -c "&" /};
$a \} ; export -f '"$s; $s"' "$@"'
}
Run Code Online (Sandbox Code Playgroud)

此Q的用法:

f='https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh'
wget -O - "$f" | strm2fnct ${f##*/} | bash -s - 'localhost:8181'
Run Code Online (Sandbox Code Playgroud)

输出:

--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
Run Code Online (Sandbox Code Playgroud)

方法。

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

  • 将整个流(注释和全部)包装在一个临时的 shell函数中;例:

    strm2fnct <<< "echo hello world"
    
    Run Code Online (Sandbox Code Playgroud)

    输出:

    self6196() {
    echo hello world
    } ; export -f self6196; self6196 "$@"
    
    Run Code Online (Sandbox Code Playgroud)
  • 默认情况下,此ad hoc函数获得一个准随机名称(实际上是字符串“ self ”,后跟PID),也可以传递一个名称,例如 strm2fnct foobarad hoc函数命名foobar()

  • 替代的每个实例$0专案名称,但...
  • 这些timeout命令wait-for-it.sh需要进一步编辑:

    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 &
    
    Run Code Online (Sandbox Code Playgroud)

    ...由于timeout看不到shell函数,因此需要导出ad hoc函数,并由调用bash -c,并且其引数需要加引号。通过运行查看更改内容:

    diff wait-for-it.sh <( strm2fnct wait-for-it.sh < wait-for-it.sh )
    
    Run Code Online (Sandbox Code Playgroud)