AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 834483
Accepted
Tom Brossman
Tom Brossman
Asked: 2017-02-24 06:36:58 +0800 CST2017-02-24 06:36:58 +0800 CST 2017-02-24 06:36:58 +0800 CST

从文本文件生成多个 Bash 选项?

  • 772

我有一个将域名作为选项的命令。我有一长串要包含的域名,因此我想将该域列表自动解析到命令中。

例如,最简单的用法是command --option=example-one.com --output=file.ext. 添加域列表后,最终执行的命令将如下所示command --option=example-one.com ... --option=example-99.io --output=file.ext (其中“...”将是 97 个更多选项,此处显示缩写)

这可以在 Bash 中进行吗?如果可以,请问它叫什么?

这是我尝试过的到目前为止还没有奏效的方法:

  1. command --option=$(cat domains.txt) --output=file.ext
  2. command "$(while read baddomains ; do echo -n '--option=$baddomains ' ; done < domains.txt)" --output=file.ext
  3. command --option="domains.txt" --output=file.ext

该命令while read baddomains ; do echo -n '--option=$baddomains ' ; done < domains.txt > all-options.txt确实将格式完美的选项列表输出到新文件 all-options.txt 中,但我不明白如何将选项的输出重定向回命令,而不是文本文件。

域列表包括西里尔字符,如果这很重要的话。

bash options
  • 2 2 个回答
  • 55 Views

2 个回答

  • Voted
  1. jordanm
    2017-02-24T06:51:48+08:002017-02-24T06:51:48+08:00

    您可以使用参数构建一个数组:

    args=()
    while read -r domain _; do
        args+=("--option=$domain")
    done < domains.txt
    
    command "${args[@]}" --output=file.ext
    
    • 1
  2. Best Answer
    Dario
    2017-02-24T07:42:52+08:002017-02-24T07:42:52+08:00
    command $(printf -- "--option=%s " $(<domains.txt)) --output=file.ext
    

    是我所知道的最紧凑的方式。

    你尝试的应该是

    command $(while read baddomains ; do echo -n "--option=$baddomains " ; done < domains.txt) --output=file.ext
    

    但这对您不起作用,因为您在子shell中使用了错误的引号,并将整个$()引号括在双引号中,因此command将整体视为一个参数。

    域列表包括西里尔字符,如果这很重要的话。

    这些命令无关紧要,但是,如果它们没有正确编码,它们怎么可能是有效的域名?如果它们是 punycoded,shell 将看不到任何西里尔字符。在这两种情况下,它都会愉快地正确处理其输入。

    • 1

相关问题

  • Mac OS X:从 python 脚本中更改 $PATH

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • crontab ifconfig 什么都不输出

  • 使用命令行工具按排序顺序计算重复项

  • 是否有 bash 等效于 ruby​​ 的“一些内容#{foo}”?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve