File test failed!
am 16.09.2011 11:53:25 von yyq
--------------080100010103040506030506
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Hi, All:
I want to test if a file exists or it is a link or not, the code is:
#!/usr/bin/perl -w
use strict;
use File::Basename;
use File::stat;
use Fcntl ':mode';
my $elfobj = $ARGV[0];
sub strtrim ()
{
#.....
}
sub copyfile ()
{
#.....
}
my @deps = `ldd $elfobj`;
foreach my $lib (@deps) {
chomp ($lib);
my ($dep,$path) = split ("=>", $lib);
##handle path, and it value is /lib64/ld-linux-x86-64.so.2, which
is a link file.
if (-e $path) {
print $path."\n";
}
}
But the highlighted code always return undef. When put the code outside
of the foreach, it ok. Any one encountered this situation?
--------------080100010103040506030506--
Re: File test failed!
am 16.09.2011 12:34:01 von Magnus Woldrich
On 2011-09-16 17:53, yyq@eisoo.com wrote:
> I want to test if a file exists or it is a link or not
You test for a regular file with -f.
You test for existance with -e.
You test for symbolic links with -l.
See
perldoc -f -x
for more information.
--=20
â=82 Magnus Woldrich
â=82 m@japh.se
â=82 http://japh.se
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: File test failed!
am 16.09.2011 13:09:31 von Shlomi Fish
Hi YYQ,
On Fri, 16 Sep 2011 17:53:25 +0800
"yyq@eisoo.com" wrote:
> Hi, All:
>=20
> I want to test if a file exists or it is a link or not, the code is:
> #!/usr/bin/perl -w
>=20
> use strict;
> use File::Basename;
> use File::stat;
> use Fcntl ':mode';
>=20
> my $elfobj =3D $ARGV[0];
>=20
> sub strtrim ()
> {
> #.....
> }
>=20
> sub copyfile ()
> {
> #.....
> }
>=20
> my @deps =3D `ldd $elfobj`;
> foreach my $lib (@deps) {
> chomp ($lib);
> my ($dep,$path) =3D split ("=3D>", $lib);
> ##handle path, and it value is /lib64/ld-linux-x86-64.so.2, which=20
> is a link file.
> if (-e $path) {
> print $path."\n";
> }
> }
Well, one problem I can see is that the ldd output contains whitespace befo=
re
and after the "=3D>", so this may trip you. To be sure what exactly is happ=
ening,
you can use the perl debugger ("perl -d") to investigate:
http://perl-begin.org/topics/debugging/
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/
In Soviet Russia, every time you kill a kitten, God masturbates.
â=94 http://linux.slashdot.org/comments.pl?sid=3D195378&cid=3D160 0=
9070
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/