how to pass command line arguments to perl script file in one-line

Viewed 711

I ve a perl script file , which runs on ubuntu terminal

So while running , i'm invoking it like that :

./myscript.pl

while running , under terminal , it starts to ask for some "confirmation" (for configuration) which i should tape , like this :

enter image description here

and then , another time with another ask for "confirmation"

enter image description here

, and then for almost 10 other times for different config confirmations where i'm always taping the default option.

So my purpose is how to be able to do it in once , in only one-line command , to be able after that to put it under bash_profile.

i would try this :

./myscript.pl "yes" "yes "no" ..... "yes"

But i don't know if that would work .

3 Answers

There are various ways to handle this. If you control the program, you should re-arrange it to accept the defaults when it's not interactive (and IO::Interactive is a good tool for that).

For really quick things, sometimes I just steal prompt from ExtUtils::MakeMaker. It's self contained so you can steal and modify it for your needs:

#!perl
use v5.10;

use ExtUtils::MakeMaker qw(prompt);

my @answers;
push @answers, prompt( "First", "yes" );
push @answers, prompt( "Second", "yes" );
push @answers, prompt( "Third", "yes" );

say "Done! Got @answers";

If I run this normally, I have to answer the prompts:

$ perl yes.pl
First [yes] cat
Second [yes] dog
Third [yes] bird
Done! Got cat dog bird

But ExtUtils::MakeMaker has an environment variable to accept the defaults:

$ PERL_MM_USE_DEFAULT=1 perl yes.pl
First [yes] yes
Second [yes] yes
Third [yes] yes
Done! Got yes yes yes

From here to the end of the answer are just shell techniques to supply input. There's no special Perl stuff going on. And, some things may not work depending on what the program itself is trying to do.

I can also feed it /dev/null, in which case it realizes the program is not running interactively, and it accepts the defaults for me:

$ perl yes.pl < /dev/null
First [yes] yes
Second [yes] yes
Third [yes] yes
Done! Got yes yes yes

But, I can also feed it a multiline string for input:

$ perl yes.pl <<HERE
yes
yes
no
HERE
First [yes] Second [yes] Third [yes] Done! Got yes yes no

That might be too much for your file, so you can echo a string with embedded lines:

$ perl yes.pl < <( echo -e "yes\nno\nhello")
First [yes] Second [yes] Third [yes] Done! Got yes no hello
$ perl yes.pl < <( echo $'yes\nno\nhello')
First [yes] Second [yes] Third [yes] Done! Got yes no hello

Or get the input from a file:

$ perl yes.pl < input.txt
First [yes] Second [yes] Third [yes] Done! Got heck yeah no way yep

And, stolen from simbabque's answer, since I'm going for a list of techniques:

$ printf "yes\nyes\nno\n...\nyes\n" | yes.pl

You can use printf on the commandline to pipe multiple lines of input into your program. If it expects things like yes and no in a fixed order, that should be enough.

$ printf "yes\nyes\nno\n...\nyes\n" | ./myscript.pl

See this blog post fore more information.

It all depends on how your program is written. I suspect it looks something like this:

#!/usr/bin/perl

use strict;
use warnings;

my $default = 'yes';
print "Do you want to proceed with this installation? [$default] ";
chomp( my $answer = <STDIN> );

$answer = $default unless length $answer;

print "You said $answer\n";

In which case, you can pass a default on the command line using code like this:

#!/usr/bin/perl

use strict;
use warnings;

my $default = 'yes';
my $answer;

if (@ARGV) {
  $answer = shift;
} else {
  print "Do you want to proceed with this installation? [$default] ";
  chomp( $answer = <STDIN> );
}

$answer = $default unless length $answer;

print "You said $answer\n";

You'd need to repeat these changes for each of the individual prompts.

Related