Re: Equal strings don't match!
by kyle (Abbot) on Jul 30, 2008 at 18:28 UTC
|
On my system, stuff read from /proc/*/cmdline comes complete with a trailing null character (chr 0) and also nulls separating the command from its arguments. I might rewrite your code this way:
use strict;
use warnings;
opendir my $proc_dh, '/proc'
or die "Can't opendir '/proc': $!";
my @every_pid = grep /\d/, readdir $proc_dh;
closedir $proc_dh;
chomp @every_pid;
my @every_cmd;
foreach my $pid ( @every_pid ) {
open my $cmdline_fh, '<', "/proc/$pid/cmdline"
or die "Can't open '/proc/$pid/cmdline': $!\n";
my ($cmd) = split /\0/, scalar <$cmdline_fh>;
push @every_cmd, $cmd if defined $cmd;
close $cmdline_fh;
}
if ( grep { '/usr/sbin/sshd' eq $_ } @every_cmd ) {
print "found\n";
}
else {
print "not found\n";
}
Notice that,
I would point out also that you may be able to say "ps -C sshd" at your command prompt and find out what you want. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
Thanks,but this is just a small part of my program,I need to get the values exactly like they are or I will need to change other parts of the program. I need to know why they don't match, because they look the same! Even with chomp the code doesn't work(I have already checked if the cmdline has newlines, but it doesn't)!
You think that if I remove the null character(\0) in all elements of @CMD, it will work?
| [reply] [Watch: Dir/Any] |
|
tr/\0//d for @CMD;
That means, however, that a command line with several arguments (e.g., "echo one two") will have those arguments all stuck together (e.g., "echoonetwo") instead of separated with nulls.
Without knowing what you're really trying to do, it's hard to give good advice. See XY Problem. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|
Problem resolved! It was the null character(\0)...I used:
$/ = "\0";
chomp(@CMD);
Cheers,
fl.
| [reply] [Watch: Dir/Any] |
Re: Equal strings don't match!
by CountZero (Bishop) on Jul 30, 2008 at 18:12 UTC
|
The usual answer is that they do not match because they are different. Issues of different character sets, "hidden" spaces at the start or the end or EOL characters may mess up your test. Do a print "**$elem**\n"; before the test so you can see what you are comparing.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [Watch: Dir/Any] [d/l] |
|
use Data::Dumper;
local $Data::Dumper::Useqq = 1;
print Dumper($str1, $str2);
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: Equal strings don't match!
by toolic (Bishop) on Jul 30, 2008 at 18:15 UTC
|
Maybe $elem has a trailing newline character. Did you try to chomp @CMD?
Please use "code" tags instead of "pre" tags, and only use them around your code snippets. Your posting renders poorly. Re-read Writeup Formatting Tips. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Equal strings don't match!
by olus (Curate) on Jul 30, 2008 at 18:13 UTC
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Equal strings don't match!
by MidLifeXis (Monsignor) on Jul 30, 2008 at 18:13 UTC
|
I don't have linux available to me right now, but are there newlines at the end of the command lines you are reading? Try the chomp command.
| [reply] [Watch: Dir/Any] [d/l] |
|
No,there aren't newlines at "cmdline" file!No need to "chomp"...
| [reply] [Watch: Dir/Any] |
Re: Equal strings don't match!
by Fletch (Bishop) on Jul 30, 2008 at 18:15 UTC
|
perldoc chomp
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [Watch: Dir/Any] |