The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
forcefully throwing error : unix script dr46014 UNIX for Dummies Questions & Answers 1 10-01-2008 04:42 AM
awk Shell Script error : "Syntax Error : `Split' unexpected Herry UNIX for Dummies Questions & Answers 2 03-17-2008 11:16 AM
Error while trying to fire a PL/SQL thro Unix Script dharmesht Shell Programming and Scripting 5 12-11-2003 10:10 AM
UNIX error log eysheikah Security 3 05-31-2003 09:21 AM
SCO UNIX error 6, HELP PBNOSGT UNIX for Dummies Questions & Answers 0 02-01-2002 11:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-18-2009
joyrules joyrules is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 3
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 10:39 AM.. Reason: Formatted script
  #2 (permalink)  
Old 03-18-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool 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 (permalink)  
Old 03-18-2009
joyrules joyrules is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 3
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 (permalink)  
Old 03-18-2009
joyrules joyrules is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 3
Thanks

Thanks Joyeg
  #5 (permalink)  
Old 03-18-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
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 (permalink)  
Old 03-18-2009
angad.makkar angad.makkar is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 16
Thumbs up 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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0