LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash script fails with "function: not found" error on Debian 7 virtual machine (https://www.linuxquestions.org/questions/linux-general-1/bash-script-fails-with-function-not-found-error-on-debian-7-virtual-machine-4175480381/)

joemb 10-11-2013 01:29 AM

bash script fails with "function: not found" error on Debian 7 virtual machine
 
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 (bash -x component-list.sh) and it populated the said text file with a list of lib files

Am running it on Debian 7.

Here is the script:
Code:

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

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

Your help in figuring out the problem would be highly appreciated.

Thank you.

pan64 10-11-2013 01:35 AM

probably you need to execute it as bash <scriptname> instead of sh <scriptname>, or just <scriptname>

joemb 10-11-2013 01:46 AM

Hi pan64,

Thanks so much for your quick and helpful response.

Regards,

Joe


All times are GMT -5. The time now is 04:59 PM.