1

in a bash script i do run this:

seq 1 $(<howmany.txt) | xargs -I% -n1 -P$(<parallel.txt) mycommand --infile file% --option A --outfile file%.new

it works.

the values in howmany.txt and parallel.txt comes from other scripts into these files.

in this example here is the value 48 in howmany.txt and the value 4 in parallel.txt.

howmany.txt is ever in a range from not less than 4 and the maximum not over 99.

parallel.txt is ever in a range from not less than 4 and the maximum not over 16.

how to add a new feature: --option N ?

in options.txt are the values: A, B and C :

in line 1 is the value: `A`
in line 2 is the value: `B`
in line 3 is the value: `C`

how can it randomize to do in 50% to use --option A, 25% to use --option B and the rest (25%) to use --option C ?

in some cases where it can not clear divide, there must be round up or down, that is no problem. is it possible to set it up in such a way that an option D could be added later?

the best way could be if it possible to options.txt to write

50 A
25 B
rest C

or

40 A
20 B
15 C
rest D

edit:

with quotes ", it does not work, dont know why ... but it works here for me only without quates around my constructs.

i try it with --option $(shuf -n1 options.txt)

in options.txt in line 1 is A in line 2 is B and so on....

i try it with sort -R | tail -n1 instead of shuf -n1, and xargs with -P1 too. nothing brings me "random options" with xargs. dont know what is wrong here, but if i test in a terminal sort -R | tail -n1 and or shuf -n1 with options.txt it brings me everytime random output, that works, but it does not work combined with xargs? what do i wrong?!?

1 Answer 1

1

If you can build options.txt to have equal possibilities between the lines, possibly by repeating the letters as needed, then you could write: --option "$( shuf -n1 options.txt )"

(And you probably should have quotes around your other $( ... ) constructs.)

3
  • with quotes ", it does not work, dont know why ... but it works here for me only without quates around my constructs.
    – user447274
    Commented Oct 10, 2024 at 20:47
  • i try it with sort -R | tail -n1 instead of shuf -n1, and xargs with -P1 too. nothing brings me "random options" with xargs. dont know what is wrong here, but if i test it in a terminal sort -R | tail -n1 and or shuf -n1 brings me everytime random output from options.txt, that works, but it does not work combined with xargs? what do i wrong here?! in options.txt in line 1 is A in line 2 is B and so on....
    – user447274
    Commented Oct 10, 2024 at 21:36
  • Frankly, I would expect it to be issues with quoting. Unless: are you hoping for random for each invocation of mycommand? As for what the quoting does: Without quoting the lines are split on whitespace into separate parameters and special characters may apply; With double-quotes ("), the entire output is one parameter, including any interior newlines; with single-quotes ('), nothing gets expanded at all. I find double-quotes to be good defensive programming.
    – David G.
    Commented Oct 11, 2024 at 19:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.