Passing command line arguments

------_=_NextPart_001_01C54075.E7225C28
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

I have a script that is reading input from ARGV.  The script is being
passed a file name as follows:
=20
datefile.pl c:\program files\IBM\SQLLIB\DB2\db2diag.log
=20
The problem I am running into is that the space is not recognized in the
argument.  All that I get passed to is is c:\program    .  How do I get
the rest of the argument.  Below is a portion of the script.  Thanks.
=20
 #***********************************************
 # check input parameter                        *
 #***********************************************
 $arg_length =3D length($ARGV[0]);
 $arg_lastchar =3D substr($ARGV[0], $arg_length-1, 1);
 $arg_string =3D $ARGV[0] ;
 print "Parameter argument is: $arg_string 
";


------_=_NextPart_001_01C54075.E7225C28--
0
Bret 4/13/2005 10:12:38 PM
📁 perl.beginners
📃 28959 articles.
⭐ 0 followers.

💬 5 Replies
👁️‍🗨️ 53 Views
Subject: Passing command line arguments

I have a script that is reading input from ARGV.  The script is being
passed a file name as follows:
 
datefile.pl c:\program files\IBM\SQLLIB\DB2\db2diag.log
 
The problem I am running into is that the space is not recognized in the
argument.  All that I get passed to is is c:\program    .  How do I get
the rest of the argument.  Below is a portion of the script.  Thanks.
 
 #***********************************************
 # check input parameter                        *
 #***********************************************
 $arg_length = length($ARGV[0]);
 $arg_lastchar = substr($ARGV[0], $arg_length-1, 1);
 $arg_string = $ARGV[0] ;
 print "Parameter argument is: $arg_string 
";


SUN1-BATCH>./foo "1 2 3" 4 5 6
Number of parameters = <4>
1 2 3
SUN1-BATCH>more foo
#!/usr/bin/perl
print "Number of parameters = <" . scalar(@ARGV) . ">
" if (scalar(@ARGV));
print $ARGV[0], "
";
SUN1-BATCH>

Hope this gives you some ideas...

jwm
0
john 4/13/2005 10:18:17 PM
On Wednesday 13 April 2005 11:12 pm, Bret Goodfellow wrote:
> I have a script that is reading input from ARGV.  The script is being
> passed a file name as follows:
>
> datefile.pl c:\program files\IBM\SQLLIB\DB2\db2diag.log

You could paste the two arguments together in perl, add
a space between them, and use that as the filename.

Or you could just quote the argument:
datefile.pl "c:\program files\IBM\SQLLIB\DB2\db2diag.log"
0
s 4/14/2005 ****:03 PM
Stephen Day wrote:
> On Wednesday 13 April 2005 11:12 pm, Bret Goodfellow wrote:
> 
>>I have a script that is reading input from ARGV.  The script is being
>>passed a file name as follows:
>>
>>datefile.pl c:\program files\IBM\SQLLIB\DB2\db2diag.log
> 
> You could paste the two arguments together in perl, add
> a space between them, and use that as the filename.

What if there are two or more spaces?  A TAB character?


John
-- 
use Perl;
program
fulfillment
0
krahnj 4/14/2005 11:18:28 PM
On Friday 15 April 2005 12:18 am, John W. Krahn wrote:

> >>datefile.pl c:\program files\IBM\SQLLIB\DB2\db2diag.log
> >
> > You could paste the two arguments together in perl, add
> > a space between them, and use that as the filename.
>
> What if there are two or more spaces?  A TAB character?

Then it would totally mess up.

I was trying to say you could do a complex thing, but it's
better to just quote the argument and not face the problem
in the first place.
0
s 4/14/2005 11:32:50 PM
On 4/13/05, Bret Goodfellow  wrote:
> I have a script that is reading input from ARGV.  The script is being
> passed a file name as follows:
>=20
> datefile.pl c:\program files\IBM\SQLLIB\DB2\db2diag.log
>=20
> The problem I am running into is that the space is not recognized in the
> argument.  All that I get passed to is is c:\program    .  How do I get
> the rest of the argument.  Below is a portion of the script.  Thanks.
>=20
>  #***********************************************
>  # check input parameter                        *
>  #***********************************************
>  $arg_length =3D length($ARGV[0]);
>  $arg_lastchar =3D substr($ARGV[0], $arg_length-1, 1);
>  $arg_string =3D $ARGV[0] ;
>  print "Parameter argument is: $arg_string 
";
>=20
>=20

This is really an OS question, not a perl question: you'd have this
problem with any language, including, I think the built-in windows
shell interpreter--but that's another issue.  Most OSes and/or shells
have some method of escaping characters.  in most unix environments,
you'd handle this by using single quotes on the command line:
datefile.pl '/path/to/file'.  I'm not sure how windows handles it
(Ithink double and singe quotes may have the opposite meanings), and
it might vary between 95 and NT/2000/XP.  Try both of the following,
one of them will work:

datefile.pl 'c:\program files\IBM\SQLLIB\DB2\db2diag.log'
datefile.pl "c:\program files\IBM\SQLLIB\DB2\db2diag.log"

If you're going to be be doing a lot of work on Windows, take a look
at perldoc perlport, and pick up a copy of _Learning Perl for Win32_

HTH,

--jay
0
daggerquill 4/14/2005 11:37:41 PM