1

There are two problems. One of them I expect 5 elements, But array store 15 elements as space.

Here is my code,

./example.sh

pick_random_data()
{
    # seed random generator
    RANDOM=$$$(date +%s)

    #take array as parameter
    declare -a argArr=("${!1}")

    # pick a random entry from the domain list to check against
    randomResult=${argArr[$RANDOM % ${#argArr[@]}]}
    echo "$randomResult"
}



request_url[0]="POST /playready_license HTTP/1.0"
request_url[1]="POST /fairplay_license HTTP/1.0"
request_url[2]="POST /fairplay_license HTTP/1.1"
request_url[3]="POST /widevine_license HTTP/1.1"
request_url[4]="POST /playready_license HTTP/1.1"


counter=1
while [ "$counter" -le 3 ] 
do
    ran_req_url=$(pick_random_data request_url[@])
    printf "{\"request_url\":\"%s\"}\n" $ran_req_url
    ((counter++))
done

bash example.sh

{"request_url":"POST"}
{"request_url":"/fairplay_license"}
{"request_url":"HTTP/1.1"}
{"request_url":"POST"}
{"request_url":"/fairplay_license"}
{"request_url":"HTTP/1.1"}
{"request_url":"POST"}
{"request_url":"/fairplay_license"}
{"request_url":"HTTP/1.1"}

But I wanna print like this

{"request_url":"POST /playready_license HTTP/1.0"}
{"request_url":"POST /playready_license HTTP/1.0"}
{"request_url":"POST /playready_license HTTP/1.0"}

And another problem is pick_random_data() function doesn't work.

And I tried another array form like

local request_url=(
    POST\ /playready_license\ HTTP/1.0
    POST\ /fairplay_license\ HTTP/1.0
    POST\ /fairplay_license\ HTTP/1.1
    POST\ /widevine_license\ HTTP/1.1
    POST\ /playready_license\ HTTP/1.1
    )

and this

local request_url=(
    "POST /playready_license HTTP/1.0"
    "POST /fairplay_license HTTP/1.0"
    "POST /fairplay_license HTTP/1.1"
    "POST /widevine_license HTTP/1.1"
    "POST /playready_license HTTP/1.1"
    )

These are still not working.

I refer random function in here.

https://www.christianroessler.net/tech/2015/bash-array-random-element.html

3
  • Don't seed the each time you're asking for a random number! you completely lose the point of a PRNG! remove the line RANDOM=$$$(date +%s), you don't really need it. Commented Jan 18, 2018 at 8:54
  • Re. line breaks, USE MORE QUOTES: printf "{\"request_url\":\"%s\"}\n" "$ran_req_url" (see the quotes for the expansion of $ran_req_url?) Commented Jan 18, 2018 at 8:59
  • In ran_req_url=$(pick_random_data request_url[@]), remove the [@]... Commented Jan 18, 2018 at 9:00

2 Answers 2

1
#!/bin/bash

pick_random_data()
{
    #take array as parameter
    declare -a argArr=("${!1}")

    # pick a random entry from the domain list to check against
    randomResult=${argArr[$RANDOM % ${#argArr[@]}]}
    echo "$randomResult"
}



request_url[0]="POST /playready_license HTTP/1.0"
request_url[1]="POST /fairplay_license HTTP/1.0"
request_url[2]="POST /fairplay_license HTTP/1.1"
request_url[3]="POST /widevine_license HTTP/1.1"
request_url[4]="POST /playready_license HTTP/1.1"


counter=1
while [ "$counter" -le 3 ] 
do
    ran_req_url=$(pick_random_data request_url[@])
    printf "{\"request_url\":\"$ran_req_url\"}\n"
    ((counter++))
done

First, you must place $ran_req_url inside of printf. Second, use mRANDOM=$RANDOM$(date +%s) instead of RANDOM=$$$(date +%s) or just $RANDOM. It is global variable and it's already randomly.

In your solution, script execute too fast and you have the same value of your $RANDOM variable every time: just add echo "It is my not randomly RANDOM: $RANDOM" right after your RANDOM=$$$(date +%s) and you will see.

Sign up to request clarification or add additional context in comments.

4 Comments

Cool, random function working! But print format a little weird in my bash, like this.{"request_url":""} POST /playready_license HTTP/1.1
@YEEN y, edited my solution. Take attention, I've removed random generator. Just use global $RANDOM variable. date +%s return the same result in each iteration of the loop, so your $RANDOM % ${#argArr[@]} operation were returning the same results more times than you need. Just $RANDOM is more randomly in your case.
You mean that I gonna use randomResult=${argArr[$RANDOM]} this form?
@YEEN use your first solution randomResult=${argArr[$RANDOM % ${#argArr[@]}]}. But don't assign any value for RANDOM variable earlier. (Check, I've removed RANDOM=$$$(date +%s) your string from my code.
0

The error may be on the line

ran_req_url=$(pick_random_data request_url[@])

$() construct is command substitution; to access array elements, use ${} construct. Another point is that @ is giving all elements, not only one.

An other point is the printf construct, prefer simpler string concatenation.

Third point is that $counter should begin to 0 if you want to access to first element, in bash array indexing is 0 based.

As a result, this should work better:

ran_req_url="${pick_random_data request_url[$counter]}"
echo "{\"request_url\":\"$ran_req_url\"}"

1 Comment

Sorry, not working on my bash...But thank you for knowing that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.