john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Perl tutorial OLD

Perl shell

Perl binary (shell?) can be at /usr/local/bin/perl or /usr/bin/perl

first.pl
#!/usr/local/bin/perl
# comment that perl can be a script language
# \\ is escape character for a single slash, \t , \n
print "Hi \\ Bye \t Bye!\n";
print 'Single Quote literal string\n ignores escapes & variables';

scalars, arrays and hashes

` is the funny tick symbol in linux

 $route = `/bin/netstat -rn`;
  print "$route\n";


   print "** Any key to return **";
  $data = <STDIN>;



 **  = exponentiation
 . = concatentation ?

 eq = string equality
 ne = string inequality
 lt , gt , le , ge = string comparisons
 cmp = returns -1 , 0 , 1

Perl (prior to 5.8.x) does not have a "switch case" statement...

So you have to add a module (and line in your code)

1
2
#!/usr/bin/perl
use Switch;

#compiled perl?
chmod u+x progname
perl progname
./progname
progname

perl -w progname (-w for warnings, -d for debugger messages)

while( $inputtest ne $exitchoice )
{
    print "\nenter the digit 0 to quit\n"; 
    chomp( $inputtest = <STDIN>);
    print ":";
    print "\n$inputtest\n";
}

foreach $morsel (@food)     # Visit each item in turn
                # and call it $morsel
{
    print "$morsel\n";  # Print the item
    print "Yum yum\n";  # That was nice
}

 #remove newlines (e.g. the enter the user has to press on the keyboard)
       chomp( $input = <STDIN>);
        print ("You entered -> $input <- \n");
        switch ( $input )
        {
                case "1"
                {
                        changeip();
                        break;
                }
                else
                {   print "else stuff";   }

         }

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/perl


# default is no SSH, should time out or reset to denied afer reboot
sub allowssh
{
    $command = "/sbin/iptables";
    $arg = " -L -n -v --line-numbers";
    @args = split(" ",$arg);
    $result = system $command,@args;

    $result = system $command;
}

sub writeconfigfile
{
 open (F,">/etc/network/interfaces");
  print F "address $ip\n";

  close (F);
}


  $cmd = "/usr/bin/wget $URL -O /tmp/tempoutput > /dev/null 2>&1";
  system $cmd;

  $size = -s "/tmp/leapfile";
  if ($size > 0) {
        print("passed.\n");
  } else {
        print("failed!\n");

  • « Amazon ssh mysql rds notes
  • Vmware rpm wget screen »

Published

Mar 4, 2011

Category

bat-vbs-perl

~295 words

Tags

  • bat-vbs-perl 51
  • old 1
  • perl 14
  • tutorial 6