0

I'd like to apologize if my question had already been asked, but english isn't my native language and I didn't find the answer. I'd like to have a bash script that executes a program I'll call MyProgram, and I want it to run with a fixed number of arguments which consist in random numbers. I'd like to have something like this:

./MyProgram for(i = 0; i < 1000; i++) $(($RANDOM%200-100))

How should I go about this?

3
  • The answers mostly seem to be long-winded variants on ./MyProgram $( for ((i = 0; i < 1000; i++)); do echo $(($RANDOM%200-100)); done ). While only 1000 arguments isn't going to stress things, you will need to be careful if the argument list grows by a couple of orders of magnitude (100000 numbers is likely too big, or close to too big, to fit in the argument list). Commented Sep 29, 2014 at 0:06
  • @JonathanLeffler, the only thing that worries me about that suggestion is that while it's a safe practice exactly as-given (dealing with values which can only ever be numbers), it's not safe in general (ie. if the values generated were filenames). The more long-winded approaches are harder to misuse when (re)applying them to real-world use cases. Commented Sep 29, 2014 at 0:19
  • @CharlesDuffy: Yes, if you have to worry about spaces in filenames, then the array-based approach suggested by chepner in his answer is safe with a little care ensuring that the value added to array is appropriately quoted. Commented Sep 29, 2014 at 1:20

3 Answers 3

2

You (mostly) just have the loop and the actual program call inverted.

for ((i=0; i < 1000; i++)); do
    ./MyProgram $((RANDOM%200 - 100))
done

If, however, you actually want 1000 different arguments passed to a single call, you have to build up a list first.

args=()
for ((i=0; i < 1000; i++)); do
    args+=( $((RANDOM%200 - 100)) )
done
./MyProgram "${args[@]}"
Sign up to request clarification or add additional context in comments.

Comments

0

The

$RANDOM % 200 - 100

is the same as the next perl

perl -E 'say int(200*rand() -100)  for (1..1000)'

e.g. the

perl -E 'say int(200*rand() -100)  for (1..1000)' | xargs -n1 ./MyProgram

will run like:

./MyProgram -10
./MyProgram 13

... 1000 times ...

./MyProgram 55
./MyProgram -31

if you need 1000 args

./MyProgram $(perl -E 'say int(200*rand() -100)  for (1..1000)')

will produce

./MyProgram 5 -41 -81 -79 -14 ... 1000 numbers ... -63 -9 95 -9 -29

Comments

0

In addition to what @chepner says, you can also use the for ... in style of for loop. This looks like:

for a in one two three; do
    echo "${a}"
done

which would produce the result:

one
two
three

In other words, the list of words after the in part, separated by spaces, is looped over, with each iteration of the loop having a different word in the variable a.

To call your program 1000 times (or just modify to produce the list of arguments to run it once as in @chepner's answer) you could then do:

for a in $(seq 1 1000); do
    ./MyProgram $((RANDOM%200 - 100))
done

where the output of the seq command is providing the list of values to loop over. Although the traditional for loop may be more immediately obvious to many programmers, I like for ... in because it can be applied in lots of situations. A crude and mostly pointless ls, for example:

for a in *; do
    echo "${a}"
done

for ... in is probably the bit of "advanced" bash that I find the most useful, and make use of it very frequently.

3 Comments

boo, hiss re: demo'ing the platform-dependent seq rather than built-in (and thus platform-independent) C-style for-loop functionality used by chepner's answer.
...also, echo "$a" is more robust than either echo $a or echo ${a}; both the latter incorrectly files named as, say, ./foo * bar.
Point taken, but it was intended as demonstrating a general principle rather than "this is better than the above". for i in {1..1000}; ... would be a "better" way of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.