Pass wget output (streaming shell script) to bash, but with extra arguments

I want to load a specific file through wget, pass it as a bashscript, and in one shot also provide arguments for it.

In my case, the script is stored at: https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh

I tried:

wget -O - https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh | bash

but it ends with:

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

since I also need to pass arguments to this bashscript (host name and port to check my specific case), namely: I need to run something like:

wait-for-it.sh localhost:8181

UPDATE: I would like the solution to be without local storage (=> pipe to bashonly please)

+4
source share
1 answer

script, , :

 # pipe source code to `bash`, run code with args *foo* and *bar*
 <stream with source code> | bash -s - foo bar 

script wait-for-it.sh $0 ( ) , :

  • , ,
  • $0 .

A bash :

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

Q:

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

.

bash , . strm2fnct:

  • ( ) ad hoc ; :

    strm2fnct <<< "echo hello world"
    

    :

    self6196() {
    echo hello world
    } ; export -f self6196; self6196 "$@"
    
  • ( "self", PID), , . strm2fnct foobar ad 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 &
    

    ... timeout , bash -c, . , :

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

Source: https://habr.com/ru/post/1677590/


All Articles