Why Does this Shell Script Hang?

I have this short and simple shell script that processes a log file with the following format:

[function address in hex notation] [timestamp when it was invoked]

and attempts to generate output in the following format:

[function name] [timestamp when it was invoked]

I've come up with the following, but for some reason, it only prints output correctly for the first line of the input file, then hangs indefinitely:

#!/bin/bash 

for line in $(egrep -o "(0x[0-9a-fA-F]+)\s[0-9]+\.[0-9]+" ./syntax_check_out.txt)
    do 
        addr=`echo $line | egrep -o "0x[0-9a-fA-F]+"` 
        stamp=`echo $line | egrep -o "[0-9]+\.[0-9]+"`
        func=`addr2line -f -e ../mzscheme3m $addr | head -1`
     
        echo "${func} ${stamp}"        
done 

Any ideas why this isn't working? I'm not very good with shell scripting, so I may have made an obvious mistake.

Thanks all in advance!

Can you post a sample of your input file and the desired output?

I notice the loop construction you chose will put one word per line instead of two. So the assignments inside the loop will not be performed corrrectly. Perhaps this will give a better result (not tested)?

egrep -o "(0x[0-9a-fA-F]+)\s[0-9]+\.[0-9]+" ./syntax_check_out.txt| 
while read addr stamp; do
  echo "$(addr2line -f -e ../mzscheme3m $addr | head -1) $stamp"
done

Sample input file attached.

An input line will look like this:

0x49d205 1.265053

And an output line should look like this:

some_function_name 1.265053

I realize I could probably use a sed/awk script for this also, but I don't see any reason why the script I have won't work.

Hi,
if the input is consistent, I gues You could simplify Your script a bit, here's a version based on Scrutinizers example:

while read addr stamp; do
  echo "$(addr2line -f -e ../mzscheme3m $addr | head -1) $stamp"
done <syntax_check_out.txt

And if something goes wrong it's probably in addr2line, what happens if You type

addr2line -f -e ../mzscheme3m 0x49d205

on the command line?

/Lakris

PS Clarifcation; Is the program ../mzscheme3m available, is it debug-able, etc

addr2line works fine if I invoke it directly in the shell for any given function address. The binary I'm inspecting is a program I wrote and compiled, and it's fully debuggable.

What a strange problem.

So did you try the revised loop as well? What result did that have?

Yes I did, and it worked! Thanks!

I didn't realize you could pipe output of a command directly into a read loop like that, which is cool :slight_smile:

Good... Alternatively the solution that lakris suggested should work too if the format of the file is truly as you described (i.e. [function address in hex notation] [timestamp when it was invoked] ) . Then you should not even need a grep filter nor a pipe..