shell - 用于处理管道的 xargs 替代方案

标签 shell unix curl

<分区>

我正在尝试运行一个 curl 命令,它需要来自管道的参数

some operation | sort | head -n -2 | curl -s "blah/blah/<argument goes here>/blah"

我不知道如何一次传递一个参数。我发现的一种方法是

some operation | sort | head -n -2 | xargs -l "$" curl -s "blah/blah/$/blah"

但是我明白了

xargs: $: No such file or directory

这是什么意思?为什么它试图将 $ 作为文件读取?

最佳答案

参见 BashFAQ #1逐行读取字符串的最佳实践方法:

#!/bin/bash
#      ^^^^ - not /bin/sh, or <() will be a syntax error

while IFS= read -r line; do
  curl -s "blah/blah/$line/blah"
done < <(some operation | sort | head -n -2)

关于shell - 用于处理管道的 xargs 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37012353/

相关文章:

linux - Bash 加入数组

mysqlcheck 不接受密码

bash - 将带有空格的变量扩展为 curl POST 变量

linux - CURL:bash 脚本中的空文件

linux - 如何让 libcurl 只安装共享库

bash - 用于在 bash printf 中着色的 ANSI 转义码

bash - 无需 curl 或 bash 即可连接到网站

bash - 如何将 MSYS2 shell 集成到 Windows 上的 Visual Studio 代码中?

c - "Ping pong"使用 unix 进程和管道的游戏

linux - Bash IP IF THEN ELSE 语句