Passing arguments to a Perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing arguments to a Perl script
# 1  
Old 12-22-2004
Passing arguments to a Perl script

I am playing around with Perl and wrote the script below that is executed from the command line, it will split data up in a file based on a value supplied. When executed you provide two arguments - the file that contains the data to be split and the character you want to split by. It works as intended, but the "split value" has to be surround by double quotes for it to work. It is a minor annoyance, but I would like to figure out how to have this work without having to use the double quotes. Any help would be appreciated. Thank you. Here is the script:
Code:
Code:
#!/usr/local/bin/perl
chomp($which_file=@ARGV[0]);
chomp($split_val=@ARGV[1]);
if (length($which_file)==0||length($split_val)==0)
   {print "You must provide a file and split value!\n";}
else
  {
   if (!-e $which_file)
      {print "$which_file does not exist!\n";}
   else
      {
       open (spfile,"<$which_file") or die ("Cannot open $which_file");
       $temp_var=<spfile>;
       close spfile;
       @split_arr=split(/$split_val/,$temp_var);
       $new_file=$which_file.".split";
       open (newf,">$new_file") or die ("Cannot open $new_file");
       foreach  $split(@split_arr){
                print newf "$split\n";
       }
       close newf;
      }
  }

I execute it like so: ./law_split file_name "split value"
This is used on a UNIX box running IBM's AIX. Perl version 5.005_02 built for aix
# 2  
Old 12-22-2004
I have few things here ....

1).
Quote:
foreach $split(@split_arr){
print newf "$split\n";

In the above ..... u do n't need "newf" at all ....



2)Also ... yr code assumes a file containing a single line.
your code does n't work for file having muliple lines.


3) Have these changes and run yr code again ...
i have no issues in getting the result.
# 3  
Old 12-22-2004
Code:
chomp($which_file=@ARGV[0]);
chomp($split_val=@ARGV[1]);

This should read $ARGV[0] etc. instead. If you have the -w flag on, it will give you a warning. And I don't think you need a chomp() on command line arguments anyway.

Putting a pair of quotes around the value is required by the shell. You can probably workaround it by

$split_val = join(' ', @ARGV[1..(@ARGV-1)]);

to have Perl absorbs all command line parameters delimited by whitespace and glues them into a single string, but the reconstructed string may not be identical to the original string you intended to get. Who says that one space is always the delimiter? For instance by giving the following as command line arguments

Code:
a  b c

Note the two spaces between a and b, which are treated as one whitespace delimiter by the shell and the code above transforms it to (thus the value of $split_val)

Code:
a b c

This arrangement is probably okay if the arguments are like a composite command, and whitespace is not significant. But literally, the string has been modified, and this is unlikely to be what you want.

So use the quotes to avoid such problems.

Last edited by cbkihong; 12-22-2004 at 11:06 PM..
# 4  
Old 12-23-2004
Thank you both for your responses. I will take a look at your suggestions and make the appropriate changes. As you can tell I am just learning how to work with Perl so your help is appreciated. Merry Christmas!
# 5  
Old 12-29-2004
I actually prefer to pass arguments to Perl scripts using the -S option for perl. An example of a program which accepted and checked two arguments would look like this:

#! /bin/perl -s

# Check Arguments
if (!$input || !$char) {
print STDERR "Error: invalid arguments.\nUsage: script.pl -input=<file name> -char=<split char>\n";
exit(1);
}

...

Just how I prefer to do it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing arguments while running the script

Hi, I have a requirement for creating a MQ (queue) where the inputs has to be passed as arguments. Running the script as below ./hi.sh "Servername" "QueueManagername" "QueuecreationCommand" cat hi.sh echo "Welcome to $1" runmqsc $2 < $3 But the queue creation command is... (9 Replies)
Discussion started by: Anusha M
9 Replies

2. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

3. Shell Programming and Scripting

Problems passing shell arguments to perl

Semi-newbie, so flame throwers to 'singe-only', please. ;-) I have a large number of (say) .html files, where I'd like to do a recursive in-place search and replace a particular string. The following bit of perl works fine: perl -pi -e 's/oldstring/newstring/g' `find ./ -name *.html` ... (2 Replies)
Discussion started by: johnny_canucl
2 Replies

4. Shell Programming and Scripting

Passing arguments to a perl script

Hi I need to pass comma seperated arguments to a perl script? It is like: Exect.pl -d GUI1,GUI2,GUI3 and I need to store these argsGUI1,GUI2,GUI3 in an array. can anyone suggest how to do that: (1 Reply)
Discussion started by: rkrish
1 Replies

5. Shell Programming and Scripting

passing arguments to external script

Hi! I have a python script that requires arguments and these arguments are file paths. This script works fine when executed like this: /my_python_script "file_path1" "file_path2" (i added quotes as some file names may have weird characters) the issue happens when i launch my python script... (14 Replies)
Discussion started by: gigagigosu
14 Replies

6. Shell Programming and Scripting

passing arguments to sql script

Hi Gurus, i have one requirement in unix script, i have a file called abc.txt in that few lines are there with the empid, i need to read each line and pass to .sql script. ex: abc.txt 2345 2346 1243 1234 i need to pass these arguments to .sql script rom unix ex: select * from... (1 Reply)
Discussion started by: Devendar
1 Replies

7. Shell Programming and Scripting

Passing arguments to Perl Function

Hi All, I am trying to pass an argument called "Pricelist" to a Perl function, then the function will open and print out the contents of the file named "Pricelist". But i can't seem to do it using my below code. Can any expert give some advice? #!/usr/local/bin/perl $DATABASE =... (1 Reply)
Discussion started by: Raynon
1 Replies

8. Shell Programming and Scripting

problem passing arguments to script

Hi, I am writing a script, which is invoked from other system using ssh. I have problems reading the arguments passing to the script. If the argument has a space in it (ex "rev 2.00"), the script considers "rev" as 1 argument and "2.00" as another. Instead i want "rev 2.00" to be considered... (5 Replies)
Discussion started by: cjjoy
5 Replies

9. UNIX for Advanced & Expert Users

Passing blank arguments to a script

All, I have a cron job script that receives several command line arguments. At some point if there are validation problems and the job cannot be run, it duplicates the entire command line into a temporary text file which is later executed as a script. Unfortunately when I pass the list of received... (7 Replies)
Discussion started by: rm-r
7 Replies

10. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies
Login or Register to Ask a Question