Use the same perl script with several different variables
I'm new to perl and have to create these files for my job. I have a script that I can run that will create the file, but I have to manually pass the variables to it. It is dumb and it takes a lot of time. I want to be able to pass the existing perl script a set of variables (the same variables with different values) over and over again, until it runs through them.
Basically, I want to keep a file with the variables:
$variable1
$variable2
$variable3
run script with these > output to a file
$variable1
$variable2
$variable3
run script with these > output to a file
so say you put your variables in space-delimited lines in a file name 'in.txt'. The content of this file for example is:
you could invoke your script 'script.pl' like this:
as you may know, in.txt is passed into the script in the default array ARGV.
inside your script, you would have something like this, then, to create file handles to open the input file, and a second arg for the output file
then you would read the input file line by line, splitting each line on spaces into an array, to pass to your processing function for output:
the output of the script in this example is:
if you want to output to multiple files, you could either pass in multiple args, or name your file with timestamps or something else programmatically.
Last edited by varontron; 10-03-2009 at 12:27 AM..
I've tested this and it works, but I'm not able to put variables in my input file and have them read as variables. I'm not sure if that makes sense, but if I declare my variables in my input file, the other script doesn't recognize them as a variable, only a string.
If you're declaring variables in the first script and then calling the second from it, you need to use some sort of persistent memory, which is probably way overcomplicated. the second perl script has no knowledge of the memory used by it's caller.
alternatively you want to declare vars in the second script to hold the vals you need, and pass those values from the first script to it. you can do this through arguments, pipes, files, etc.
I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open())
I would like to run another PERL-script from first one, not... (1 Reply)
Hi Chaps,
Im after some advise with a script i've written however doesnt appear to work how I would like.
Basically I have a perl script which sucessfully pulls an expect script to login to multiple nodes and obtain some output from the nodes (total number of nat ports that are in use... (0 Replies)
Not sure what I am doing wrong here, but I can print the list with no issue. Just a blank screen with the 'do'.
#!/usr/bin/perl
open FILE, "upslist.txt";
while ($line=<FILE>){
if ($line=~/^(.*?),(.*?)$/){
#print "ups:$1 string:$2\n";
do 'check_snmp_mgeups-0.1.pl -H $1 -C $2';
} ... (1 Reply)
Hi All,
i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way??
#!/bin/ksh
##script is sample.ksh
age=$1
gender=$2
class=$3
.
.
.... (3 Replies)
I don't fully understand variables in perl.
If we have a variable defined like this "my $number = 1" then this is called a lexical variable? But if you define this at the top of a script then why isn't it a global variable because it would be available throughout the file?
Sorry if this is... (1 Reply)
I have a perl script that opens a text file containing numbers on each line:
for example:
755993
755994
755995
755996
755997
755998
The perl script takes these numbers and store them as an array @raw_data, where I can access individual numbers by using $raw_data for the value 755993.... (2 Replies)
Hi.
I have three arrays.
@a=('AB','CD','EF');
@b=('AB,'DG',HK');
@c=('DD','TT','MM');
I want to compare the elements of the first two array and if they match then so some substition.
I tried using the if statement using the scalar value of the array but its not giving me any output.
... (7 Replies)
I'm writing a script to automate some post-install tasks on RHEL4 servers.
I need the following code to insert an 'A' in the middle of a string, then replace the string in a file. I know I can use sed to do this, but I'd like to use perl's in place edit so I don't have to write to a temp file,... (1 Reply)
Hi all,
I have a perl script which declares two variables and calls another perl script which accesses those variables. But I am unable to access the variables in the called script. My script is as follows:
my $ENV{a}="20";
system("perl called.pl");
and my called.pl contains:
print... (3 Replies)