我將遞歸運行數據的基本目錄,然後修改其中的每個文件,並在另一個基本目錄上創建一個新文件。所以我需要兩個參數,一個是原始數據庫目錄的路徑,另一個是用於放入新文件的基礎目錄,但是我的代碼有問題。當我將這兩個參數放在主函數下時,而不是在終端上輸入它們。希望有人能幫助我。爲什麼這個bash腳本不能遞歸運行?
以下是我的代碼:
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
用四個空格前綴代碼/數據。請看[編輯幫助](http://stackoverflow.com/editing-help)。 – Cyrus
全局變量有種挫敗遞歸的思想。 –