Array Declaration and For Loop | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Array Declaration and For Loop

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-15-2007
33junaid 33junaid is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 1 June 2010, 9:29 AM EDT
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Array Declaration and For Loop

I am just stucked in syntax.This is more like a array and for loop problem.

I want to use ls -l command and get filezise and filename of all filenames in the directory in an array (say array#1).
After 2 minutes of sleep, i want to get the same information in another array (say array#2).

The command ls -l | awk '{print $9 " " $5}' prints all the filenames and then space and then the filezise of that file. I want to compare each array value and then get those filenames which have same size in both arrays.



For example:
Declare Array1......
>ls -l | awk '{print $9 " " $5}'
File1 Size1 (copy this value in array1[0])
File2 Size2 (copy this value in array1[1])
File3 Size3 (copy this value in array1[2])
FileN SizeN (copy this value in array1[N])

After sleep 120, make another array called array2 and then
Declare array2.......
>ls -l | awk '{print $9 " " $5}'
File1 Size1 (copy this value in array2[0])
File2 Size2 (copy this value in array2[1])
File3 Size3 (copy this value in array2[2])
FileN SizeN (copy this value in array2[N])

May be we can get new files after 2 minutes but we have to get all the rows in it.

Then using a for loop, compare each value of array1 with array2 and where array1 value=array2 value, I have to get the value of array1 first argument (i.e. File name itself not the whole string which will include the filesize which is seperated by space).

Please convert this to correct synatx in shell.Thanks.
Sponsored Links
    #2  
Old 09-15-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 20 October 2014, 4:49 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
With zsh, bash, ksh93 and without arrays,
you could write something like this:


Code:
first="$(grep -v ^total <(ls -s))"
sleep ....
read< <(grep  -f <(echo "$first") <(ls -s));echo "${REPLY#* }"

Sponsored Links
    #3  
Old 09-15-2007
33junaid 33junaid is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 1 June 2010, 9:29 AM EDT
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
it is not giving the correct result....where is the filesize in first variable?
    #4  
Old 09-15-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 20 October 2014, 4:49 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
In the first column:


Code:
zsh 4.3.4% first="$(grep -v ^total <(ls -s))"
zsh 4.3.4% echo "$first"
2256 abs-guide.pdf
   4 Desktop
   0 Examples
   4 images

What's wrong with the result you get?
Sponsored Links
    #5  
Old 09-15-2007
33junaid 33junaid is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 1 June 2010, 9:29 AM EDT
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
I just wrote this script and is working very fine:

#!/bin/bash

counter1=0
for i in `ls -ltr | grep -v "total" | grep -v "pre_swh" | awk '{ print $9 ":" $5 }'`;
do


arr_key_old[$counter1]=$i


echo "Counter1 is $counter1 "
echo "Key Old is : ${arr_key_old[$counter1]} "

counter1=`expr $counter1 + 1`
done

echo "Before sleeping"
sleep 120
echo "After sleeping"

counter2=0

for i in `ls -ltr | grep -v "total" | grep -v "pre_swh" | awk '{ print $9 ":" $5 }'`;
do

arr_key_new[$counter2]=$i


echo "Counter2 is $counter2 "
echo "Key new is : ${arr_key_new[$counter2]} "

counter2=`expr $counter2 + 1`
done

counter3=1
for j in `echo ${arr_key_old[*]}`
do

for k in `echo ${arr_key_new[*]}`
do

if [ "$j" = "$k" ]
then
echo " Here testing for old and new : $j is equal to $k"
file_name=`echo $j|awk -F':' '{ print $1 }'`
echo "$counter3 Final file name is $file_name"
counter3=`expr $counter3 + 1`
fi
done
done
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array with do while and if loop nitin Shell Programming and Scripting 5 11-14-2011 10:09 PM
Array declaration in Shell script xerox Shell Programming and Scripting 11 02-28-2011 10:58 AM
Array declaration problem f_o_555 UNIX for Dummies Questions & Answers 2 12-04-2009 01:42 AM
Help with awk in array in while loop vincyoxy Shell Programming and Scripting 2 03-19-2009 12:36 PM
i cannot give array declaration in shell script naree Solaris 20 02-19-2008 02:27 AM



All times are GMT -4. The time now is 03:04 AM.