0

Is there any way to capture the texts on termianl screen inside a perl script. I know there are some functions like system,exec,backticks but the problem is that they execute commands FROM the script.For ex:- in terminal i write cd/ (or) ls,and after that i run my perl script which will read what was written on termianl screen(in this case, scipt will capture cd/ (or) ls-whichever was given to termianl). I came with one solution that by passing the commands which you wrote on termianl as a command line arguments to the script,but any other way???

9
  • I'm guessing that you'd want my $input = <STDIN>; but this halts your code untill the user has actually made input and pushed enter. Also you'd wanna use chomp($input); afterwords. Commented Oct 14, 2014 at 7:52
  • can you please expand..I am new to perl..just started last 2-3 days Commented Oct 14, 2014 at 7:54
  • Try the following code: print "Enter your name: "; my $input = <STDIN>; chomp($input); print "Hello $input, nice to meet you.\n"; Commented Oct 14, 2014 at 7:57
  • this is sort of like: printf("\n Enter a number"); scanf("%d",&a) ; in C...I don't want the script to wait for the user input..I want it to read it what is written on the terminal screen.for example for running the script i write perl tool.pl so when i run the scipt it should read perl tool.pl which is on the terminal or whatever cmds was given before that...Is it possible?? Commented Oct 14, 2014 at 7:58
  • @ShikharDeep: Don't use printf when print is sufficient. Commented Oct 14, 2014 at 8:02

2 Answers 2

1

Like this maybe:

history | perl -ne 'print $_'
Sign up to request clarification or add additional context in comments.

3 Comments

This is a variation on the "idea of passing the command(which i am trying to capture) as a cmd line argument" mentioned by Shikhar in his comments, but I think it's probably the closest he's going to get.
what will -ne will perform??
The perlrun page of the Perl documentation will tell you what Perl's various command line options do, including -n and -e.
1

As I understand it, in a situation where you've typed some stuff into a terminal like this:

[tai@littlerobot ~] echo "Hello"
Hello
[tai@littlerobot ~] perl myscript.pl

You want myscript.pl to be able to access the echo "Hello" part, and possibly also the Hello that was that command's output.

Perl does not provide such a feature. No programming language does or can provide such a feature because the process in which your script/program runs has no intrinsic knowledge about what happened in the same terminal before it was run. The only way it could access this text would be if it could ask the currently running terminal, which will have some record of this information (i.e. the scrollback buffer), even if it cannot distinguish between which characters in the text were typed by you, and which are output. However, I know of no terminal that exposes that information via any kind of public API.

So if you want myscript.pl to be able to access that echo "Hello", you'll need to pass it to your script. Piping history to your script (as shown by Mark Setchell in his answer) is one technique. history is a shell built-in, so it has as much knowledge as your shell has (which is not quite the same knowledge as your terminal has). In particular it can give you a list of what commands have been typed in this shell session. However, it cannot tell you about the output generated by those commands. And it cannot tell you about other shell sessions, so doing this in Perl is fairly useless:

my @history = `tcsh -c history`;

The last thing you could try (though it would be incredibly complicated to do) would be to ask the X server (or Windows if running on that operating system) for a screen shot and then attempt to locate which rectangle the current terminal is running in and perform OCR on it. This would be fraught with problems though, such as dealing with overlapping windows.

So, in summary, you cannot do this. It's nothing to do with Perl. You cannot do this in any programming language.

1 Comment

yeah thats what i am trying to do (to capture echo hello)..Ok if its not possible then i will go on by passing the cmd as cmd line arguments.Thanks for the detailed explaination

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.