Error in Unix Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error in Unix Script
# 1  
Old 03-18-2009
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
Code:
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")

Last edited by joyrules; 03-18-2009 at 11:39 AM.. Reason: Formatted script
# 2  
Old 03-18-2009
Tools Please repost your code using CODETAGS

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.
# 3  
Old 03-18-2009
Agin Script Formatted

Code:
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

# 4  
Old 03-18-2009
Thanks

Thanks Joyeg
# 5  
Old 03-18-2009
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.
# 6  
Old 03-18-2009
MySQL This works....

Hi Joyeg....

Just try this out:
Code:
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script error message

I have this UNIX script code with a query to export sql table in Oracle and export to csv file. The code gets the data correctly. However, when I run the script second time, I got the error message "not spooling currently" and shows the older data in csv file. When I delete the csv file and run... (5 Replies)
Discussion started by: Hope
5 Replies

2. UNIX for Beginners Questions & Answers

UNIX script error message

Greeting!! I wrote the below script to e-mail me only file names in a specific directory when a file is delayed for some time in a that directory. I am getting unexpected eof error message. I don't want any email if the folder is blank.(if the condition is not met) I am not getting the email at... (4 Replies)
Discussion started by: Hope
4 Replies

3. UNIX for Advanced & Expert Users

Isql and If Exist syntax error in UNIX script

Hello Everyone, Coming again for your help to solve the below error: In a script, i had created a temp table (Temp_table) and loaded the data in it using bcp command (performed successfully) and I wanted to move it to the preferred table (called Main_table) for further use. hence I have added... (7 Replies)
Discussion started by: Suresh
7 Replies

4. Shell Programming and Scripting

Isql syntax error in UNIX script

Hello Everyone, Coming again for your help to solve the below error: In a script, i had created a temp table (Temp_table) and loaded the data in it using bcp command (performed successfully) and I wanted to move it to the preferred table (called Main_table) for further use. hence I have added... (1 Reply)
Discussion started by: Suresh
1 Replies

5. Shell Programming and Scripting

UNIX shell script error: not found

Hello All, I'm working on one Unix shell sript that will actually extract a column data from Oracle table using sqlplus and writes onto a file. I'm getting this error when executing the shell script: rpt.ksh: ^JScott: not found Below is the code in the shell script: `sqlplus -s... (1 Reply)
Discussion started by: venkat_reddy
1 Replies

6. Shell Programming and Scripting

Error in UNIX script

When i run a script in linux. It shows the following error. 'icsftpagent.sh: line 114: syntax error near unexpected token `'icsftpagent.sh: line 114: `Nicsftp() The script is as follows. #!/bin/bash #### Copyright Notice: #### #### Copyright (c) 2007 Network Intelligence Corporation ... (7 Replies)
Discussion started by: sourabh.chhabra
7 Replies

7. Shell Programming and Scripting

UNIX Error in Script

When i run a script in linux. It shows the following error. 'icsftpagent.sh: line 114: syntax error near unexpected token ` 'icsftpagent.sh: line 114: `Nicsftp() My Script is as follows: #!/bin/bash #### Copyright Notice: #### #### Copyright (c) 2007 Network Intelligence Corporation ... (1 Reply)
Discussion started by: sourabh.chhabra
1 Replies

8. Shell Programming and Scripting

Syntax Error in Unix Shell Script

I am trying to run a unix script in my home directory.Snippet below echo "`date '+%Y%m%d_%H%M%S'` Getting ProductList.dat" if ( -f $DIR/ProductList.dat) then cp $DIR/ProductList.dat MigratedProductList.dat else echo "`date '+%Y%m%d_%H%M%S'`ProductList.dat does not exist; Processing... (4 Replies)
Discussion started by: Mary James
4 Replies

9. UNIX for Dummies Questions & Answers

forcefully throwing error : unix script

how can i make one script fail if some condition is not satisfied.i m writing if ..else logic in script.i need some standard command to do that ,, (1 Reply)
Discussion started by: dr46014
1 Replies

10. Shell Programming and Scripting

Error while trying to fire a PL/SQL thro Unix Script

hi , I tried running a PL/SQL script through unix shell script. But am getting the following error. "Message file sp1<lang>.msb not found Error 6 initializing SQL*Plus " Kindly suggest. Regards, Samit (5 Replies)
Discussion started by: dharmesht
5 Replies
Login or Register to Ask a Question