作者:青锋寒万岁 | 来源:互联网 | 2023-02-03 13:57
我想通过下载特定的文件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
仅管道)
1> agc..:
对于非递归的脚本,很简单:
# pipe source code to `bash`, run code with args *foo* and *bar*
| bash -s - foo bar
但是该脚本wait-for-it.sh
包含$0
,并且具有一定的递归性(它自己调用),并且使其与流式传输不兼容,因为:
流中没有文件名,随机访问会起作用,
无法更改$0
流的名称。
一个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
:
将整个流(注释和全部)包装在一个临时的
shell函数中;例:
strm2fnct <<<"echo hello world"
输出:
self6196() {
echo hello world
} ; export -f self6196; self6196 "$@"
默认情况下,此ad hoc函数获得一个准随机名称(实际上是字符串“ 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
看不到shell函数,因此需要导出ad hoc函数,并由调用bash -c
,并且其引数需要加引号。通过运行查看更改内容:
diff wait-for-it.sh <( strm2fnct wait-for-it.sh