1

I have a txt file with a bunch of 1 word strings that will serve as arguments:

input.txt

arg1 
arg2 
arg3
…
argN

I need to pass these arguments in a (zsh) terminal where the command looks like:

$run_program "settings:default argument:{NEED TO PUT ARGUMENT HERE}" 

Notice the quotes need to keep their place.

So I am trying to call the program N times. Is there a way to do this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sej
  • 13
  • 2

3 Answers3

0

Read the lines of your files and store them in an array (see how to do it in zsh there). After that, it is simply a matter of looping through the array and firing the desired command for each element.

# read the file (zsh-only syntax):
args=( "${(@f)$(< file.txt)}" )
# loop through its lines (sh-compatible syntax):
for arg in "${arg[@]}" ; do
  "$run_program" "settings:default argument:$arg"
done

Of course if you do not need the contents of the file for anything else, you could collapse the first two lines into one and avoid naming an array to store the lines.

for arg in "${(@f)$(< file.txt)}" ; do
  "$run_program" "settings:default argument:$arg"
done

Alternatively, you can read the file line by line and process these lines directly. For that, you’d use a canonical pattern which combines the read primitive (used carefully) and a while loop, and which is not specific to zsh.

# sh-compatible syntax:
while IFS= read -r arg || [ -n "$arg" ] ; do
  "$run_program" "settings:default argument:$arg"
done < file.txt
Maëlan
  • 3,586
  • 1
  • 15
  • 35
  • Hi thanks for the answer! I'm relatively new to programming and I apologize for the rather naive question but is this a bash script you are writing? So I would write one of these options you mentioned in a .sh file and then run the file? – Sej Aug 24 '19 at 02:40
  • So I managed to follow some youtube video on bash and found out how to make .sh file, I then tried to run the file with ./run.sh but got this error: line 4: ${(@f)$(< file.txt)}: bad substitution, the file.txt is in the same directory as this .sh file. I tried the first two options but got the same error :( – Sej Aug 24 '19 at 02:51
  • Welcome to SO! I used zsh since it is the language you mentioned in your question. “Bash” and “zsh” are two different [shells](https://en.wikipedia.org/wiki/Unix_shell), sharing a common ancestor called “sh”. Thus they are similar in syntax and features, but not identical. In particular, the cryptic `${(@f)…}` part is specific to zsh, so if you want to use it, you have to call it with zsh, it won’t work with bash (it is an invalid syntax). Alternatively, you may use my third snippet, which is pure sh syntax and thus compatible with sh, bash and zsh. – Maëlan Aug 24 '19 at 05:16
  • To make a script be executed by zsh, give it the file extension `.zsh` (instead of `.sh`) and use a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) like `#!/usr/bin/env zsh` (instead of e.g. `#!/usr/bin/env bash`). – Maëlan Aug 24 '19 at 05:22
  • There's no real reason not to use the `sh`-compatible loop in `zsh` as well. Note that the `|| [ -n "$arg" ]` is a hack to accomodate non-POSIX input files that don't end with a linefeed. (`read` has a non-zero exit code but still populates `arg` with what it could read in that case.) – chepner Aug 24 '19 at 15:57
0
for a in `cat input.txt`; do
    run_program "settings:default argument:{$a}"
done

If there is a 'space' character in your arguments then it will not work.

Attaullah Khan
  • 303
  • 2
  • 8
  • By slightly modifying the way you read the file, you can make this program correct for every possible input (wrong quoting, be it against whitespaces or other “special characters”, is probably the number 1 flaw in code snippets around the web). You can also avoid spawning an unnecessary process `cat`. See my answer. – Maëlan Aug 24 '19 at 20:58
0

Use xargs:

xargs -I {} "$run_program" "settings:default argument:{}" <input.txt

Note that the -I not only establishes the placeholder for the argument ({}), it also ensures that the program will be called once for each line of the input.

user1934428
  • 19,864
  • 7
  • 42
  • 87