Anyone good with Perl?

Currently reading:
Anyone good with Perl?

Joined
Apr 7, 2011
Messages
3,555
Points
716
Location
Rotherham
Just got a quick query regarding a perl script.

basically I have this line and want to allow the user to enter multiple values

Code:
print "please type the service instance...\n";
my $servinst = <STDIN>;

EDIT: worked it out it can be done like this:

Code:
#!/usr/bin/perl 
print "Enter service instances separated by comma\n";
my $servinst = <STDIN>;
chomp $servinst;
my @hosts = split(/\,/, $input);
foreach(@hosts) {
print "Anything"\n";
 
}

but no idea how i can implement this into my script :(
 
Last edited:
You've asked and then answered it yourself.. what do you mean you don't know how to implement it in to the rest of the script. What are you doing with the values? You'll have them all in @hosts.. that's fine, do with them what you need?
 
sorry not gave much context,

I've decided i don't like the way iv done it above and wish to do in a loop i.e. keeps asking the user to input a value until a special key is pressed.

The full script I wish to run for each inputted $servinst is:

Code:
print "please type the service instance...\n";
 
my $servinst = <STDIN>;
chomp ($servinst);
print "service instance>>> $servinst\n";
 
my $bit1 = 'grantPrivilegeToAllChildren(GROUP, "NHS_SPINE_CSI", "';
my $bit2 = '", "RAD_VIEW_INSTANCE");';
 
print "$bit1\n";
print "$bit2\n";
 
my $line1 = join("", $bit1, $servinst, $bit2);
print "$line1\n";
 
my $bit1 = 'grantPrivilegeToAllChildren(USER, "nhs_spine_admin", "';
my $bit2 = '", "RAD_INSTANCE_ADMIN");';
 
print "$bit1\n";
print "$bit2\n";
 
my $line2 = join("", $bit1, $servinst, $bit2);
print "$line2\n";
 
my $sdate = `date "+%Y%m%d"`;
chomp $sdate;
 
my $outfile = join("", "/opt/netcool/rad/scripts/radsh/RAD-Privs-", $sdate, ".radsh");
 
#open (OUTFILE,">>/opt/netcool/rad/scripts/radsh/RAD-Privs-today");
open (OUTFILE,">>$outfile");
print OUTFILE "$line1\n";

not sure about the loop though :(
 
keeps asking the user to input a value until a special key is pressed.

Code:
print "please type the service instance...\n";
my $servinsts = <STDIN>;

That should actually do what you're asking. When hit enter, it'll insert a newline char - which will cause the input to skip onto the next array element. Input capture will cease when you enter and EoF (End of File) char - typically Control+D on *nix).

Code:
stty -a

Will confirm your eof.

edit; then just loop through each element in $servinsts


Code:
print "please type the service instance...\n";
my $servinsts = <STDIN>;

foreach $servinst (@servinsts) {

chomp ($servinst);
print "service instance>>> $servinst\n";
 
my $bit1 = 'grantPrivilegeToAllChildren(GROUP, "NHS_SPINE_CSI", "';
my $bit2 = '", "RAD_VIEW_INSTANCE");';
 
print "$bit1\n";
print "$bit2\n";
 
my $line1 = join("", $bit1, $servinst, $bit2);
print "$line1\n";
 
my $bit1 = 'grantPrivilegeToAllChildren(USER, "nhs_spine_admin", "';
my $bit2 = '", "RAD_INSTANCE_ADMIN");';
 
print "$bit1\n";
print "$bit2\n";
 
my $line2 = join("", $bit1, $servinst, $bit2);
print "$line2\n";
 
my $sdate = `date "+%Y%m%d"`;
chomp $sdate;
 
my $outfile = join("", "/opt/netcool/rad/scripts/radsh/RAD-Privs-", $sdate, ".radsh");
 
#open (OUTFILE,">>/opt/netcool/rad/scripts/radsh/RAD-Privs-today");
open (OUTFILE,">>$outfile");
print OUTFILE "$line1\n";
}
 
Last edited:
the existing code asks for a service instance as above, but upon entering one and pressing enter, the script will run and end and therefore the script has to be run again to add another service instance.

so although it looks like it should do what im asking, it really doesnt :(
 
erm..im on the server using 'PuTTY', the script is written in perl on a UNIX system. Thats all I know mate.

just seen your edit, cheers mate i thought it was a foreach loop.

Now i just need it to read each input seperately by repeating 'please type service instance'

Found this that does something similar but no idea how i'd modify to suit my code:

Code:
Loop Method (keeps going until Q is pressed)
 
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Term::ReadKey;
 
sub display_a {
print "I am in display_a\n";
}
sub display_b {
print "I am in display_b\n";
}
my $dispatch_for = {
a => \&display_a,
b => \&display_b,
q => sub { ReadMode('normal'); exit(0) },
};
while ( 1 ) {
print "Press a key!\n";
ReadMode('cbreak');
my $char = ReadKey(10);
defined $dispatch_for->{$char} && $dispatch_for->{$char}->();
}

this is the problem when you arent an expert on how to create scripts but try modifying one lol
 
Last edited:
It should repeat though,

Code:
my $servinsts = <STDIN>;

that should create an array, and wait for input until you do an eof.

and then the loop loops through that array doing all the things you're needing to do.

What did stty -a output?

EDIT - MY BAD


Code:
my [USER=108197]ser[/USER]vinsts = <STDIN>;

SORRY!
 
Last edited:
like this mate (ignore the comments, it's just ideas as i went along, and ignore the spacing - the format seems to reset)

it works using the count to decide what to do

Code:
#!/usr/bin/perl
 
#print "please type the next service instance,\n";
#print "or just press enter to finish...\n";
 
my $nullcnt = 1;
my $servinst;
my $sdate = `date "+%Y%m%d"`;
chomp $sdate;
my $outfile = join("", "RAD-Privs-", $sdate, ".radsh");
 
#open (OUTFILE,">>$outfile");
 
system("clear");
print "\n";
print "*** PRIVS-O-MATIC ***\n";
 
while ($nullcnt) {
&readline;
&writelines if ($nullcnt);
                      }
 
[URL="https://www.fiatforum.com/usertag.php?do=list&action=hash&hash=close%28OUTFILE%29"]#close(OUTFILE)[/URL]
 
print "\n";
print "***Now need to run:-\n";
print "cd /opt/netcool/rad/scripts/radsh\n";
print "nohup cat $outfile | $RAD_HOME/bin/rad_radshell > /tmp/privs.out &n\n";
print "\n";
 
# sub-routines:-
sub readline {
print "\n";
print "please type the next service instance,\n";
print "or just press enter to finish...\n";
 
#my $servinst = <STDIN>;
$servinst = <STDIN>;
chomp ($servinst);
 
if ($servinst ne "") {
# print "valid service instance>>> $servinst\n";
}
else {
print "no more service instances to add...\n";
 #print "no service instance>>> $servinst\n";
    #close(OUTFILE);
    #exit
    $nullcnt = 0;
  }
}
 
sub writelines {
print "adding service instance $servinst...\n";
 
#  print ">>> writing lines...\n";
  my $bit1 = 'grantPrivilegeToAllChildren(GROUP, "NHS_SPINE_CSI", "';
  my $bit2 = '", "RAD_VIEW_INSTANCE");';
 
#  print "$bit1\n";
#  print "$bit2\n";
 
  my $line1 = join("", $bit1, $servinst, $bit2);
#  print "$line1\n";
 
Last edited:
Back
Top