0

I have a dynamic path in the variable DATASET_CONFIG

This is a small code to demonstrate the problem

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
RUN_SCRIPT="$SCRIPT_DIR/file.py"
DATASET_CONFIG="$SCRIPT_DIR/../dataset_config/ffhq.json"
hps_dataset="--dataset_config $DATASET_CONFIG --dataset_worker_num 16"
python_version="python3"

$python_version "$RUN_SCRIPT" \
$hps_dataset \
;

As you can see I have used "$RUN_SCRIPT" instead of $RUN_SCRIPT because SCRIPT_DIR contain whitespace but I cannot do the same for $hps_dataset

evgeni fotia
  • 4,650
  • 3
  • 16
  • 34
  • 1
    [I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ#BashFAQ.2F050.I.27m_trying_to_put_a_command_in_a_variable.2C_but_the_complex_cases_always_fail.21) – glenn jackman Mar 06 '19 at 14:43

2 Answers2

1

You need to use an array to store the dataset. I'd also recommend you stop using ALLCAPS varnames (here's why):

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
run_script="$script_dir/file.py"
dataset_config="$script_dir/../dataset_config/ffhq.json"
hps_dataset=( --dataset_config "$dataset_config" --dataset_worker_num 16 )
python_version="python3"

"$python_version" "$run_script" "${hps_dataset[@]}"

Use all the quotes shown here.

Because we're using an array, you cannot use /bin/sh to run the script. You'll have to explicitly use bash or ksh (or perhaps zsh)

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

You should use something like this

hps_dataset="--dataset_config \"$DATASET_CONFIG\" --dataset_worker_num 16"

Also for the future variables in bash are all caps like HPS_DATASET.

Aidan H
  • 124
  • 9