Error in Unix Script

Hi!
Following is the script to find the number of users and the total space consumed by them in the directory
But I am getting few errors Pls help me

data=`ls -lt $1 |tr -t [' '] ['\t']|tr -s '\t'|cut -f3,5`

count=0

flag=1

sum=0

users_flag=1

check=0

declare -a users

for a in $data

do

	if [ $users_flag -eq 1 ]

	then

		for ((i = 0; $i <= count; $i++))

		do

			if [ $a = ${users[$count]} ]

			then

				flag=0

				break

			fi

		done

		if [ $flag  -eq 1 ]

		then

			users[$count]=$a

			count=expr`$count + 1`

			echo $a >> userdata

		fi

		users_flag=0

	else

		if [ $flag -eq 1 ]

		then

			

			for b in $data

			do

				if [ $check -eq 0 ]

				then

					if [ $b = $a ]

					then 

						check=1

					fi

				else

					sum=`expr $sum + $b`

					check=0

				fi 

			done

		fi

	fi

done

Errors are
q3: 7: declare: not found
q3: 9: Syntax error: word unexpected (expecting "do")

Once you paste your script, highlight it and then click on the # on the icons bar above; this will keep the formatting. This makes it much easier to follow the code and the various conditions.

data=`ls -lt $1 |tr -t [' '] ['\t']|tr -s '\t'|cut -f3,5` 
count=0 
flag=1 
sum=0 
users_flag=1 
check=0 
declare -a users 
for a in $data 
do 
    if [ $users_flag -eq 1 ] 
    then 
        for ((i = 0; $i <= count; $i++)) 
        do 
            if [ $a = ${users[$count]} ] 
            then 
                flag=0 
                break 
            fi 
        done 
        if [ $flag  -eq 1 ] 
        then 
            users[$count]=$a 
            count=expr`$count + 1` 
            echo $a >> userdata 
        fi 
        users_flag=0 
    else 
        if [ $flag -eq 1 ] 
        then 
             
            for b in $data 
            do 
                if [ $check -eq 0 ] 
                then 
                    if [ $b = $a ] 
                    then  
                        check=1 
                    fi 
                else 
                    sum=`expr $sum + $b` 
                    check=0 
                fi  
            done 
        fi 
    fi 
done 

Thanks Joyeg

What version of unix / linux ?
uname -a

Which shell? Is this "sh", "ksh", "bash" or what?
echo $SHELL

Please show an example of what the output of the script should look like and state whether you count directory sizes as well as file sizes towards the per-user total.

Please state whether there are subdirectories in the directory you wish to analyse.

Hi Joyeg....

Just try this out:

#!/bin/bash

declare -a name_arr
declare -a size_arr

i=0 
for files in `ls`
do
   name=`ls -l $files | awk '{print $3}'`
   size=`ls -l $files | awk '{print $5}'`

   if [ $i -eq 0 ]
   then
      name_arr[$i]=`echo $name`
      size_arr[$i]=`echo $size`
      i=`expr $i + 1`
   else
      j=0
      while [ $j -lt $i ]
      do
         if [ "${name_arr[$j]}" = "$name" ]
         then
	    size_arr[$j]=`expr ${size_arr[$j]} + $size`
	    break
         else
	    name_arr[$i]=`echo $name`
	    size_arr[$i]=`echo $size`
	    i=`expr $i + 1`
         fi
         j=`expr $j + 1`
      done
   fi
done

j=0
while [ $j -lt $i ]
do
	echo "${name_arr[$j]} : ${size_arr[$j]}"
	j=`expr $j + 1`
done

Lemme know incase of any problem

Also, for your declare error, try to run your script in bash shell.