PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > put find output in an array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

put find output in an array

Réponse
 
LinkBack Outils de la discussion
Vieux 25/08/2006, 11h39   #1
johnmmcparland
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut put find output in an array

Hi all,

I have a script which finds all the files edited x days ago.

Having found these files, I wish to loop round them, check their name,
and if they do not have the same name as the script, remove them.

My probelm is once I've executed the find command;

find . -mtime ${maxTime}

how do I get this into an array?

After this I would do something like

for f in $array
do
.....
done

where $array is the array containing the find output

thanks

  Réponse avec citation
Vieux 25/08/2006, 12h06   #2
Stephan 'smg' Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

johnmmcparland wrote:
> Hi all,
>
> I have a script which finds all the files edited x days ago.
>
> Having found these files, I wish to loop round them, check their name,
> and if they do not have the same name as the script, remove them.
>
> My probelm is once I've executed the find command;
>
> find . -mtime ${maxTime}
>
> how do I get this into an array?


You don't need that.
find . -mtime ${maxTime} -exec echo {} \;
>
> After this I would do something like
>
> for f in $array
> do
> ....
> done
>
> where $array is the array containing the find output
>
> thanks
>

np

- --
Stephan 'smg' Grein, <stephan at stephan minus rockt dot de>
https://stephangrein.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iQIVAwUBRO7ZsI1LAjD4wnXUAQIh4w/9H1Z2WQSf2bHa9QtI9FYVWrHc4r84QtFb
BQREyevHDWlxrDFgv3vhd3tKIgHWmSdvpmrrCUSqPXISrlkxG/1ny26YXYXeJ5/U
VydPuBKJG4Olvwl8InPeRlVXioa2fvLAFKd4RmXANW0rZlmB5t vPsmfG8qIIhQpx
9Lbz47Ox7j9r3Vft+7pnpM+zraCbMsNOmChDrC/h9G5tBbhvo34PjReNXhZsR0EQ
emqbDzmm0xYn/dpJTrEi4yNWEVbenZFns4Kt+VSu1bCa3jdfLtiJ7Lpr1Y1cHfD 5
xjOB8RuGULKFM1x+l5sBhcpfjEWUm1+nMX23c/iHcehkPhpN1KTSatHJxdH8x1Xk
2CGqMPMOvZIZSS8gR2KFGdBhWZZKsE6lqexo3xAe6gaCpYGSVm 7Rr+epl6dE2l6d
NHC+CZ3voiTtAAFPN/mPKX+JtaZ0UVOFdC0npj7+3AoLnkdGwZROjazfYzRFmz7C
d7sG6wI9plYx+MJXNCjxPJ0rvaZHvHw3duEIQpk87vZdgLvQNN A/bhnmH9hgw5Wa
5eRkH5nPaCJRJmazbM7YEJodqQW8RAvqFP5Rjp4cy7Kx6cEnNs 7gu87WMF+IDibV
ivmDFhj9i+q9MQu9aR8irNaLDt6PGeLOQVU/xnedp6gq/8HK9jRzwrqwJKKt6OgL
APMIhUcSoz4=
=zar6
-----END PGP SIGNATURE-----
  Réponse avec citation
Vieux 25/08/2006, 12h07   #3
Stephan 'smg' Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Also you could pipe the find output to a for loop.

Bye.
- --
Stephan 'smg' Grein, <stephan at stephan minus rockt dot de>
https://stephangrein.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iQIVAwUBRO7Z941LAjD4wnXUAQI14RAAkJ5qg2wxajGOH0TJiQ SOQFjJzQV8mNuo
IZTsXoVFecAutcLNzjIZRstuBytf+SuKRi7/Yb0ndtEyTkpjngx79o+hLgu4yBLn
HXAMFBqB2ILeJcdk2wEfk2FjtIBbB2GP0s4V6VRadyMCfYYTmp s/0wXTW6jWk4Cs
p4XLEEk/LIu5soggAU4v1/O3uUbeB3Tn8FwqDALloEIFAwZEJyZAkx+d16Ewrvz+
8wesp9CjwMjoVUJNHbWMwojHOutDVEdv8pK/6S5PYTtbtv0Iwmh675/QWPrm21DP
7wNeqzrsnGq9Kd6mn8og6nCJgJo1GXIy9GnrYkDk1P9VPrN1lz eJ0MRIa0ce2sqV
Y16h1pO91uPFQpCtqM6ROHxWu2bG6gWxk1i6SDxIb9WjK7u4P1 0SQVuYfgKbW1+J
NxbXXqX08BDuLdf6A7+CetNClruh6BlQgf6Lbw60ejVYVIBizZ rIQY997IIvzEuG
CdyF/V87u+Fx1uWUU1XvHMae5c7bNvMGJvLt4zJoBAvyMQ5Z9gvGVOW 9NwGXNnvA
LHDp2VoyiVrQAf0EQwVmSNj+VWup8nwNBu2sWAaeeZHLNZSqmw RRpbOWUiWh4/Np
Z2I0O3oJxFSN4JSqm7ObtPo2a38hfvku25h4/3vn9vsC41qXW2L/mvZmWWdsVJ/2
9USFAoNE12c=
=00/l
-----END PGP SIGNATURE-----
  Réponse avec citation
Vieux 25/08/2006, 13h36   #4
johnmmcparland
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

when I do the -exec ...

what variable are the filenames in?

if want to do;

if ${f} != fileNameNotToBeDeleted
rm ${f}

where f is a filename what should I be using in place of f?

Stephan 'smg' Grein wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> johnmmcparland wrote:
> > Hi all,
> >
> > I have a script which finds all the files edited x days ago.
> >
> > Having found these files, I wish to loop round them, check their name,
> > and if they do not have the same name as the script, remove them.
> >
> > My probelm is once I've executed the find command;
> >
> > find . -mtime ${maxTime}
> >
> > how do I get this into an array?

>
> You don't need that.
> find . -mtime ${maxTime} -exec echo {} \;
> >
> > After this I would do something like
> >
> > for f in $array
> > do
> > ....
> > done
> >
> > where $array is the array containing the find output
> >
> > thanks
> >

> np
>
> - --
> Stephan 'smg' Grein, <stephan at stephan minus rockt dot de>
> https://stephangrein.de
> GnuPG-Key-ID: 0xF8C275D4
> FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.5 (GNU/Linux)
>
> iQIVAwUBRO7ZsI1LAjD4wnXUAQIh4w/9H1Z2WQSf2bHa9QtI9FYVWrHc4r84QtFb
> BQREyevHDWlxrDFgv3vhd3tKIgHWmSdvpmrrCUSqPXISrlkxG/1ny26YXYXeJ5/U
> VydPuBKJG4Olvwl8InPeRlVXioa2fvLAFKd4RmXANW0rZlmB5t vPsmfG8qIIhQpx
> 9Lbz47Ox7j9r3Vft+7pnpM+zraCbMsNOmChDrC/h9G5tBbhvo34PjReNXhZsR0EQ
> emqbDzmm0xYn/dpJTrEi4yNWEVbenZFns4Kt+VSu1bCa3jdfLtiJ7Lpr1Y1cHfD 5
> xjOB8RuGULKFM1x+l5sBhcpfjEWUm1+nMX23c/iHcehkPhpN1KTSatHJxdH8x1Xk
> 2CGqMPMOvZIZSS8gR2KFGdBhWZZKsE6lqexo3xAe6gaCpYGSVm 7Rr+epl6dE2l6d
> NHC+CZ3voiTtAAFPN/mPKX+JtaZ0UVOFdC0npj7+3AoLnkdGwZROjazfYzRFmz7C
> d7sG6wI9plYx+MJXNCjxPJ0rvaZHvHw3duEIQpk87vZdgLvQNN A/bhnmH9hgw5Wa
> 5eRkH5nPaCJRJmazbM7YEJodqQW8RAvqFP5Rjp4cy7Kx6cEnNs 7gu87WMF+IDibV
> ivmDFhj9i+q9MQu9aR8irNaLDt6PGeLOQVU/xnedp6gq/8HK9jRzwrqwJKKt6OgL
> APMIhUcSoz4=
> =zar6
> -----END PGP SIGNATURE-----


  Réponse avec citation
Vieux 25/08/2006, 15h48   #5
Jon LaBadie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

johnmmcparland wrote:
> Hi all,
>
> I have a script which finds all the files edited x days ago.
>
> Having found these files, I wish to loop round them, check their name,
> and if they do not have the same name as the script, remove them.
>
> My probelm is once I've executed the find command;
>
> find . -mtime ${maxTime}
>
> how do I get this into an array?
>
> After this I would do something like
>
> for f in $array
> do
> ....
> done
>
> where $array is the array containing the find output
>
> thanks
>


find . -type f -mtime ${maxTime} ! -name ${badname} -exec rm {} ';'

===

find . -type f -mtime ${maxTime} |
while read name
do
#something with "$name"
done

===

array=( $(find . -type f -mtime ${maxTime}) )

for f in ${array[*]}
do
#something with "$f"
done
  Réponse avec citation
Vieux 25/08/2006, 16h18   #6
johnmmcparland
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

Thanks,


Jon LaBadie wrote:
> johnmmcparland wrote:
> > Hi all,
> >
> > I have a script which finds all the files edited x days ago.
> >
> > Having found these files, I wish to loop round them, check their name,
> > and if they do not have the same name as the script, remove them.
> >
> > My probelm is once I've executed the find command;
> >
> > find . -mtime ${maxTime}
> >
> > how do I get this into an array?
> >
> > After this I would do something like
> >
> > for f in $array
> > do
> > ....
> > done
> >
> > where $array is the array containing the find output
> >
> > thanks
> >

>
> find . -type f -mtime ${maxTime} ! -name ${badname} -exec rm {} ';'
>
> ===
>
> find . -type f -mtime ${maxTime} |
> while read name
> do
> #something with "$name"
> done
>
> ===
>
> array=( $(find . -type f -mtime ${maxTime}) )
>
> for f in ${array[*]}
> do
> #something with "$f"
> done


  Réponse avec citation
Vieux 26/08/2006, 18h15   #7
kenneth kahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

johnmmcparland wrote:

> I have a script which finds all the files edited x days ago.
>
> Having found these files, I wish to loop round them, check their name,
> and if they do not have the same name as the script, remove them.
>
> My probelm is once I've executed the find command;
>
> find . -mtime ${maxTime}
>
> how do I get this into an array?


If you use somthing like KSH93 or equivalent, you can do

set -A files 0 $(find . -mtime ${maxTime})

This will set up an array files[1] .. files[n]; you can also set up a zero based
array by leaving off the leading '0' value.

You can then set up a loop:

typeset -i filecnt=${#files[*]}
typeset -i cnt=0;
while cnt=cnt+1 ; (( cnt < filecnt )) ; do
# stuff against ${files[cnt]}
done
  Réponse avec citation
Vieux 26/08/2006, 19h29   #8
Michal Nazarewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

"johnmmcparland" <johnmmcparland@googlemail.com> writes:

> I have a script which finds all the files edited x days ago.
> Having found these files, I wish to loop round them, check their name,
> and if they do not have the same name as the script, remove them.
>
> My probelm is once I've executed the find command;
> find . -mtime ${maxTime}
> how do I get this into an array?
>
> After this I would do something like
> for f in $array; do ...; done
> where $array is the array containing the find output


The problem with find is that it's useless. Without -print0 option
it'll just crush and burn but unfortunatelly -print0 is not in the
standard. You can therefore do two things, either:

find . -type f -mtime $maxTime -exec some_script {} \;

where some_script is a script which takes the file name and delete it
if you wish; or:

find . -type f -mtime $maxTime ! -name 'some-pattern' -exec rm {} \;

where some-pattern is a pattern which file must not much.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
  Réponse avec citation
Vieux 26/08/2006, 20h05   #9
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

2006-08-26, 13:15(-04), kenneth kahn:
[...]
> If you use somthing like KSH93 or equivalent, you can do
>
> set -A files 0 $(find . -mtime ${maxTime})


ITYM

IFS=$'\n'
set -f
set -A files 0 $(find . -mtime "$maxTime" -print)

which is slightly better but not correct yet.

You need GNU find and zsh for the task.

IFS=$'\0'
files=($(find . -mtime $maxTime -print0))

or simply as zsh has find functionality builtin:

files=(./**/*(ND.m$maxTime))

--
Stéphane
  Réponse avec citation
Vieux 26/08/2006, 21h15   #10
First Lensman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

> how do I get this into an array?

In the Korn Shell there is an ability to execute a command and put the
results into an array:

#!/bin/ksh
set -A myarray $(find ./ -name "*.pl")
let count=0
while (( $count < ${#myarray[*]} )); do
echo FILE: ${myarray[count]}
let count="count + 1"
done

Also, you could use perl:

#!/usr/bin/perl
open FINDEM,'find ./ -name "*.pl" |' or die "Cannot open pipe: $!\n";
@findarr = <FINDEM>;
for ($i = 0; $i <= $#findarr; $i++) {
print "FILE: $findarr[$i]";
}

You can also use this perl:

#!/usr/bin/perl
@findarr = `find ./ -name "*.pl"`;
for ($i = 0; $i <= $#findarr; $i++) {
print "FILE: $findarr[$i]";
}

But, I think the backtick ( ` ) operator has been deprecated and may
not be available in current or future versions of perl. I'm using
version 5.8.0 of perl -- not sure if this is current.

I didn't use your exact find command for my testing purposes. This will
at least give you a head start.

Hope this s.

Art Ramos

  Réponse avec citation
Vieux 26/08/2006, 21h42   #11
First Lensman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

Your second option was great but it only allows one pass on the
results.

> array=( $(find . -type f -mtime ${maxTime}) )
> for f in ${array[*]}
> do
> #something with "$f"
> done


I tried this option but it doesn't like the outer parenthesis on the
first line for some reason.
Is this a mistype?

Thanks.

Art Ramos

  Réponse avec citation
Vieux 26/08/2006, 22h05   #12
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

First Lensman wrote:
> > how do I get this into an array?

>
> In the Korn Shell there is an ability to execute a command and put the
> results into an array:
>
> #!/bin/ksh
> set -A myarray $(find ./ -name "*.pl")
> let count=0
> while (( $count < ${#myarray[*]} )); do
> echo FILE: ${myarray[count]}
> let count="count + 1"
> done
>
> Also, you could use perl:
>
> #!/usr/bin/perl
> open FINDEM,'find ./ -name "*.pl" |' or die "Cannot open pipe: $!\n";
> @findarr = <FINDEM>;
> for ($i = 0; $i <= $#findarr; $i++) {
> print "FILE: $findarr[$i]";
> }
>
> You can also use this perl:
>
> #!/usr/bin/perl
> @findarr = `find ./ -name "*.pl"`;
> for ($i = 0; $i <= $#findarr; $i++) {
> print "FILE: $findarr[$i]";
> }
>
> But, I think the backtick ( ` ) operator has been deprecated and may
> not be available in current or future versions of perl. I'm using
> version 5.8.0 of perl -- not sure if this is current.


Never heard of that.. my current version is: 5.8.7

pipe or backtick or qx() are not necessary in this case. Perl has
modules, like File::Find, to do such job, which should be
platform-independent.
##################
use strict;
use warnings;
use File::Find;

my @findarr=();
find(\&findpl, @ARGV);
sub findpl {
push @findarr, $File::Find::name if -f && /\.pl$/;
}
print map "$_\n", @findarr;
###################

Xicheng

  Réponse avec citation
Vieux 26/08/2006, 22h23   #13
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

First Lensman wrote:
> Your second option was great but it only allows one pass on the
> results.
>
> > array=( $(find . -type f -mtime ${maxTime}) )
> > for f in ${array[*]}
> > do
> > #something with "$f"
> > done

>
> I tried this option but it doesn't like the outer parenthesis on the
> first line for some reason.
> Is this a mistype?
>


that worked fine in the following shells:

1) Linux 2.6.12-9-386 GNU/Linux,
GNU bash, version 3.00.16(1)-release (i486-pc-linux-gnu)
2) SunOS 5.8
GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)

not working under 'tcsh'.

Also, if there are whitespaces within filenames, it might be better to
reset IFS before assigning array, like:

IFS= ; arrar=( $(find . -type f -mtime ${maxTime}) )

Xicheng

  Réponse avec citation
Vieux 26/08/2006, 23h38   #14
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: put find output in an array

First Lensman wrote:
>>how do I get this into an array?

>
> In the Korn Shell there is an ability to execute a command and put the
> results into an array:
>
> #!/bin/ksh
> set -A myarray $(find ./ -name "*.pl")
> let count=0
> while (( $count < ${#myarray[*]} )); do
> echo FILE: ${myarray[count]}
> let count="count + 1"
> done
>
> Also, you could use perl:
>
> #!/usr/bin/perl
> open FINDEM,'find ./ -name "*.pl" |' or die "Cannot open pipe: $!\n";
> @findarr = <FINDEM>;
> for ($i = 0; $i <= $#findarr; $i++) {
> print "FILE: $findarr[$i]";
> }


A while loop is normally used in that situation. Also if 'find' dies for some
reason you should capture its error code when you close the filehandle and you
can avoid invoking a shell to run find by passing open a list instead of a string:

#!/usr/bin/perl
open FINDEM, '-|', 'find', './', '-name', '*.pl'
or die "Cannot open pipe: $!\n";
while ( my $file = <FINDEM> ) {
print "FILE: $file";
}
close FINDEM
or warn $! ? "Error closing find pipe: $!" : "Exit status $? from find";


> You can also use this perl:
>
> #!/usr/bin/perl
> @findarr = `find ./ -name "*.pl"`;
> for ($i = 0; $i <= $#findarr; $i++) {
> print "FILE: $findarr[$i]";
> }


You don't need a second copy of the list stored in an array:

#!/usr/bin/perl
for my $file ( `find ./ -name "*.pl"` ) {
print "FILE: $file";
}

Or without the loop:

#!/usr/bin/perl
print map "FILE: $_", `find ./ -name "*.pl"`;


> But, I think the backtick ( ` ) operator has been deprecated and may
> not be available in current or future versions of perl. I'm using
> version 5.8.0 of perl -- not sure if this is current.


In all the many years I haved used Perl I have never heard that and I doubt
very much that that is true.



John
--
use Perl;
program
fulfillment
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h02.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23505 seconds with 22 queries