这是我目前正在运行的命令:
curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")'
此命令的响应是一个URL,如下所示:
$ curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")'
http://google.com
我想使用任何URL来实际执行此操作:
curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")' | curl 'http://google.com'
有没有简单的方法可以在一行中完成这一切?
答案 0 :(得分:0)
将xargs
与占位符一起使用,stdin
的输出带有-I{}
标记,如下所示。 -r
标志用于确保不会在先前curl
输出的空输出上调用grep
命令。
curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")' | xargs -r -I{} curl {}
-I
页面中有关标记-r
和GNU xargs man
的小描述,
-I replace-str
Replace occurrences of replace-str in the initial-arguments with
names read from standard input.
-r, --no-run-if-empty
If the standard input does not contain any nonblanks, do not run
the command. Normally, the command is run once even if there is
no input. This option is a GNU extension
(或)如果您正在寻找没有其他工具的bash
方法,
curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")' | while read line; do [ ! -z "$line" ] && curl "$line"; done