Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Passing user input to a shell command

by jdporter (Canon)
on May 17, 2007 at 21:44 UTC ( #616116=note: print w/ replies, xml ) Need Help??


in reply to Passing user input to a shell command

You should not be using qw in the system call. Take it out.

Try this, to see why:

print "$_\n" for qw "perl /Users/james/perlstuff/macc/macc1 $file";

You might have other problems with that program as well.
For starters, you should probably chomp($file) immediately after reading it in.
Also, you're opening the file for reading, then immediately passing the filename to an external program. I really don't think you want to do both. You certainly don't need to open a file before passing it to an external program; that program will do what it wants with the name, and wouldn't see your open filehandle anyway.
Thirdly, you're pulling in the Shell module, but not using it. You could use it, by doing this:

use Shell qw( perl ); . . . perl "/Users/james/perlstuff/macc/macc1", $file;
A word spoken in Mind will reach its own level, in the objective world, by its own weight


Comment on Re: Passing user input to a shell command
Select or Download Code
Re^2: Passing user input to a shell command
by james734 (Initiate) on May 17, 2007 at 22:06 UTC
    I know that the file $file does not exist, I open it to insure that user is typing the file exists. The question should have been how do I tell the shell line that $file = <STDIN> is the value that is entered by the user and to put it into the shell line ie User enters test.txt So $file is now = test.txt the shell command shout be perl /Users/james/perlstuff/macc/macc1 test.txt. James
      Ignore me I got it working

        In a forum it is polite to explain what you did to fix it. That way when someone else has a similar problem and finds your thread, they can then use your solution! ;)


        ___________
        Eric Hodges
      I open it to insure that user is typing the file exists

      Opening it is not the best way to do that. There are the file test operators, such as -f:

      -f $file or die "You specified a non-existent file.\n";

      Even if you do open it for this purpose, you should definitely close it right away, before letting another process have it.

      A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re^2: Passing user input to a shell command
by adrianh (Chancellor) on May 20, 2007 at 16:21 UTC
    You should not be using qw in the system call

    Or, you should be using qw() but you need to fully specify the path to perl. That way you don't have to worry about $file having spaces in it.

      Did you try this?

      print "$_\n" for qw "perl /Users/james/perlstuff/macc/macc1 $file";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://616116]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-28 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls