Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Passing user input to a shell command

by james734 (Initiate)
on May 17, 2007 at 21:19 UTC ( #616111=perlquestion: print w/ replies, xml ) Need Help??
james734 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to take a input from users with <STDIN> and pass it to a shell command, but it is not taking the variable in the shell command. Here is what I am doing:
#!/usr/local/bin/perl -w -i.bak #macc auto format use warnings; use strict; use Shell; my ($file); print "Enter File Name to be converted.\n"; print "File should be in this direstory.\n"; $file = <STDIN>; open(IN, $file) or die "The file $file". "is not present.\n"; system qw "perl /Users/james/perlstuff/macc/macc1 $file";
Any help would be great.

Comment on Passing user input to a shell command
Download Code
Re: Passing user input to a shell command
by jdporter (Canon) on May 17, 2007 at 21:44 UTC

    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
      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
        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
      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";
Re: Passing user input to a shell command
by liverpole (Monsignor) on May 17, 2007 at 21:48 UTC
    Hi james734,

    Your problem, more likely than not, is that there's a newline character at the end of $file;

    Try adding the line:

    $file = <STDIN>; chomp $file;

    Another piece of advice is to use $! when printing errors that result from system calls:

    open(IN, $file) or die "The file $file is not present ($!)\n";

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Passing user input to a shell command
by FunkyMonk (Canon) on May 17, 2007 at 22:03 UTC
    In addition to comments made by the other monks, I'd also suggest you move to the three argument open and lexical filehandles.

    So, instead of

    open(IN, $file) or die "The file $file". "is not present.\n";

    use

    open my $IN, "<", $file or die "can't open $file: $!";
Re: Passing user input to a shell command
by shmem (Canon) on May 17, 2007 at 22:17 UTC

    Well, up to here it has really been nearly all PerlMonks on you :-)

    To elaborate further

    • gratuituous parens in my ($file);
    • you don't check whether you've actually read something from STDIN, nor what. Sanitize your input (see perlsec)
    • ...

    okay, I'll shut up... welcome to the monastery, btw :-)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-08-31 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls