0

I have a bash shell script as follows.

#!/bin/bash
MKDIR(){
    if [ ! -d $1 ]; then
        mkdir -p $1
    fi
}
WORKER_CNT=4
dirs=/home/sysadmin/Nyan/cv_samples_v1.3.0/action_recognition_net/data/selfinf
declare -a RGB_PATH_LIST
i=0
for entry in "$dirs"/*
do
  RGB_PATH_LIST[i++]="$entry"
done

#for((i=1;i<=${#RGB_PATH_LIST[@]};i++))
#do
#    echo "${RGB_PATH_LIST[i]}"
#done

RUN_WORKERS(){
    echo "$RGB_PATH_LIST"
    for((i=0;i<$RGB_PATH_LIST;i++)); do
        if [ ${RGB_PATH_LIST[i]} != "NULL" ]; then
            mkdir -p ${RGB_PATH_LIST[i]}/"flow"
            mkdir -p ${RGB_PATH_LIST[i]}/"of"
            ./AppOFCuda --input=${RGB_PATH_LIST[i]}/"rgb"/"*.png" --output=${RGB_PATH_LIST[i]}/"flow" --preset=fast --gridSize=1 \
            &&
            python ./convert_of.py --input_flow_folder ${RGB_PATH_LIST[i]}/"flow" --output_folder ${RGB_PATH_LIST[i]}/"of" &
        fi
    done
    wait
     for((i=0;i<$WORKER_CNT;i++)); do
         if [ ${RGB_PATH_LIST[i]} != "NULL" ]; then
             rm -r ${RGB_PATH_LIST[i]}/"flow"
         fi
     done
}

But it doesn't create any flow and of subfolders inside RGB_PATH_LIST folders. How can I solve?

batuman
  • 7,066
  • 26
  • 107
  • 229
  • If `mkdir` does not print an error message, I guess the directories are created at a different place than you think. Do a `ls -ld ${RGB_PATH_LIST[i]}/*` after the mkdir and check.... – user1934428 Aug 15 '23 at 07:29
  • 2
    I don't see anything that actually runs either function. Also, in `for((i=0;i<$RGB_PATH_LIST;i++))` you probably want `${RGB_PATH_LIST[@]}` (the number of elements in the array, not the first element), and I don't see how any element could ever be "NULL". [shellcheck.net](https://www.shellcheck.net) will point out a bunch of other problems/bad practices, especially bad quoting (e.g. use `"${RGB_PATH_LIST[i]}/flow"` instead of `${RGB_PATH_LIST[i]}/"flow"`). – Gordon Davisson Aug 15 '23 at 07:41
  • 1
    `mkdir -p $1` should be replaced by `mkdir -p "$1"` (with quotes) to accept directory names with special characters. – Wiimm Aug 15 '23 at 12:36
  • 2
    shorter version of `if [ ! -d $1 ]; then mkdir -p "$1"; fi` is `[ -d "$1" ] || mkdir -p "$1"`. But `mkdir -p` includes the condition, a simple `mkdir -p "$1"` does the same. – Wiimm Aug 15 '23 at 12:39
  • 2
    I really wish the SO folks would make the [tag:bash] tag instruction mandatory - "For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here.". See also [correct-bash-and-shell-script-variable-capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) – Ed Morton Aug 15 '23 at 20:25

1 Answers1

1

the for loop is not iterating correctly over the RGB_PATH_LIST array and you must to edit the line for((i=0;i<$RGB_PATH_LIST;i++)); do to for((i=0;i<${#RGB_PATH_LIST[@]};i++)); do

check it out :

#!/bin/bash
MKDIR(){
    if [ ! -d $1 ]; then
        mkdir -p $1
    fi
}
WORKER_CNT=4
dirs=/home/sysadmin/Nyan/cv_samples_v1.3.0/action_recognition_net/data/selfinf
declare -a RGB_PATH_LIST
i=0
for entry in "$dirs"/*
do
  RGB_PATH_LIST[i++]="$entry"
done

#for((i=1;i<=${#RGB_PATH_LIST[@]};i++))
#do
#    echo "${RGB_PATH_LIST[i]}"
#done

RUN_WORKERS(){
    echo "$RGB_PATH_LIST"
    for((i=0;i<${#RGB_PATH_LIST[@]};i++)); do
        if [ ${RGB_PATH_LIST[i]} != "NULL" ]; then
            mkdir -p ${RGB_PATH_LIST[i]}/"flow"
            mkdir -p ${RGB_PATH_LIST[i]}/"of"
            ./AppOFCuda --input=${RGB_PATH_LIST[i]}/"rgb"/"*.png" --output=${RGB_PATH_LIST[i]}/"flow" --preset=fast --gridSize=1 \
            &&
            python ./convert_of.py --input_flow_folder ${RGB_PATH_LIST[i]}/"flow" --output_folder ${RGB_PATH_LIST[i]}/"of" &
        fi
    done
    wait
     for((i=0;i<$WORKER_CNT;i++)); do
         if [ ${RGB_PATH_LIST[i]} != "NULL" ]; then
             rm -r ${RGB_PATH_LIST[i]}/"flow"
         fi
     done
}

RUN_WORKERS

I don't have access right now to check, but it seems to me that this method should solve the problem. Please let me know the result. thnx.

Freeman
  • 9,464
  • 7
  • 35
  • 58