root_dir=/output_folder_contains_subdirs
subdirs=(`ls $root_dir`)
CUDA_DEVICES=(0 1 2 3)
SCRIPT=./corrupt.py
# CUDA_VISIBLE_DEVICES=$device python $SCRIPT --input_dir $root_dir --output_json $root_dir_corrupt.json
CUR_DEVICE=0
for subdir in ${subdirs[@]}; do
    #check if directory exists
    if [ ! -d $root_dir/$subdir ]; then
        continue
    fi
    # skip __pycache__
    if [ $subdir == "__pycache__" ]; then
        continue
    fi
    root_dir_corrupt=$root_dir/$subdir/corrupt
    device=${CUDA_DEVICES[$CUR_DEVICE]}
    echo "CUDA_VISIBLE_DEVICES=$device python3.11 $SCRIPT --input_dir $root_dir/$subdir --output_json $root_dir_corrupt.json"
    CUDA_VISIBLE_DEVICES=$device python3.11 $SCRIPT --input_dir $root_dir/$subdir --output_json $root_dir_corrupt.json &
    CUR_DEVICE=$((CUR_DEVICE+1))
    if [ $CUR_DEVICE -eq 4 ]; then
        CUR_DEVICE=0
    fi
done
wait
