From 8960b9a69c99277da039ef4af89e3cd50b327483 Mon Sep 17 00:00:00 2001 From: Sam Al-Sapti Date: Sat, 6 May 2023 00:58:28 +0200 Subject: [PATCH] Use for + eval --- src/gen_data.sh | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/gen_data.sh b/src/gen_data.sh index 7e4a596..b9ad9f9 100755 --- a/src/gen_data.sh +++ b/src/gen_data.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +set -eo pipefail + patient_pref() { printf '%d %d' "-100" "$1" } @@ -10,22 +12,22 @@ therapist_pref() { secret_share() { local secret="$1" - share_0="$(( RANDOM % (secret / 2 + 1) ))" - share_1="$(( RANDOM % (secret / 2 + 1) ))" - share_2="$(( secret - (share_0 + share_1) ))" + export share_0="$(( RANDOM % (secret / 2 + 1) ))" + export share_1="$(( RANDOM % (secret / 2 + 1) ))" + export share_2="$(( secret - (share_0 + share_1) ))" } -max_index=5 +max_case=5 iter="$1" for _ in $(seq "$iter"); do - rand="$(( (RANDOM % max_index) + 1 ))" + rand="$(( (RANDOM % max_case) + 1 ))" secret_share "$rand" - p_data_s+=( "$rand" ) - p_data_0+=( "$(patient_pref $share_0)" ) - p_data_1+=( "$(patient_pref $share_1)" ) - p_data_2+=( "$(patient_pref $share_2)" ) + + for j in $(seq 0 2); do + eval 'p_data_'$j'+=( $(patient_pref "$share_'$j'") )' + done done t_data_s=( $(shuf -e "${p_data_s[@]}") ) @@ -33,9 +35,9 @@ t_data_s=( $(shuf -e "${p_data_s[@]}") ) for i in $(seq 0 "$(( iter - 1 ))"); do secret_share "${t_data_s[$i]}" - t_data_0+=( "$(therapist_pref $share_0)" ) - t_data_1+=( "$(therapist_pref $share_1)" ) - t_data_2+=( "$(therapist_pref $share_2)" ) + for j in $(seq 0 2); do + eval 't_data_'$j'+=( $(therapist_pref "$share_'$j'") )' + done done for i in $(seq 0 2); do