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/

相关文章:

php - cURL错误如何解决(七): couldn't connect to host?

bash - 从函数 : Why does "declare -A foo=$(bar)" work but not "declare -A foo; foo=$(bar)"? 的输出创建 Bash 关联数组

linux - 如何显示自系统上次在 linux 中重新启动以来运行次数最多的程序

c - 在链表上插入一个新节点会创建一个新节点

bash - 连接时变量会被覆盖

php - 在 PHP 中使用 fopen($url) 和 curl 有什么重要区别?

c - 在 C 语言的基本 Shell 程序中,如何退出与父进程关联的所有进程?

linux - 在 shell 脚本中使用 strace 监控所有正在运行的进程

linux - 如何在 grep 中排除某些单词?

curl - 如何使用 curl、wget 从 owncloud 下载文件