Sponsored Content
Top Forums Shell Programming and Scripting Perl: One action if an element doesn't exist in array Post 302487105 by ahmed_zaher on Wednesday 12th of January 2011 03:19:12 AM
Old 01-12-2011
Many thanks for your support.

It works fine with my simple example but I unfortunatly I still don't know how to make it for my actual script.

I want to run some cisco commands after checking that exact number doesn't exist in the following command output:

Code:
FWSMLAN1# sh vlan
203, 300-324 , 326, 329-331 , 333-340 , 342, 344, 346-351 , 353, 355-356 , 370, 388-390 , 399-401 , 404, 515-517 , 629, 658, 830, 834, 838, 843-844 

the commands want to be written if exact number doesn't exist:
Code:
@output = $con->cmd("show running-config interface vlan $vlan_id$");
print join("\n", @output);
my @output_line = split (/\s+/, $output[2]);
if ("$output_line[0]" eq "ERROR:")
{
 @output = $con->cmd("configure terminal");
 print join("\n", @output);
 @output = $con->cmd("interface vlan $vlan_id$");
 print join("\n", @output);
 @output = $con->cmd("no shut");
 print join("\n", @output);
 @output = $con->cmd("nameif $nameif$");
 print join("\n", @output);
 @output = $con->cmd("security-level $sec_level$");
 print join("\n", @output);
 @output = $con->cmd("ip address $IP$ $mask$ standby $standby_IP$");
 print join("\n", @output);
 @output = $con->cmd("exit");
 print join("\n", @output);
}


and here is my script:

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Opsware::NAS::Connect;
 
my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$');
my @output;
my $con = Opsware::NAS::Connect->new(-user => $user, -pass => $pass, -host => $host, -port => $port);
$con->login();
 
$con->connect( '192.168.15.15' ) or die "Failed to connect.";
$con->cmd("terminal length 0");
@output = $con->cmd("show vlan");
print join("\n", @output);
my @vlan = split (/,/, $output[1]);
for (my $i=0; $i <= $#vlan; $i++)
{
 if ( "$vlan[$i]" =~ /(\d+)-(\d+)/ )
 {
  print "start: $1, end: $2\n";
  for (my $j=$1; $j <= $2; $j++)
  {
   if ($j eq $vlan_id$)
   {
    print "vlan $j found\n";    
   }
  }
 }
 else
 {
  if ($vlan[$i] eq $vlan_id$)
  {
   print "vlan $vlan[$i] found\n";
  }
 }
} 
 
@output = $con->disconnect();

Appreciate your advise.
Thanks in advance,
Ahmed

---------- Post updated 01-12-11 at 03:19 AM ---------- Previous update was 01-11-11 at 11:42 AM ----------

Hello,

I think I make you confused.

in simple words,
I want to run multiple commands when "string" is member of array and then
run other multiple commands when "string" is NOT member of array.

Thanks in advance,

Appreciate your reply,
Ahmed
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What hapens if a group member doesn't exist?

As part of a NIS implementation, (I think) I want to create a group (in /etc/group) that has users that do not exist on the target machine. What effect will this have? Will it cause any problems? Thanks, Gary Cooper (1 Reply)
Discussion started by: Gary Cooper
1 Replies

2. Shell Programming and Scripting

Perl delete an element from array

Probably I am not seeing it or I am not using the "delete" correctly I had the following codes but it does not work for me #!/bin/perl -w ... @sysFile1 = (a_b, a_c, a_d); @sysFile2 = (a_c, a_e, b_f); foreach $line1 (@sysFile1){ trim(\$line1); (my $tmp1, my $tmp2) = split/_/,... (6 Replies)
Discussion started by: ahtat99
6 Replies

3. Shell Programming and Scripting

Remove an element from an array in PERL

Hello guys, I have the following question. Consider the following code in PERL for($xeAnumber=0; $xeAnumber<@xeAnumber; $xeAnumber++) { if(@xeAnumber==@final_file) { @final_file=@xeTimeStamp; }... (3 Replies)
Discussion started by: chriss_58
3 Replies

4. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

5. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

6. Shell Programming and Scripting

previous element in the array perl

Hi, How to get previous/next element in the array perl Example @queue = (1, 2 ,3 , 4); I want to get value of 1 and 2, or, 2 and 3, or 3 and 4...etc and compare to value which one is greater to do that I need to get previous and next element of array ? (1 Reply)
Discussion started by: guidely
1 Replies

7. Solaris

User directory doesn't exist

Hii all, i create the user useradd -d /home/kk kk passwd kk when i tried to login to kk i get a error user directory doesn't exist then i tried useradd kkk passwd kkkwhen i tried to login to kkk i get the same error user directory doesn't exist. (4 Replies)
Discussion started by: vipinkumarr89
4 Replies

8. Shell Programming and Scripting

Perl: How to check whether my array contains element x

Hi All, I am new to perl I am stuck in simple problem I need your help I want to define a subroutine. sub check_if_entity_exists(@array_to_be_checked,$entityName) I have array as http-listener-1 http-listener-2 http-listener-3 http-listener-4 If i send http-listener-3 my... (1 Reply)
Discussion started by: javaholics
1 Replies

9. Shell Programming and Scripting

Perform an action if certain text exist in output (PERL)

Hello, I'm attempting to write a tool that checks an IP address for existing PTR records then if there are no PTR records does a ping to see if it response. Then if there is no response, it should print a message saying This is what I have so far. #!/usr/bin/perl $nxdomain =... (4 Replies)
Discussion started by: spartan22
4 Replies

10. Homework & Coursework Questions

Group Doesn't Exist

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I'm able to create a group but when I'm trying to delete the group it keeps stating Group Doesn't Exist. I know... (2 Replies)
Discussion started by: GoBoyGo
2 Replies
array(n)						       Tcl Built-In Commands							  array(n)

__________________________________________________________________________________________________________________________________________________

NAME
array - Manipulate array variables SYNOPSIS
array option arrayName ?arg arg ...? _________________________________________________________________ DESCRIPTION
This command performs one of several operations on the variable given by arrayName. Unless otherwise specified for individual commands below, arrayName must be the name of an existing array variable. The option argument determines what action is carried out by the command. The legal options (which may be abbreviated) are: array anymore arrayName searchId Returns 1 if there are any more elements left to be processed in an array search, 0 if all elements have already been returned. SearchId indicates which search on arrayName to check, and must have been the return value from a previous invocation of array startsearch. This option is particularly useful if an array has an element with an empty name, since the return value from array nextelement won't indicate whether the search has been completed. array donesearch arrayName searchId This command terminates an array search and destroys all the state associated with that search. SearchId indicates which search on arrayName to destroy, and must have been the return value from a previous invocation of array startsearch. Returns an empty string. array exists arrayName Returns 1 if arrayName is an array variable, 0 if there is no variable by that name or if it is a scalar variable. array get arrayName ?pattern? Returns a list containing pairs of elements. The first element in each pair is the name of an element in arrayName and the second element of each pair is the value of the array element. The order of the pairs is undefined. If pattern is not specified, then all of the elements of the array are included in the result. If pattern is specified, then only those elements whose names match pat- tern (using the matching rules of string match) are included. If arrayName isn't the name of an array variable, or if the array contains no elements, then an empty list is returned. array names arrayName ?pattern? Returns a list containing the names of all of the elements in the array that match pattern (using the matching rules of string match). If pattern is omitted then the command returns all of the element names in the array. If there are no (matching) elements in the array, or if arrayName isn't the name of an array variable, then an empty string is returned. array nextelement arrayName searchId Returns the name of the next element in arrayName, or an empty string if all elements of arrayName have already been returned in this search. The searchId argument identifies the search, and must have been the return value of an array startsearch command. Warning: if elements are added to or deleted from the array, then all searches are automatically terminated just as if array done- search had been invoked; this will cause array nextelement operations to fail for those searches. array set arrayName list Sets the values of one or more elements in arrayName. list must have a form like that returned by array get, consisting of an even number of elements. Each odd-numbered element in list is treated as an element name within arrayName, and the following element in list is used as a new value for that array element. If the variable arrayName does not already exist and list is empty, arrayName is created with an empty array value. array size arrayName Returns a decimal string giving the number of elements in the array. If arrayName isn't the name of an array then 0 is returned. array startsearch arrayName This command initializes an element-by-element search through the array given by arrayName, such that invocations of the array nex- telement command will return the names of the individual elements in the array. When the search has been completed, the array done- search command should be invoked. The return value is a search identifier that must be used in array nextelement and array done- search commands; it allows multiple searches to be underway simultaneously for the same array. | array unset arrayName ?pattern? | Unsets all of the elements in the array that match pattern (using the matching rules of string match). If arrayName isn't the name | of an array variable or there are no matching elements in the array, then an empty string is returned. If pattern is omitted and is | it an array variable, then the command unsets the entire array. KEYWORDS
array, element names, search Tcl 8.3 array(n)
All times are GMT -4. The time now is 05:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy