简体   繁体   English

使用bash脚本遍历目录时出现分段错误

[英]Segmentation fault when looping through directories with bash script

I understand this is usually to do with recursion, except I am pretty sure I am not doing this. 我知道这通常与递归有关,除非我很确定自己没有这样做。 On each time round the loop it gives a segmentation error pointing to the for loop's line. 在循环的每次循环中,它都会给出指向for循环行的分段错误。

for file in $DATAFOLDER*; do
    echo $file;
    filename=$(basename $file);
    echo $filename;
    echo "$DESTFOLDER$SUBFOLDER${filename%%.*}"
    pwd
    # move data to cppapplication folder
    cp -R "$file" "$DESTFOLDER";
    sleep 1
    # create subdirectory
    mkdir "$SUBFOLDER";
    # create new folder for extracted data
    mkdir "$DESTFOLDER$SUBFOLDER${filename%%.*}";
    # copy MassLynx header file into new folder (get the description info)
    cp "$file/_HEADER.TXT" "$DESTFOLDER$SUBFOLDER${filename%%.*}";
    # run the cppapplication
    # <program> <project file> <MassSpectrum> <Mobilogram> <bins in Da> <extract m/z ranges>
    ./cppapplication.exe "$filename" 0 1 1 0 ;
    # fix last character comma bug
    sed -i '$ s/,$//g' MassMobility.txt
    # move the created files into the new folder
    mv Mobilogram.txt "$DESTFOLDER$SUBFOLDER${filename%%.*}";
    mv MassMobility.txt "$DESTFOLDER$SUBFOLDER${filename%%.*}"; 
    mv MassMobilityXaxis.txt "$DESTFOLDER$SUBFOLDER${filename%%.*}";
    mv MassMobilityYaxis.txt "$DESTFOLDER$SUBFOLDER${filename%%.*}";    
    # get driftscope quicklook image
    sleep 3
    python "C:\Users\ganesh\Dropbox\PhD\03_Amphitrite\CppApplication\quicklook_driftscope.py" "$DESTFOLDERWINDOWS$SUBFOLDERWINDOWS" "${filename%%.*}" "$DESTFOLDERWINDOWS$SUBFOLDERWINDOWS${filename%%.*}";


    # remove MassLynx Project
    rm -r "$filename";

done

Aside from the copying of the file, I don't think the loop has any contact with the original folder being looped through. 除了复制文件之外,我认为循环与循环通过的原始文件夹没有任何联系。

This is the error: 这是错误:

./extract_all_projects.sh: line 26:  7500 Segmentation fault      (core dumped) 

I have no idea what the problem is as "cppapplication.exe" works fine on these files if run outside of the bash script. 我不知道这是什么问题,因为如果在bash脚本之外运行,“ cppapplication.exe”在这些文件上可以正常工作。

Your $filename could be empty or not as expected. 您的$filename可能为空或与预期不符。 I'm sure the segfault is the cppapplication.exe crashing (Why didn't you tell us what line 26 is?) To test this, try 我确定segfault是cppapplication.exe崩溃了(为什么不告诉我们第26行是什么?)要对此进行测试,请尝试

./cppapplication.exe "" 0 1 1 0

and see if it segfaults. 看看它是否存在段错误。 If I had to guess: the app tries to open a file but doesn't check the return value and soldiers on with a NULL pointer. 如果我不得不猜测:该应用程序尝试打开文件,但不检查返回值,并使用NULL指针指示士兵是否继续前进。

BTW, all except the first semicolon in your script are useless. 顺便说一句,脚本中除第一个分号外的所有字符都没有用。 The shell is not C :-) 外壳不是C :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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