0

Is there such a program for Terminal like the iMacros plugin for Firefox, that can record what I did, then automatic do the work I have done? I hope that there is such a program to help me do the repeat work in the terminal, so any tips?

Regards

4 Answers 4

1

Google finds this. Is it what you are after?

If I were to automate human-to-TTY interaction I would rather do it using Expect.

1
  • Expect is what I wanted!Thank you very much.My idea is that record what I typed in the terminal with tmux's capture-pane & save_buffer functions, then write a perl script the generate a expect script, when I do the repeat work, I can just run the script!Is that a good idea? Thanks again!
    – liunx
    Oct 9, 2012 at 2:39
1

bash contains all the utilities you actually need. If you want to execute a number of commands with a single command, you can either write your own shell scripts or write functions.

Functions are easy

A few days ago, I defined a function cs to change the directory and list it via ls.

function cs() { cd $1 && ls ; }

$1 is the first argument. If you want to use the list of all arguments, use $@.

It can be invoked like

$ cs my_directory

This is great to quickly group a number of commands together inside your current session. You can place the function at the end of your ~/.bashrc to load it with every new instance of bash.

Scripts are quite powerful

Scripts execute in their own process. This means that any variables you assign are local to this process. To write a script, place a shebang at the top. It consists of the characters #! followed by the path to the interpreter that will execute your file. Shebangs could include

#!sh
#!/bin/bash
#!/usr/bin/perl

In a shell script, you could then do stuff like

#!/bin/bash
VARIABLE=42
echo "The variable is $VARIABLE"  # echo appends a newline
echo "I'm now going to Google"
firefox http://www.google.com

Don't forget to make the script executable via

$ chmod +x my_script

It can then be executed like

$ ./my_script         # from the current directory
$ ~/path/to/my_script # absolute path, ~ is your User directory.

The bash syntax contains conditionals and loops and switches and almost anything. If you loathe the low performance or if you need nicer syntax, you can easily teach yourself a few lines of Perl or Python.

Seeing what you typed

These solutions depend on knowing what you want to do before you do it. If this is not the case, you can look at the history file (~/.bash_history by default) or use the history command.

$ history 5

lists the 5 last commands, with a running number. In my case, this gives

 1892  function foo() { cd $1 ; ls ; }
 1893  foo ..
 1894  history
 1895  history 15
 1896  history 5

You can easily use this command list to write a function or a shell script.

A quick homebrew solution

Here is a solution for macro recording. It uses two user-defined functions, and an inline perl script to do the interesting processing.

function create-marker() { echo "Starting the recording" ; }
function process-marker() { history | perl -ne's/^\s*\d*\s*//; chomp; push @commands, $_; }{ pop @commands; push @macro, pop @commands until $commands[-1] eq "create-marker" or !@commands; print "$_\n" for reverse @macro;' ; }

This session

$ create-marker
Starting the recording
$ cd ~
$ ls
# omitted
$ firefox http://www.google.com
$ process-marker

Creates following output:

cd ~
ls
firefox http://www.google.com

You can then easily copy-and-paste that into a new script.


Note: I don't have a Mac and tested this on Ubuntu. Paths may differ, but the concepts are sound.

1
  • Thanks a lot, It's helpful, but when I interactive with a ssh connection or serial connection, we may can not get help from history, right? So the macro recorder may be limited, but thank you all the same!
    – liunx
    Oct 9, 2012 at 2:05
0

It's unlikely to interest you if you're not a user, but emacs has a shell mode (M-x shell) which of course supports the normal emacs editting commands, including keyboard macros (C-x ( to start recording, C-x ) to stop, then C-x e to execute). If all you're doing is repeating a bunch of typing, this may be useful.

But more broadly I agree with bobah -- you don't state the problem you're trying to solve, but it's almost certainly something better suited to tools like expect than raw input forgery...

0
0

I know you're asking this for Terminal, but since I have not heard about such a program, an alternative would the ZOC Terminal. It is commercial so it may not be up your alley, but it is the closest thing I can think of, given your problem description. It does support using a local shell on Mac OS (essentially a replacement for terminal) and it has a script recording feature (Script-menu -> Record Scipt). You can also map these scripts (or shorter texts) on buttons and key combinations.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .