I've recently been trying to write a script that can be used to automate my c++ program in bash. If you know AutoIt, I can simply show you an example of what I am trying to do:




OK For those who do not speak AutoIt:

The first example sends a simulated "1" keystroke followed by an enter(CR)

The second example sends alt-a

I'm specifically trying to do this in bash. No other scripting language will do

Also, I would not prefer a spoonfeed. Please specify how to write it. No writing the script for me is allowed.

  • 1
    Are you automating a command-line, ncurses, or graphical program? – user1686 Apr 20 '13 at 16:02
  • 1
    I'm automating a command line for now. It is supposed to feed input into cin's(console inputs) automatically – Ishan Apr 20 '13 at 16:09

To feed text into a program's stdin, use pipes and/or redirection:

echo 1 | myprogram

(echo "First line"
 echo "Second line") | myprogram

myprogram <<EOF
First line
Second line

In case of interactive CLI programs (not full-terminal ones), it is possible to use named pipes or coprocesses as a sort of poor-man's expect (which you ruled out due to being Tcl):

mkfifo in out
myprogram <in >out &
echo "First line" >in
read -r reply <out

mkfifo in out
myprogram <in >out &
exec {infd}>in {outfd}<out
echo "First line" >&$infd
read -r reply <&$outfd

coproc foo { myprogram; }
echo "First line" >&${foo[1]}
read -r reply <&${foo[0]}

(Be careful when reading from the output pipe; e.g. head -1 <out won't just read one line – it'll buffer one full 4k block, print one line, discard the rest.)

| improve this answer | |
  • Both of the answers are equally good. But, this is what I weas more looking for. – Ishan Apr 21 '13 at 4:57
  • What is meant by CLI program / full terminal? – Chris Stryczynski Nov 18 '17 at 16:43
  • CLI stands for Command Line Interface. A CLI program is a simple program that runs on the command line and generally uses stdin and stdout as it performs some task and then finishes execution and returns to the command line prompt. grep and curl are good examples of this. A full terminal program is something that takes control of the entire window, like vim and screen. – Trevin Avery Jun 20 '18 at 0:59

You're looking for xdotool.

xdotool's equivalent of your example commands are:

xdotool key 1 Return
xdotool keydown Alt key a keyup Alt
| improve this answer | |
  • 1
    this is good. but I was thinking about some pure bash code – Ishan Apr 20 '13 at 16:32
  • 10
    @Ishan: It's practically impossible to do such things with pure bash code. Bash cannot use X11 directly, and it cannot use any existing X11 library either. You would have to connect to the X11 socket directly (bash only does TCP, not Unix sockets), parse the X11 protocol by hand (parsing binary data in bash is destined to fail) and craft the apropriate responses. bash is a shell, it is designed to work by chaining external tools. – user1686 Apr 20 '13 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.