Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I echo string with bash command more in Perl?

Tags:

bash

perl

This is what I tried:

my $s = "s" x 1000;
my $r = `echo $s |more`;

But it doesn't work, my program exits directly...

like image 389
lexer Avatar asked Sep 08 '11 13:09

lexer


2 Answers

It does not work in your example, because you never print $r. The output is captured in the variable $r. By using system() instead, you can see the output printed to STDOUT, but then you cannot use the output as you (probably) expected.

Just do:

print $r;

Update: I changed say to print, since "echo" already gives you a newline.

To escape shell meta characters, as mentioned in the comments, you can use quotemeta.

You should also be aware that | more has no effect when capturing output from the shell into a variable. The process is simply: echo | more | $r, and you might as well skip more.

like image 163
TLP Avatar answered Nov 14 '22 21:11

TLP


try with the system() command :

my $s = "s" x 1000;
my $r = system("echo $s |more");

will display all your 's', and in $r you will have the result (0 in this case) of the command.

like image 33
Cédric Julien Avatar answered Nov 14 '22 23:11

Cédric Julien