Search code examples
perlenterkeystrokesimulate

Simulate pressing enter key in Perl


I have following perl script which saves the output from the command into a textfile

#!/usr/bin/perl 
use warnings;
use strict;
use Term::ANSIColor;

my $cmd_backupset=`ssh user\@ip 'dsmadmc -id=username -password=passwd "q backupset"' >> output.txt`;

open CMD, "|$cmd_backupset" or die "Can not run command $!\n";
print CMD "\n";
close CMD;

The output of output.txt is this:

Text Text Text
...
more...   (<ENTER> to continue, 'C' to cancel)

The script is still running in the terminal and when I press enter, the output.txt file gets the extra information. However, I must press enter more than 30 times to get the complete output. Is there a way to automate the script so when the last line in output.txt is more..., it simulates pressing enter?

I have tried with Expect (couldn't get it installed) and with echo -ne '\n'


Solution

  • Most interactive commands, like the one you are using, accept some flag or some command to disable pagination. Sometimes, connecting their stdin stream to something that is not a TTY (e.g. /dev/null) also works.

    Just glancing over IBM dsmadmc docs, I see it accepts the option -outfile=save.out. Using it instead of standard shell redirection would probably work in your case.