Solved

Trailing 1 being inserted?

Posted on 2003-11-06
19
171 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:ZiaTioN
  • 7
  • 3
  • 3
  • +3
19 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 9696694
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;
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9696881
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9696899
think you meant:
  @results = split(/\s+/,$check);
0
 
LVL 5

Author Comment

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

0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9697076
#!/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]$
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9697310
hmm, would be nice if you tell us what you expect, and what is wrong
0
 
LVL 51

Expert Comment

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

Author Comment

by:ZiaTioN
ID: 9697363
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?
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9697370
>> .. 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.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 84

Expert Comment

by:ozo
ID: 9697554
print @results . "\n";
is the same as
print scalar @results ."\n";
you probably wanted
print @results, "\n";
# note , instead of .
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 20 total points
ID: 9698172
@results = print $check;

That line is doing 2 things (3 depending on how you break it down);
   1) prints the value of $check
   2) assigns the value of $check to first element of the @results array
   3) it also adds/appends the failure/sucsess status of the print statement (which is 0 or 1) to the array element.

Since you already have the output of your id command in the $check scalar, I'm not sure why you're assigning it a 1 element array.
0
 
LVL 8

Expert Comment

by:inq123
ID: 9698175
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!
0
 
LVL 8

Expert Comment

by:inq123
ID: 9698181
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.
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9699384
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.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9701698
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. :-)
0
 
LVL 8

Expert Comment

by:inq123
ID: 9701795
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. :-(
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9702756
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?
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9702947
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)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 9711737
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;
 }
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now