
sammclean23
Novice
Nov 21, 2012, 1:51 AM
Post #1 of 2
(3101 views)
|
Writing multiple lines - if required
|
Can't Post
|
|
Hi All, The following code I have written takes a user inputted 'service instance' and writes it to a file. Once done, the user is asked whether they wish to delete the 'service instances' and if "Y" is chosen, the next part of the script is executed. Currently, the user is only able to input one service instance at a time (due to the loop) however, I would like to have the option of either pasting a big list of 'services instances' or being able to type one by one how it is now. It is technically possible to paste in data now, it just means the loop will run "x" amount of times until it is all added, however this looks messy and inefficient. Below is the code and how it looks when run, any help is greatly appreciated. When run:
Please type the next service instance, or just press enter to finish... Hi adding service instance Hi... Please type the next service instance, or just press enter to finish... How adding service instance How ... Please type the next service instance, or just press enter to finish... Are adding service instance Are... Please type the next service instance, or just press enter to finish... No more service instances to add... Do you want to run the delete? (Y/N) Y
#!/usr/bin/perl #Variables# my $nullcnt = 1; my $servinst; my $sdate = `date "+%Y%m%d"`; chomp $sdate; my $rad_home_dir = '$RAD_HOME'; my $outdir = "/opt/netcool/rad/scripts/radsh/"; my $outfile = join("", "RAD-Delete-", $sdate, ".radsh"); my $outname = join("", $outdir, $outfile); my $delpath = $outdir; #Clear screen and print message on screen# system("clear"); print "\n"; print "*************** Welcome to RAD Service Instance Auto-Delete ***************\n"; #pause for 1 second# sleep(1); #run sub writeline2# &writeline2; while ($nullcnt) { &readline; &writelines if ($nullcnt); } print "\n"; print "Do you want to run the delete? (Y/N)\n"; my $userconfirm = <STDIN>; chomp ($userconfirm); my $userconfirm = uc($userconfirm); if ($userconfirm eq "Y") { &rundelete } else { print "\nDelete cancelled, no deletes will be made.\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"; $servinst = <STDIN>; chomp ($servinst); if ($servinst ne "") { } else { print "No more service instances to add...\n"; $nullcnt = 0; } } sub writelines { print "adding service instance $servinst...\n"; my $bit1 = 'deleteServiceInstance(new String[] { "'; my $bit2 = '" });'; my $line2 = join("", $bit1, $servinst, $bit2); open (OUTNAME,">>$outname"); print OUTNAME "$line2\n"; close(OUTNAME); } sub writeline2 { my $line1 = 'rad.setUserForSession("root");'; open (OUTNAME,">$outname"); print OUTNAME "$line1\n"; close(OUTNAME); } sub rundelete { print "\n"; print "Changing to directory to run delete script: "; sleep(2); print "$delpath\n"; sleep (2); chdir($delpath) or die "Cant change directory\n"; exec "nohup cat $outfile | $rad_home_dir/bin/rad_radshell > /tmp/deletes.out";
|