I have a code in C and it compiles . I used the compiled executive (eg., a.o) as a command and use it in a bash script. But there is an segmentation error when I run the script sometimes. I tried to find out why.
0) This is my bash script. It use the a.o and input binary data, and output the text file. I wrote this code in order to process a lot of binary data files automatically and save the results in different text files.
#!/bin/bash
function usage {
echo 'usage: ./readIAP0_all [OPTIONS] [inputpath] [outputpath]'
echo 'This script read all the binary N0 data into text data'
echo 'options:'
echo '-h display help and exit'
}
while getopts ":h" opt;do
case $opt in
h):
usage
exit 0
;;
esac
done
files=$(ls $1/*DAT)
for i in $files;do
echo $i
infile=${i##*/}
basename=${infile%%.*}
newname=${basename%%.*}.txt
outfile=$2/$newname
echo $outfile
a.o $i >$outfile
echo 'processed the file $i'
done
1) first, in the script code, I tried to add the debug option when I call a.O:
a.o --debug >$outfile
Then run the script again, and it is obvious that the problem is not of bash script, instead of a.O
2) Then I made a simple test. I use the a.o and one binary file directly, and launch it with Stace. Strace can trace system calls and signals. It can runs the command until it exists. I launched the code:
strace a.o inputfile >outputfile
Then on the screen it stops at :
open("energy_tables.dat", O_RDONLY) = -1 ENOENT (No such file or directory)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
This shows that the code a.o calls and opens a file energy_tables.dat while it runs, but this file is not found/opened properly ! This causes the segmentation error !
3) when I put the right file of energy_tables.dat in the same folder as a.o, voila, it works !
4) It reminds me that an exception should be added in the original C code for openning the energy_table.dat !
Aucun commentaire:
Enregistrer un commentaire