perl if elsif statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl if elsif statements
# 1  
Old 09-16-2009
perl if elsif statements

I have having problems with an IF statement in my perl script:

Code:
if ($model eq "N\\A") {}
  elsif ($kernel =~ m/xen/) {
    $model = ("Virtual Machine\n")};

What i am trying to accomplish is if the model is set to "N\A" and the kernel variable has xen somewhere in it i would like to change the variable to "virtual machine". Otherwise just leave it alone. The rest of my script works great this is the last part. The code above does not do what i want it to, it turns every model into a virtual machine regardless if it is set or not.

Thanks,

Sean
# 2  
Old 09-16-2009
Use the and (&&) operator

In your code you're saying if model equals N\A then do nothing or if kernel has xen in it change model to Virtual Machine.

You'll want something similar to the following I think:

Code:
if (($model eq 'N\A') && ($kernel =~ m/xen/)) {
    $model = "Virtual Machine\n";
}

# 3  
Old 09-16-2009
Quote:
Originally Posted by insania
...
What i am trying to accomplish is if the model is set to "N\A" and the kernel variable has xen somewhere in it i would like to change the variable to "virtual machine". Otherwise just leave it alone.
...
Assuming "the variable" means "model" -

Code:
$
$
$ cat insania.pl
#!/usr/bin/perl -w
$model  = $ARGV[0];
$kernel = $ARGV[1];
print "Before if...\n";
print "model  = $model\n";
print "kernel = $kernel\n";
# if the model is set to "N\A" and the kernel variable
# has xen somewhere in it i would like to change the
# variable to "virtual machine".
# Otherwise just leave it alone.
if ($model eq "N\\A" and $kernel =~ m/xen/) {
    $model = "Virtual Machine";
}
print "\nAfter if...\n";
print "model  = $model\n";
print "kernel = $kernel\n";
$
$ # Case 1 : model is "N\A" and kernel has "xen" in it
$
$ perl insania.pl "N\A" abcxenabc
Before if...
model  = N\A
kernel = abcxenabc
 
After if...
model  = Virtual Machine
kernel = abcxenabc
$
$
$ # Case 2 : model is "N\A" and kernel does not have "xen" in it
$
$ perl insania.pl "N\A" abcyenabc
Before if...
model  = N\A
kernel = abcyenabc
 
After if...
model  = N\A
kernel = abcyenabc
$
$
$ # Case 3 : model is not "N\A" and kernel has "xen" in it
$
$ perl insania.pl "M\A" abcxenabc
Before if...
model  = M\A
kernel = abcxenabc
 
After if...
model  = M\A
kernel = abcxenabc
$
$
$ # Case 4 : model is not "N\A" and kernel does not have "xen" in it
$
$ perl insania.pl "M\A" abcyenabc
Before if...
model  = M\A
kernel = abcyenabc
 
After if...
model  = M\A
kernel = abcyenabc
$
$

HTH,
tyler_durden
# 4  
Old 09-16-2009
Sweet, the following code worked:

Code:
  if (($model eq "N\\A\n") && ($kernel =~ m/xen/)) {
    $model = "Virtual Machine\n";
  }

Had to add the \n on the "N\\A" variable because that is how it is coded earlier in the script, i failed to mention that.

Thanks again for both of your help, if they assigned points like the ITRC, i would give you both 10's.

Sean
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Elsif not working in perl

have issue where my elsif is always failing. Basically i have a file with sets of 2 lines, the 1st line that containing "ipwr" and the 2nd line containing a value or "unknown". if the 2nd line contains a value then i want to print the pair of lines. open (INFO, "temp.txt") or die; ... (3 Replies)
Discussion started by: johnny921
3 Replies

2. Shell Programming and Scripting

Perl combine multiple map statements

I have a file like file. file.TODAY.THISYEAR file.TODAY.LASTYEARI want to substitute the words in caps with their actual values so that output should look like file.140805 file.140805.2014 file.140805.2013For this I am reading the file line bye line in an array and using multiple map... (1 Reply)
Discussion started by: sam05121988
1 Replies

3. Shell Programming and Scripting

Too many if statements..

Hello. I am new here and new to scripting. I used to have a very basic script that worked for simple backup/restore of files. I have expanded it and well... I have ended up with a complete mess. It still backs up and restores but there is so many issues that stem from the many if statements I... (3 Replies)
Discussion started by: gameinn
3 Replies

4. Shell Programming and Scripting

Using While and If statements

Hi guys, Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while loop to determine the largest number in the range. ... (2 Replies)
Discussion started by: jjb1989
2 Replies

5. Programming

String if statements - Perl

So far, all I have been able to come up with is: if ($check=~/no/ || $check=~/n/) but the problem with this, is that it looks for any character and the if statement is true. So if I wanted to check for an argument "-help" or lets say a variable string that could be a file name. Then... (2 Replies)
Discussion started by: adelsin
2 Replies

6. UNIX for Dummies Questions & Answers

Help with For Statements

Hi, I am trying to write a for statement that will allow for the ps, who, finger, and date commands to run. Can anyone help? I use Putty. (22 Replies)
Discussion started by: lexydoll87
22 Replies

7. UNIX for Advanced & Expert Users

if statements

This is for a program I have to do to calculate the day of the week. I need to write an if statement that will do the following: if day is 29 and year is odd, don't calculate dayif ( day == 29 && year == ??? )I know how to do it for the day but I don't know how to do it for the year. (4 Replies)
Discussion started by: pwanda
4 Replies

8. Shell Programming and Scripting

Bourne Shell: if elsif question

Hi All, Must be something obvious I am missing, but the simple script below doesn't work. #!/bin/sh x=4 if then echo "x is $x" elsif then echo "x is greater than 4" else echo "x is less than 4" fi When I run this script, I get the error message: 7: Syntax error... (3 Replies)
Discussion started by: leostar_10
3 Replies

9. UNIX for Dummies Questions & Answers

Else in If Statements

Sorry to be a pain, but how does the else work in the if statements? Ive been making scripts with if statements but i cant get the else statements working. Can you help? (8 Replies)
Discussion started by: chapmana
8 Replies

10. Shell Programming and Scripting

Perl - backticks v system in if statements

Can someone explain the difference between backticks and system when evaluated in these if statements: sub getDate { print "start date\n"; if ( system("/bin/date") ) { print "can't get date\n"; exit(2); } print "finish date\n"; } Returns the following: start date Thu... (5 Replies)
Discussion started by: gjkeenan
5 Replies
Login or Register to Ask a Question