Link to home
Start Free TrialLog in
Avatar of ZiaTioN
ZiaTioN

asked on

Trailing 1 being inserted?

I was messing around with the beginnings of a simple script and noticed early one that the output I expected to see whas there but it has a trailing "1" inserted on the end. Why is this 1 being added and how do I get rid of it?

[code]
#!/usr/bin/perl

use strict;
my($check, @results);

chomp($check = `id -g`);

print $check . "\n";

@results = print $check;

print @results . "\n";
[/code]

if you are a normal user it is suppose to report back:

500
500

but it does not. This is what I get back:

500
5001

Has an extra 1 on the end.
Avatar of Tintin
Tintin

Not sure what you are trying to do with the assignmnet

@results = print $check;

If you want to check the success of the id call, use

my $check = `id -g` or die "Can not run id command $!\n";

If you want to add the value of $check to the @results array, use

push @results,$check;
Avatar of ZiaTioN

ASKER

What I am trying to dow ith that call is load the results od the id -g command into the array.

So when I enter "id -g" I want whatever the system returns as the result of that query to be added to that array. I am not sure pushing it is the way to go. As a matter of fact I am pretty sure it is not what I am looking for.

if the system returns my group id as 500 when I "print @results" I want it to print 500. However it does not it prints 5001.
think you meant:
  @results = split(/\s+/,$check);
Avatar of ZiaTioN

ASKER

Still no go. Your suggestion and the push suggestion only load the "1" into the array and not the results of the id command.

Avatar of ZiaTioN

ASKER

#!/usr/bin/perl

use strict;
my($check, @results);

chomp($check = `id -g`);

print $check . "\n";

@results = split(/\s+/,$check);

print @results . "\n";


This is the updated code with the split method.

Here is the output when the script is ran.

[ziation@ToughGuy ziation]$ perl -c access_checker
access_checker syntax OK
[ziation@ToughGuy ziation]$ ./access_checker
500
1
[ziation@ToughGuy ziation]$
hmm, would be nice if you tell us what you expect, and what is wrong
> .. the results of the id command
aha, you want the gid, why not using:
 $g=(split(/\s+/,$())[0]; print $g__
Avatar of ZiaTioN

ASKER

I thought I already did that in my original post to start this thread. Also I thought it would be very clear from looking at the source code but I guess not.

I want the value of the @results array to be what the system returns as a response to the query "id -g".

The response if the screipt is ran by a user with normal user permissions would be 500 but I am getting "1" as the value of the array @results.

Can you not see that in the sample script execution I supplied in my previous post?
Avatar of ZiaTioN

ASKER

>> .. the results of the id command
>aha, you want the gid, why not using:
 >$g=(split(/\s+/,$())[0]; print $g__


I did not use that because to tell you the truth I have no idea what that even does. LOL...

Care to explain it? If it works I would love to use it.
print @results . "\n";
is the same as
print scalar @results ."\n";
you probably wanted
print @results, "\n";
# note , instead of .
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi ZiaTioN,

It seems a simple case (wonder why so many posts?), the 1 is the result of your statement:

print @results . "\n";

Here @results has only one data member, so it's a 1 in scalar context.  Your @results = print $check basically first print 500, and then your print @results . "\n" prints "1\n", thus the line became 5001\n which is what you saw.

Did I miss something?

Cheers!
FishMonger,

Seems you beat me by seconds in this post. :=)  It's funny to see this simple case puzzled two big experts.  No offense, though.
Avatar of ZiaTioN

ASKER

Well you might have not missed something but what I missed was the fact that just assigning the array the print statement would actually print the value which I did not know.

@results = print $check;

This is what I was using just to load the results into the array and did not know that it would print it also.
You probably already figured this out, but if you want to put the value of $check into an array, you'd use either one of these statements.

@results = $check;

push @results, $check;

The first one will will accomplish what you're currently doing.  The second one would be used (in a loop) to build up the array with multiple elements.


inq123,
Yes, this time I was able to post seconds prior to you, but with my current work schedule, I'm not able to spend as much time in here as I would like.  So, you'll probably be beating me more often. :-)
Fishmonger, I found I tend to disagree with you, and this time's no exception. :-)  I don't understand why OP wanted @results = print $check;  But if OP wants the return of print stored in @results, but at the same time do not want the string to be printed actually to STDOUT, I think the closest thing that can be done is to open a filehandle for /dev/null, and print to that filehandle.  But then again, there's no real way of faithfully simulating the return status of print to STDOUT unless you actually print to STDOUT.

BTW, FishMonger, if I want to beat you on posts, I can probably do so these couple days since I'm on vacation at home. That'll be tough for you to beat. :-)  But I also intend to relax a bit so I'm not really very active as I found out time flew so fast when you're on vacation. :-(
Avatar of ZiaTioN

ASKER

Thanks alot for the help guys. I had figured out (with your guys help) already how to push the return value of $check into @results. Now I have a problem when comparing it to the value of another array I have in the script.

Here is the code:

------------------code-----------------------------

#!/usr/bin/perl

use strict;
my($check, @results, @gid, $timestamp, @timestamp);

chomp($check = `id -g`);

if (-e "gid_log") {
   open(file, "<gid_log") || die "Cannot open gid_log";
   @gid = <file>;
   close(file);
   @results = $check;
}else{
   print `touch gid_log`;
   open(file, ">gid_log") || die "Cannot open gid_log";
   @results = print file $check . "\n";
   close(file);
   open(file, "<gid_log") || die "Cannot open gid_log";
   @gid = <file>;
   close(file);
}

if (@results == @gid) {
   print "They are the same!\n";
}else{
   chomp($timestamp = `date`);
   if (-e "notify.txt") {
      open(notify, ">>notify.txt") || die "Cannot open notify.txt";
      @timestamp = print notify "Your access was removed at " . $timestamp . "\n";
      close(notify);
   }else{
      print `touch notify.txt`;
      open(notify, ">>notify.txt") || die "Cannot open notify.txt";
      @timestamp = print notify "Your access was removed at " . $timestamp . "\n";
      close(notify);
   }
}

----------------------end code---------------------------------------------

When I compare @gid to @results to see if they equal each other it gets satisfied and prints the statement "They are the same!" and exits. Now this is suppose to happen when they are the same but if I open up gid_log after it is created by the script anc change the value of the integer inside to say like 5 and run the script @gid should have a value of 5 and@results should have a value of 0 (if ran as root) so the if statement should not be satisfied and should jump to else but it does not. It still prints out the statement saying they are the same when they are not.

Any ideas why?
if (@results == @gid)

That tests if they have the same nunber of elements.  It doesn't look at their values.  Use scalars insread of the arrays.

  $gid = <file>;
  $results = $check;
if ($results == $gid)

or

if ($check == $gid)
Let's rewrite your code to be a more Perl literate.  I've also removed all the redudant code (arrays, if tests etc):

#!/usr/bin/perl
use strict;

my $check = `id -g` or die "Can not run id command $!\n";
my $gid;

if (-e "gid_log") {
   open FILE, "gid_log" or  die "Can not open gid_log $!\n";
   $gid = <FILE>;
   close FILE;
} else {
    open FILE, ">gid_log" or die "Can not open gid_log $!\n";
    print FILE $check;
    close FILE;
}

chomp ($gid,$check);

if ($gid == $check) {
   print "They are the same!\n";
} else {
   open NOTIFY, ">>notify.txt" or  die "Can not open notify.txt $!\n";
   print NOTIFY "Your access was removed at " . localtime() . "\n";
   close NOTIFY;
 }