简体   繁体   中英

why can't this bash script run recursively?

I am going to recursively run through the base directory of data, and then modified each file in it, and create a new file on the another base directory. So I need two arguments, one is the path for original data base directory, the other is for the base directory I put new file in. But something wrong with my code. When I put these two argument under main function, rather than inputting them on the terminal. Wish someone can help me out.

the following is my code:


function traverse() {   
    for file in $(ls "${data_path}")
    do
        echo "in file: ${data_path}/${file}"
        if [[ ! -d ${data_path}/${file} ]]; then

            if [[ ${data_path}/${file} == *.nii.gz ]];then

                echo "is nifti: ${data_path}/${file} "

            else
        echo "not file"
        echo ${data_path}

        temp_path=${data_path/'/data2/Projects/Incoming_monkey'/}
        new_path="${new_destination}/${temp_path}"
        mkdir -p ${new_path}
        echo ${new_path}
        fi
        else
            echo "entering recursion with: ${data_path}/${file}"
            traverse "${data_path}/${file}" "${new_destination}"
        fi
    done
}
function main() {

   echo "main start"

   data_path=/data2/Projects/Incoming_monkey/MAJOM/08_20170706/func
   new_destination=/data2/Projects/reorientation

   traverse "${data_path}" "${new_destination}"
}

main

I haven't tried to reason the logic behind the code, but I can see some obvious errors. The variables data_path and new_destination created in the main function are global meaning that this is why you are able to read them in the traverse function. To fix that I added the declare keyword before them in order to be local to the main function. Also you are passing those two variables as parameters to the traverse function, which from the code it does not read any of those arguments. To fix that, I replaced the variables names by $1 and $2 to read them as params.

Edit: Found more variables that need to be local. ( temp_path and new_path )

#!/bin/bash

function traverse() {   
    for file in $(ls "${1}") # Replace data_path with 1
    do
        echo "in file: ${1}/${file}"
        if [[ ! -d ${1}/${file} ]]; then

            if [[ ${1}/${file} == *.nii.gz ]];then

                echo "is nifti: ${1}/${file} "

            else
        echo "not file"
        echo ${1}

        declare temp_path=${1/'/data2/Projects/Incoming_monkey'/}
        declare new_path="${2}/${temp_path}"   # Replaced new_destination by 2
        mkdir -p ${new_path}
        echo ${new_path}
        fi
        else
            echo "entering recursion with: ${1}/${file}"
            traverse "${1}/${file}" "${2}"
        fi
    done
}
function main() {

   echo "main start"

   declare data_path=/data2/Projects/Incoming_monkey/MAJOM/08_20170706/func
   declare new_destination=/data2/Projects/reorientation

   traverse "${data_path}" "${new_destination}"
}

main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM