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
Populating array raised an error yoavbe Shell Programming and Scripting 1 12-05-2008 08:14 AM
Creating an array to hold posix thread ids: Only dynamic array works kmehta High Level Programming 4 09-21-2008 09:24 PM
populating array using awk yoavbe Shell Programming and Scripting 1 09-09-2008 05:30 PM
Awk help with populating variable jmd2004 Shell Programming and Scripting 3 08-11-2008 04:25 PM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 09:42 AM

Reply
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 07-16-2009
Veenak15 Veenak15 is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 25
Populating an Array

Guys,

I need to iterate populate an array while going over files in directory. Can someone please tell me syntax I tried this but it isn't working ==>


Code:
for F in `ls -p "${directory1}" | grep -v "\/"`
do 
  cd "${directory2}"
    cmp  "${directory2}"/"${F}"  "${directory1}"/"${F}"  >/dev/null;REPLY=$?
  if [ ${REPLY} -eq 0 ]
  then
        file_arr_idd=($F)
	echo "$file_arr_idd[0]"
  else
        file_arr_diff=($F)
  fi
done

  #2 (permalink)  
Old 07-16-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
arr=(a b c)
but you can't add element using this method (I think, not checked).

Method 1, works with every posix-sh
init cnt:
idd=1
diff=1

using:
file_arr_idd[$idd]=$F
(( idd+=1 ))

same way diff array
file_arr_diff[$diff]=$F
(( diff+=1 ))

Method 2, assosiative arrays in ksh
init array:
typeset -A file_arr_idd file_arr_diff
using ex:
file_arr_idd[$F]=1
file_arr_diff[$F]=1

print out used array id, ex:

Code:
for f in ${!file_arr_idd[*]}
do   
     echo $f
done

  #3 (permalink)  
Old 07-16-2009
Veenak15 Veenak15 is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 25
Hey kshji!! I just completed my Program I used the follwoing method to populated and run my array:


Code:
set -A file_arr_diff 
set -A file_arr_idd 
i=0
j=0


for F in `ls -p "${directory1}" | grep -v "\/"`
do 
  cd "${directory2}"
  F2=$(find "${directory2}" -name "$F")
  if [ "$F2" = "" ]
  then
        print "WARNING: Could not locate file : $F in $directory2"
  fi
  cmp  "${directory2}"/"${F}"  "${directory1}"/"${F}"  >/dev/null;REPLY=$?
  if [ ${REPLY} -eq 0 ]
  then
        file_arr_idd[i]="${F}"
	((i+=1))
  else
        file_arr_diff[j]="${F}"
	((j+=1))
  fi
done

for FILES in ${file_arr_idd[*]} 
do
   echo "$FILES are identical" 
done
	
echo "********************************************************************************************"

for FILES in ${file_arr_diff[*]} 
do
   echo "$FILES are different" 
done

echo "************************************End of Program*******************************************"



---------- Post updated at 03:13 PM ---------- Previous update was at 03:13 PM ----------

Thanks a lot for your help and prompt reply
Reply

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 07:31 AM.


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