Tool to record an interactive bash session to shell script


Is there a tool to record an interactive shell session to a shell script?

For example if we entered in a terminal:

interactive2sh reconfig_foo
vim /etc/foo.cfg
/etc/init.d/foo restart

then I'd want the tool to create a shell script containing something like:

echo bar >> /etc/foo.cfg
/etc/init.d/foo restart

I can record an interactive session into a shell script using the code below. However, I would have thought that this was a common enough task that there would be an existing tool. Whenever I search for such a tool I just get lots of references to script/scriptreplay which would reproduce the output but wouldn't actually reconfigure the service "foo".

#prototype of interactive2sh, a tool for recording a bash session into a shell script 
export OUTPUT_SH_FILE=~/$
if [ -e $OUTPUT_SH_FILE ]
    find $OUTPUT_SH_FILE* -type f -exec echo -e \\n---------- '{}' \; -exec cat '{}'  \; 
    echo ^^^^^ script already exists ^^^^^
    exit 1
mkdir $
mkdir $OUTPUT_SH_FILE.out
echo 'SH_FILE=`readlink -f "$0"`' >> $OUTPUT_SH_FILE
echo cd `pwd` >> $OUTPUT_SH_FILE

#Start a new shell, which logs various things needed to replay the commands
bash --rcfile <(
cat << "RCFILE_EOF"
set +x
source "$HOME/.bashrc"
export PROMPT_COMMAND='RETRN_VAL=$?;echo "$(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) # [$$] [RETRN_VAL=$?]" >> $OUTPUT_SH_FILE'
vim () {
    for f in "$@"
        fullpath=`readlink -f "$f"`
        [ -e $"$fullpath" ] || cp --parents "$fullpath" $ 
    /usr/bin/vim "$@"
    for f in "$@"
        fullpath=`readlink -f "$f"`
        test ! -e $fullpath || cmp "$fullpath" $"$fullpath" || (
            cp --parents "$fullpath" $OUTPUT_SH_FILE.out
            echo mv $fullpath $fullpath.`date -r $fullpath +%F`.bak >> $OUTPUT_SH_FILE
            echo cp '$SH_FILE'.out/$fullpath $fullpath >> $OUTPUT_SH_FILE
export PS1='[$OUTPUT_SH_FILE] \w>'

chmod +x $OUTPUT_SH_FILE

Best Answer

I've hacked together a very basic implementation which might be enough for your needs. It's based on two scripts: record and replay.

Here is the record script:

history | tail -1 | awk '{ print 1+$1 }' > /tmp/record

And this is the replay script:

fc -l `cat /tmp/record` `history | tail -1 | awk '{ print $1-1 }'` | cut -d\  -f2-

The first one saves your current history number into /tmp/record. The second script shows all the commands you have given since record was invoked.