Bash script fails with "function: not found" error

Hello everyone,

I am having problems figuring this out.
This script below is supposed to create a list of file names with their "md5sum", in a file "lib-list.txt"

When I run it "sh component-list.sh " I get this:component-list.sh: 4: component-list.sh: function: not found
component-list.sh: 5: local: not in a function
Interestingly, I have run it in debug mode and it populated the said text file with a list of lib files

Am running it on Debian 7.

Here is the script:

#!/bin/bash 
cd /usr/local/activemq

function list_dir {
local path=$1
local level=$2
file=$3

#echo $path
#echo $level
if [ $level -gt 2 ];then return;fi
for i in `ls $path`
do
        local line=""
        for n in `seq 0 $level`
        do
            line+="\x20\x20"
        done
if [ `echo $i | grep -E "\.zip$|\.jar$"` ]
then
      md=`md5sum $path/$i | awk '{print $1}'`
      echo -e $line${i##*/}"\x20\x20\x20\x20"$md >> "./$file"
fi
done

for i in `ls $path`
do
        local line=""
        for n in `seq 0 $level`
        do
            line+="\x20\x20"
        done
if [ -d $path/$i ]
then
    echo -e $line${i##*/} >> $file
    list_dir $path/$i $[level+1] $file
fi
done
}
rm lib-list.txt
echo -e "lib" >> lib-list.txt
list_dir ./lib 0 lib-list.txt

Can someone help me out please.

Are you sure /usr/local/activemq exists or is readable/traversable? And you must be able to write there for you have lines such as:

echo -e $line${i##*/}"\x20\x20\x20\x20"$md >> "./$file"
echo -e $line${i##*/} >> $file

In case you just give a file name...
#--- Addendum: forgotten...
And especially :

rm lib-list.txt
echo -e "lib" >> lib-list.txt
list_dir ./lib 0 lib-list.txt

Hi vbe,

Thanks for the respnse.

Yes, /usr/local/activemq exists with these rights: drwxr-xr-x

Under the directory, all files have either "-rwxr-xr-x" or "-rw-r--r--" and directories have either "drwxr-xr-x" or "drwxr-sr-x"

try:

 ./component-list.sh 

your script is not sh compatible it seems...

No 'need' to define variables with local as this doesnt 'exist'.
Remove those and it should run.

However, you could update the 'outdated' `command` to $(command) while beeing on it.. :wink: