Sponsored Content
Full Discussion: perl if elsif statements
Top Forums Shell Programming and Scripting perl if elsif statements Post 302353960 by durden_tyler on Wednesday 16th of September 2009 04:29:57 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PPI::Statement::Compound(3)				User Contributed Perl Documentation			       PPI::Statement::Compound(3)

NAME
PPI::Statement::Compound - Describes all compound statements SYNOPSIS
# A compound if statement if ( foo ) { bar(); } else { baz(); } # A compound loop statement foreach ( @list ) { bar($_); } INHERITANCE
PPI::Statement::Compound isa PPI::Statement isa PPI::Node isa PPI::Element DESCRIPTION
"PPI::Statement::Compound" objects are used to describe all current forms of compound statements, as described in perlsyn. This covers blocks using "if", "unless", "for", "foreach", "while", and "continue". Please note this does not cover "simple" statements with trailing conditions. Please note also that "do" is also not part of a compound statement. # This is NOT a compound statement my $foo = 1 if $condition; # This is also not a compound statement do { ... } until $condition; METHODS
"PPI::Statement::Compound" has a number of methods in addition to the standard PPI::Statement, PPI::Node and PPI::Element methods. type The "type" method returns the syntactic type of the compound statement. There are four basic compound statement types. The 'if' type includes all variations of the if and unless statements, including any 'elsif' or 'else' parts of the compound statement. The 'while' type describes the standard while and until statements, but again does not describes simple statements with a trailing while. The 'for' type covers the C-style for loops, regardless of whether they were declared using 'for' or 'foreach'. The 'foreach' type covers loops that iterate over collections, regardless of whether they were declared using 'for' or 'foreach'. All of the compounds are a variation on one of these four. Returns the simple string 'if', 'for', 'foreach' or 'while', or "undef" if the type cannot be determined. TO DO
- Write unit tests for this package SUPPORT
See the support section in the main module. AUTHOR
Adam Kennedy <adamk@cpan.org> COPYRIGHT
Copyright 2001 - 2011 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.18.2 2011-02-25 PPI::Statement::Compound(3)
All times are GMT -4. The time now is 06:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy