[英]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. 当我将这两个参数放在main函数下时,而不是在终端上输入它们。 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. 在main函数中创建的变量data_path
和new_destination
是全局的,这就是为什么您能够在traverse
函数中读取它们的原因。 To fix that I added the declare
keyword before them in order to be local to the main function. 为了解决这个问题,我在它们之前添加了declare
关键字,以便位于主要功能的本地。 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. 另外,您会将这两个变量作为参数传递给traverse
函数,该traverse
函数不会从代码中读取任何这些参数。 To fix that, I replaced the variables names by $1 and $2 to read them as params. 为了解决这个问题,我用$ 1和$ 2替换了变量名,将它们读作参数。
Edit: Found more variables that need to be local. 编辑:发现更多需要本地变量。 ( temp_path
and new_path
) ( temp_path
和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.