perl if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl if statement
# 1  
Old 12-25-2004
perl if statement

Hello guys,

I was wondering why my code doesn't read a variable when using if statement as follows:
Code:
$userlist='users.txt';
$user='b999';
                open (ACTULOG, ">>$userlist");
                print ACTULOG "$user\n";
                close (ACTULOG)

this will work and prints b999 into users.txt file
BUT
Code:
$userlist='users.txt';
$user='b999';
$grepinfo=`/usr/bin/grep -w $user $userlist`;
        if ($user = $grepinfo) {
       exit ;
        } else {
                open (ACTULOG, ">>$userlist");
                print ACTULOG "$user\n";
                close (ACTULOG)
                }

wont work and it will prints empty line into users.txt if b999 does not exists! it should print b999 into the file if doesn't exists and if exists it will exit safely.

any idea why the line print ACTULOG "$user\n"; isn't possible to read the $user variable?


Thanks.
# 2  
Old 12-25-2004
You assigned to $user in the "if" test:

if ($user = $grepinfo) {

If the grep fails, no output is dumped to $grepinfo, which got assigned to $user. So you get an empty line when you print() it.
# 3  
Old 12-25-2004
Quote:
if ($user = $grepinfo) {
Basically the above if stmt is checked against 'true' always .....
as u are assigning grepinfo value to user.


so .. evry time u are 'exit' ing out ....;


Do string comparision using 'eq' opearaor in perl.

Check the following link .....

http://www.pageresource.com/cgirec/ptut7.htm
# 4  
Old 12-25-2004
i was able to change the if statement to
if ( $grepinfo =~ /$user/ ) and worked just fine Smilie


Thanks for the advice guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - what does this statement mean -Regex

push @MACARRAY, "$+{catalog} $+{machine}\n" if ($info =~ /(?<catalog>catalog).+?(?<machine>\*+)/ms); I am (still) trying to solve problem. Looking around on the server I found this piece of code. Specifically what does "$+{catalog} $+{machine}\n" do ? Thanks in advance (1 Reply)
Discussion started by: popeye
1 Replies

2. Shell Programming and Scripting

Understanding perl statement

can someone help me how to interpret this line? my ($class, $hashref) = @_; my $portfolio = {}; if ($hashref->{portfolio_id}) { ($portfolio) = GEmySQL->get ("select * from portfolio where portfolio.id=$hashref->{portfolio_id}"); } =============== Question: how do... (2 Replies)
Discussion started by: onlinelearner02
2 Replies

3. UNIX for Dummies Questions & Answers

Help with Perl If statement

Hi All, I am writing a perl script where I take 2 variables from the user as STDIN to scan the lines of a file to produce as output. How can I do an IF loop to test this for example in the mock file 12 10 35 20 37 5 45 12if I take user input as 40 and 10, how can I get the output lines in... (3 Replies)
Discussion started by: pawannoel
3 Replies

4. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

5. Shell Programming and Scripting

Condition statement in perl

#!/usr/bin/perl $output1 = "/home/log.txt" $output2 = "/home/grep.txt" #Statement1 creates an output file called log.txt. #Statement2 greps a line from log.txt and store the result in grep.txt I want to create a condition where if the file grep.txt is empty repeat process. Thanks. (1 Reply)
Discussion started by: sureshcisco
1 Replies

6. Shell Programming and Scripting

Perl - automating if statement test

Hello all, I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file. There are approximately 200 different input files. Each has... (3 Replies)
Discussion started by: Galt
3 Replies

7. Shell Programming and Scripting

help with if statement perl scripting

Sorry for the newbie question I need to create a perl script to check the modifed time of a file I already have that part created but need help in creating a if statement that basicly will check to see if the modifed time is at least 2hrs older then the current time if the stament is true to... (2 Replies)
Discussion started by: nettech207
2 Replies

8. Shell Programming and Scripting

Help with perl and if file statement

Hello and thank you in advance. I have a perl script that will basically copy files out of a directory , creat a new current date stamped dir and move them that current time stamped dir and append the file names with current time stamp. I need to add line(s) that this is only run if FileX... (1 Reply)
Discussion started by: Dadwith2boys
1 Replies

9. Shell Programming and Scripting

Diamond operator in Until Statement (perl)

Hello: I have the following perl script which is giving me trouble inside the second elsif statement. The purpose of the script is to go through a file and print out only those lines which contain pertinent information. The tricky part came when I realized that certain items actually spanned... (5 Replies)
Discussion started by: erichpowell
5 Replies

10. Shell Programming and Scripting

Problem with if statement in perl

if (($fields eq $hwp) && ($fields eq 'Y')) { $fields = "INTEGRAL"; } elsif ($fields eq $hwp) { $fields = "INTEGRAL"; } elsif ($fields ne $hwp) { $fields = "SEPARATE"; } print "$fields $fields $fields\n"; Output: The problem here is that the first... (2 Replies)
Discussion started by: kamitsin
2 Replies
Login or Register to Ask a Question