1

How can I use this code to create more commands. The current version does 2. How can i do 3 or 4 or more?

my $startprocess = `(echo "y" | nohup myprocess) &`

The original question answered by user DVK:

Can I execute a multiline command in Perl's backticks?

edit: thanks for the reply sebastian. I have to run everything in one line because Im running a program within terminal and i want to make progressive commands.

e.g command 1 starts the program. command 2 navigates me to the menu. Command 3 lets me change a setting. Command 4 lets me issue a command that prompts a response that I can only get under the condition of that new setting.

To run multiple commands would keep me trapped in step one.

Community
  • 1
  • 1

1 Answers1

5

The line you quoted contains one command line piped together. That's not running multiple commands.

Did you consider using open?

my $pid = open my $fh, '-|', 'myprocess';
print $fh "y\n";

There is no need to run multiple commands in one (backtick) line, why not just use multiple ones?

$first = `whoami`;
$second = `uptime`;
$third = `date`;

Backticks are used to capture the output of the command, system just runs the command and returns the exit state:

system '(echo "y" | nohup myprocess) &';

All solutions allow multiple commands piped together as this is a shell feature and all commands just pass the command string to the shell (unless it's simple enough to handle it without a shell):

Bash:

$ ps axu|grep '0:00'|sort|uniq -c|wc

Perl:

system "ps axu|grep '0:00'|sort|uniq -c|wc";
$result = `ps axu|grep '0:00'|sort|uniq -c|wc`;
open my $fh, '|-', "ps axu|grep '0:00'|sort|uniq -c|wc";

Always watch the quotation marks: system "foo "bar" baz"; won't pass "bar" baz as arguments to the foo command.

Lots of common stuff in this answer: Please be more detailed in your question to get a better reply.

Sebastian
  • 2,472
  • 1
  • 18
  • 31