YEEN
YEEN

Reputation: 87

Ignore spaces when use array as function parameter in bash script

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

Upvotes: 1

Views: 69

Answers (2)

sahaquiel
sahaquiel

Reputation: 1838

#!/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.

Upvotes: 1

Bentoy13
Bentoy13

Reputation: 4966

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\"}"

Upvotes: 0

Related Questions