1

I have a command that takes domain names as options. I have a long list of domain names to include and so I'd like to automate the parsing of that list of domains into the command.

For example, the simplest usage would be command --option=example-one.com --output=file.ext. After adding the list of domains the eventual command to execute would look like command --option=example-one.com ... --option=example-99.io --output=file.ext (where '...' would be 97 more options, shown abbreviated here)

Is this possible to do in Bash and if yes, what's it called please?

Here's what I tried that didn't work so far:

  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

The command while read baddomains ; do echo -n '--option=$baddomains ' ; done < domains.txt > all-options.txt does output a list of perfectly formatted options into a new file all-options.txt, but I can't understand how to redirect output of the options back into the command, instead of a text file.

The list of domains includes Cyrillic characters, if that matters.

Tom Brossman
  • 301
  • 4
  • 13

2 Answers2

1

You can build an array with the arguments:

args=()
while read -r domain _; do
    args+=("--option=$domain")
done < domains.txt

command "${args[@]}" --output=file.ext
jordanm
  • 889
  • 5
  • 9
  • Sorry if this is a dumb question, but which part of this example is pulling the list of domains from my external domains.txt file? – Tom Brossman Feb 23 '17 at 15:22
  • 1
    @TomBrossman sorry, I left off the redirection for the while loop. Updated. – jordanm Feb 23 '17 at 15:34
  • This gave me an error `Syntax error: "(" unexpected` but this may be due to an issue with the particular command I'm trying to run and not because of an error in your example. Thanks anyhow, I'll try to adapt this for something else. – Tom Brossman Feb 23 '17 at 17:16
  • @TomBrossman that likely means you are trying to run it in `/bin/sh`, not `/bin/bash`. – jordanm Feb 23 '17 at 17:28
1
command $(printf -- "--option=%s " $(<domains.txt)) --output=file.ext

is the most compact way I know.

What you were trying should have been

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

but it didn’t work for you because you used the wrong quotes in the subshell, and enclosed the whole $() in double quotes, so that command saw the whole as one argument.

The list of domains includes Cyrillic characters, if that matters.

It doesn’t matter to these commands, but, if they are not properly punycoded, how can they be valid domain names? If they are punycoded, the shell will see no cyrillic characters whatsoever. It will happily process its input correctly in both cases.

Dario
  • 841
  • 8
  • 11