If I want to simulate an enter key pressed,I just use:
echo -ne '\n' | mycommand
But "mycommand" requires to wait some seconds before actually press enter key. How could I wait some seconds before simulate the enter key pressing?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityUse expect
, something like this should do:
#!/usr/bin/env expect
spawn "mycommand"
expect "Press Enter to continue" { send "\r" }
interact
Of course, you need to change "Press Enter to continue" to the actual message.
echo...
with flags, which acts differently depending on shell, consider using the more well-defined printf "\n"
(or plain echo
I suppose)
echo
alone which sends a newline by itself already. Feels silly to remove that newline with -n
(non-standard) just to add it back with -e
(even less portable) and \n
.
Jul 16, 2020 at 15:07
expect
solution ;-)
send "\r"
instead of send "\n"
to similar pressing Return (shouldn't make much difference as the pty line discipline will likely translate \r
to \n
anyway)
Jul 16, 2020 at 15:11
spawn -noecho
to avoid echoing the command. Maybe #! /usr/bin/env expect
as not many systems have expect
in /bin
.
Jul 16, 2020 at 15:14
(sleep 12; echo) | mycommand
mycommand
will read an empty line after 12 seconds and see end-of-file straight after. If that's a problem, you can add a delay after as well:
(sleep 12; echo; sleep 5) | mycommand
See also expect
to send input based on what the command outputs (it also uses pseudo-terminals which makes it look like there's a real user).
mycommand
executes before the end of commands in ()
?
you could use xdotool to simulate a "enter":
xdotool key 'Return' | commandhere
or you can make it sleep then enter a command:
sleep 0.5; xdotool key 'Return' | commandhere
curl
only looks for it after a few moments. That's different to the requirement