Home > Net >  Execute a shell script as it would be an interactive session
Execute a shell script as it would be an interactive session

Time:10-30

For documentation purposes I am trying to execute a shell script in a way that it looks as you typed it by hand in an interactive shell.

Script:

x=123
echo $x

Then execute:

PS4="$PS1"
set -x -v
. ./demo

Output:

. ./demo
user@host:~/tmp$ . ./demo
x=123
user@host:~/tmp$ x=123
echo $x
user@host:~/tmp$ echo 123
123

Desired output:

user@host:~/tmp$ x=123
user@host:~/tmp$ echo $x
123

It does not have to be bash. Any solution that simulates an interactive session is welcome.

How can I achieve the desired result?

CodePudding user response:

A pretty simple solution is to start an interactive shell and redirect your script file to the input stream:

bash -i </path/to/script-file

CodePudding user response:

Use script. It will record your session including timing information and play it back for you. eg:

$ script -r output /bin/sh
Script started, output file is output
sh-3.2$ x=123
sh-3.2$ echo "$x"
123
sh-3.2$ exit
exit

Script done, output file is output
$ script -p output
Script started on Sat Oct 29 20:00:16 2022
sh-3.2$ x=123
sh-3.2$ echo "$x"
123
sh-3.2$ exit
exit

Script done on Sat Oct 29 20:00:22 2022

In the above, the first section is entered interactively, and then played back with script -p

  • Related