Perl variable to shell command...?
Perl variable to shell command...?
am 04.09.2007 23:55:19 von Rohit
Hi, I am trying to use perl local variable with shell command call.
Example:
$filename=myFile.txt
@fileRec=`cat $filename`;
Here shell is not able to identify $filename variable. Any idea or
suggestions?
I am using array of filename so it is necessary for me to use cat with
variable.
Thanks in advance.
~Rohit
Re: Perl variable to shell command...?
am 05.09.2007 00:25:38 von usenet
On Sep 4, 2:55 pm, Rohit wrote:
> Hi, I am trying to use perl local variable with shell command call.
> Example:
> $filename=myFile.txt
You should quote your string literals. And scope your variable (and
'use strict;' at the top of your program). And to make it really
bullet-proof, fully qualify the filename:
my $filename = '/path/to/my/file.txt';
> @fileRec=`cat $filename`;
> Here shell is not able to identify $filename variable. Any idea or
> suggestions?
Your program is started from a directory other than the one your file
resides in. You can do one of the following:
- fully qualify the filenames
- cd to the appropriate directory before running your program
- chdir() to the appropriate directory within your program
FWIW, @fileRec=`cat $filename`; is of dubious quality. If your file
is large your program will crash. It would be safer to open the file
(in Perl) and process it one line at a time in a while{} loop. And
you have better control over your exception handling for error
conditions.
At the very least you should fully qualify your cat command (ie, '/usr/
bin/cat' or whatever). It is a potential security risk to call
unqualified system commands.
--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)
Re: Perl variable to shell command...?
am 05.09.2007 01:10:52 von Rohit
On Sep 4, 3:25 pm, use...@DavidFilmer.com wrote:
> On Sep 4, 2:55 pm, Rohit wrote:
>
> > Hi, I am trying to use perl local variable with shell command call.
> > Example:
> > $filename=myFile.txt
>
> You should quote your string literals. And scope your variable (and
> 'use strict;' at the top of your program). And to make it really
> bullet-proof, fully qualify the filename:
>
> my $filename = '/path/to/my/file.txt';
>
> > @fileRec=`cat $filename`;
> > Here shell is not able to identify $filename variable. Any idea or
> > suggestions?
>
> Your program is started from a directory other than the one your file
> resides in. You can do one of the following:
> - fully qualify the filenames
> - cd to the appropriate directory before running your program
> - chdir() to the appropriate directory within your program
>
> FWIW, @fileRec=`cat $filename`; is of dubious quality. If your file
> is large your program will crash. It would be safer to open the file
> (in Perl) and process it one line at a time in a while{} loop. And
> you have better control over your exception handling for error
> conditions.
Yupe, you are right. My file size is 210KB, and it is failing while I
am piping its output for further processing.
$symbolFile = "$symbolFile[$fileCount]";
....
....
$var123="$symbolFile";
@dataVar = `/bin/cat $var123 | grep -i ^func`
If I remove piping here, it works great. Let me see now perl parsing.
>
> At the very least you should fully qualify your cat command (ie, '/usr/
> bin/cat' or whatever). It is a potential security risk to call
> unqualified system commands.
>
> --
> The best way to get a good answer is to ask a good question.
> David Filmer (http://DavidFilmer.com)
Thanks for your comments.
~Rohit
Re: Perl variable to shell command...?
am 05.09.2007 01:43:42 von Tad McClellan
usenet@DavidFilmer.com wrote:
> On Sep 4, 2:55 pm, Rohit wrote:
>> Hi, I am trying to use perl local variable with shell command call.
>> Example:
>> $filename=myFile.txt
>
> You should quote your string literals. And scope your variable (and
> 'use strict;' at the top of your program).
And use semicolons between statements.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: Perl variable to shell command...?
am 05.09.2007 02:13:29 von glex_no-spam
Rohit wrote:
>> On Sep 4, 3:25 pm, use...@DavidFilmer.com wrote:
>>> On Sep 4, 2:55 pm, Rohit wrote:
[...]
>> FWIW, @fileRec=`cat $filename`; is of dubious quality. If your file
>> is large your program will crash. It would be safer to open the file
>> (in Perl) and process it one line at a time in a while{} loop. And
>> you have better control over your exception handling for error
>> conditions.
> Yupe, you are right. My file size is 210KB, and it is failing while I
> am piping its output for further processing.
>
> $symbolFile = "$symbolFile[$fileCount]";
> ...
> ...
> $var123="$symbolFile";
> @dataVar = `/bin/cat $var123 | grep -i ^func`
>
> If I remove piping here, it works great. Let me see now perl parsing.
If you remove the pipe, it doesn't "work" at all.
Why are you using cat at all? The grep command takes filenames
as arguments.
man grep
If you want to use perl to read through the file:
perldoc perlintro
Look for "Files and I/O"
Re: Perl variable to shell command...?
am 05.09.2007 07:19:25 von usenet
On Sep 4, 4:43 pm, Tad McClellan wrote:
> And use semicolons between statements.
Yeah, that too...
Re: Perl variable to shell command...?
am 05.09.2007 17:23:36 von Benoit Lefebvre
On Sep 4, 5:55 pm, Rohit wrote:
> Hi, I am trying to use perl local variable with shell command call.
>
> Example:
>
> $filename=myFile.txt
>
> @fileRec=`cat $filename`;
>
> Here shell is not able to identify $filename variable. Any idea or
> suggestions?
> I am using array of filename so it is necessary for me to use cat with
> variable.
>
> Thanks in advance.
>
> ~Rohit
I like to do it that way
$filename = "myfile.txt";
$cmd = "cat ". $filename;
@fileRec = `$cmd`;
Re: Perl variable to shell command...?
am 06.09.2007 08:54:47 von Josef Moellers
Benoit Lefebvre wrote:
> On Sep 4, 5:55 pm, Rohit wrote:
>=20
>>Hi, I am trying to use perl local variable with shell command call.
>>
>>Example:
>>
>>$filename=3DmyFile.txt
>>
>>@fileRec=3D`cat $filename`;
>>
>>Here shell is not able to identify $filename variable. Any idea or
>>suggestions?
>>I am using array of filename so it is necessary for me to use cat with
>>variable.
>>
>>Thanks in advance.
>>
>>~Rohit
>=20
>=20
> I like to do it that way
>=20
> $filename =3D "myfile.txt";
>=20
> $cmd =3D "cat ". $filename;
>=20
> @fileRec =3D `$cmd`;
While this is how I'd do it, too (I can then show the entire command for =
debugging purposes), it won't help the OP as the net result won't be any =
different.
Most likeley, the file isn't accessable, e.g. because it's in a=20
different path or it is read-protected, or the OP isn't telling us=20
everything, as his code is not valid Perl anyway.
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
Re: Perl variable to shell command...?
am 06.09.2007 10:05:29 von Klaus
On Sep 5, 1:10 am, Rohit wrote:
> On Sep 4, 3:25 pm, use...@DavidFilmer.com wrote:
>
> > On Sep 4, 2:55 pm, Rohit wrote:
>
> > > Hi, I am trying to use perl local variable with shell command call.
> > > Example:
> > > $filename=myFile.txt
>
> > You should quote your string literals.
[ ...snip... ]
> > my $filename = '/path/to/my/file.txt';
You have replaced your string literal myFile.txt
by a single variable $symbolFile[$fileCount]:
> $symbolFile = "$symbolFile[$fileCount]";
While you should quote string literals, you should *not* quote a
single variable.
See perlfaq 4: What's wrong with always quoting ``$vars''?
--
Klaus
Re: Perl variable to shell command...?
am 06.09.2007 16:06:54 von Ben Morrow
Quoth Josef Moellers :
> Benoit Lefebvre wrote:
> > On Sep 4, 5:55 pm, Rohit wrote:
> >
> >>$filename=myFile.txt
> >>
> >>@fileRec=`cat $filename`;
> >>
> >>Here shell is not able to identify $filename variable. Any idea or
> >>suggestions?
> >
> > I like to do it that way
> >
> > $filename = "myfile.txt";
> >
> > $cmd = "cat ". $filename;
> >
> > @fileRec = `$cmd`;
>
> While this is how I'd do it, too (I can then show the entire command for
> debugging purposes),
For cases when you need an external command, I'd agree. This is not one
of them: perl is perfectly capable of reading a file on its own:
my @fileRec = do {
open my $FH, '<', 'myFile.txt'
or die "can't read 'myFile.txt': $!";
<$FH>;
};
or indeed
use File::Slurp qw/read_file/;
my @fileRec = read_file 'myFile.txt';
> Most likeley, the file isn't accessable, e.g. because it's in a
> different path or it is read-protected, or the OP isn't telling us
> everything, as his code is not valid Perl anyway.
....except it is, it just doesn't mean what he thinks it does:
~% perl -le'print myFile.txt'
myFiletxt
~%
'myFile' and 'txt' are autoquoted, and the . concatenates them. Yet
another reason why you should always use strictures: in this case, the
rather perversely-named strict 'subs' (it should clearly be strict
'strings') rather than the usual strict 'vars'.
Ben
Re: Perl variable to shell command...?
am 07.09.2007 20:37:42 von Rohit
On Sep 6, 7:06 am, Ben Morrow wrote:
> Quoth Josef Moellers :
>
>
>
> > Benoit Lefebvre wrote:
> > > On Sep 4, 5:55 pm,Rohit wrote:
>
> > >>$filename=myFile.txt
>
> > >>@fileRec=`cat $filename`;
>
> > >>Here shell is not able to identify $filename variable. Any idea or
> > >>suggestions?
>
> > > I like to do it that way
>
> > > $filename = "myfile.txt";
>
> > > $cmd = "cat ". $filename;
>
> > > @fileRec = `$cmd`;
>
> > While this is how I'd do it, too (I can then show the entire command for
> > debugging purposes),
>
> For cases when you need an external command, I'd agree. This is not one
> of them: perl is perfectly capable of reading a file on its own:
>
> my @fileRec = do {
> open my $FH, '<', 'myFile.txt'
> or die "can't read 'myFile.txt': $!";
> <$FH>;
> };
>
> or indeed
>
> use File::Slurp qw/read_file/;
>
> my @fileRec = read_file 'myFile.txt';
>
> > Most likeley, the file isn't accessable, e.g. because it's in a
> > different path or it is read-protected, or the OP isn't telling us
> > everything, as his code is not valid Perl anyway.
>
> ...except it is, it just doesn't mean what he thinks it does:
>
> ~% perl -le'print myFile.txt'
> myFiletxt
> ~%
>
> 'myFile' and 'txt' are autoquoted, and the . concatenates them. Yet
> another reason why you should always use strictures: in this case, the
> rather perversely-named strict 'subs' (it should clearly be strict
> 'strings') rather than the usual strict 'vars'.
>
> Ben
Yupe, thanks everyone. I have replace entirely replaced my code..
for ($fileCount = 0; $fileCount < 3; $fileCount++) {
$symbolFile = $symbolBreakpadFile[$fileCount];
open SYMBFILEHANDLE, "< $symbolFile" or die $!;
@funcRec= ;
open FHANDLE, "> $SymbolPlistFile[$fileCount]" or die $!;
myMain();
close(SYMBFILEHANDLE);
}
Thanks again for your time and pointing out my mistakes, which I could
have never identified.
Thanks, Rohit
It's a journey from Learning to Learning....which never ends...!
Re: Perl variable to shell command...?
am 08.09.2007 01:01:44 von Ben Morrow
Quoth Rohit :
>
> Yupe, thanks everyone. I have replace entirely replaced my code..
OK, 'round we go again... :) (please don't be offended by this: the aim
is solely to help you learn better Perl practices).
> for ($fileCount = 0; $fileCount < 3; $fileCount++) {
Do you have
use strict;
use warnings;
at the top of your script? If not, you should have, and you should fix
all the error messages you get.
Using a C-style for loop is nearly always a bad idea: Perl has better
ways of iterating over things. In this case, over a list of numbers:
for my $fileCount (0..2) {
The 'my' means the variable doesn't exist outside the for loop, and also
keeps strict happy.
The only reason you need the index at all is because you have two
parallel arrays. This is usually a sign you should have a data
structure, something like
my @symbolFile = (
{
Breakpad => "breakpad #1",
Plist => "plist #1",
},
{
Breakpad => "breakpad #2",
Plist => "plist #2",
},
);
and then you could simply iterate over the array
for my $symbolFile (@symbolFile) {
# use $symbolFile->{Breakpad} and $symbolFile->{Plist}
}
but changing that will require changing the rest of your program, so you
may want to leave it for now.
> $symbolFile = $symbolBreakpadFile[$fileCount];
This needs a 'my'.
> open SYMBFILEHANDLE, "< $symbolFile" or die $!;
You should use three-arg open, nowadays. You should also keep your
filehandles in variables, and give sensible error messages.
open my $SYMBFILEHANDLE, '<', $symbolFile
or die "can't read '$symbolFile': $!";
> @funcRec= ;
> open FHANDLE, "> $SymbolPlistFile[$fileCount]" or die $!;
> myMain();
Oh dear, you are passing data to a function by modifying a global value
(in this case, a filehandle). This is a very bad idea: it makes it very
hard to see where values come from when something goes wrong. If you
switch to keeping filehandles in variables you can pass it into the
function as a parameter:
open my $PLIST, '>', $SymbolPlistFile[$fileCount]
or die "can't write '$SymbolPlistFile[$fileCount]': $!";
myMain($PLIST);
and then in myMain:
sub myMain {
my ($PLIST) = @_;
# write to $PLIST instead of FHANDLE
}
Also note that one of the advantages of scoping your variables with my
is you no longer need such long names: you should be able to easily see
the piece of code a variable name is valid over, and see that it is
unique.
> close(SYMBFILEHANDLE);
If you use lexical filehandles (filehandles in variables) they close
automatically when the variable goes out of scope. This is a saving if
you're not going to check the return value of close anyway (not
generally necessary on files you are reading, but worth doing on files
you are writing, as an error can be delayed until then).
> Thanks again for your time and pointing out my mistakes, which I could
> have never identified.
That's quite alright :).
Ben