Hi, I'm writing a set of small utilities as scripts, and I got a
segmentation fault working on one of them.

The script is suppoused to align text with spaces. Say you have this file:

Foo1\tFoo2
Baaaaaaaaaaaar\tBar2
Baz

Where \t are horizontal tabs. My script would replace the tabs with an
adequate number of spaces to align foo2 and bar2.

Right now it works with a file named "file" in the working directory.
Of course this is only temporal.

The problem is that I get a segmentation fault when I run it. That
never happened to me with a shell script. And I can't see where should
be a problem.

I'm running OpenBSD 4.1-stable, GENERIC, i386. I don't know if it's
important, but I didn't create a swap partition (I'm planning to
change this).

If someone could light me, I'd be very grateful.

Here is the script:

#!/bin/sh

IFS='
'

file=file

for line in `< "${file}"`; do

        fields=`printf '%s' "${line}" | sed 's/[^       ]//g' | wc -m`
        fields=$((${fields} + 1))

        if [ "${fields}" -eq 1 ]; then
                printf '%s' "${line}"
        else
                for field in `jot "${fields}"`; do
                        max_width=`cut -f "${field}" "${file}" | awk '{ l = 
length($0); if
(l > m) m = l } END { print m }'`
                        width=`printf '%s' "${line}" | cut -f "${field}" | awk 
'{ print
length($0) }'`
        
                        printf '%s' "`printf '%s' "${line}" | cut -f 
"${field}"`"
        
                        if [ "${field}" -lt "${fields}" ]; then
                                for i in `jot "$((${max_width} - ${width}))"`; 
do
                                        printf '.'
                                done
                        fi
                done
        fi

        printf '\n'
done

Reply via email to