In a shell script, I want to 'spool' (record) the shell prompt & command typed alongwith its output into a file.

eg:

In the script, if I give a 'ls' command, the file should contain:

<<root@server ~>>$ ls
a
b
c.txt
<<root@server ~>>$

i.e. the command as if issued on the shell followed by its output.

Any ideas on how to accomplish this? Tried using the 'script' command but to no use.

Recommended Answers

All 10 Replies

What's wrong with script?

What's wrong with script?

If I use the 'script' command inside a shell script, it does not work as intended.

Executing the shell script, I get

Script started, output file is tmpfile.txt
bash-3.2$

I realize this does not exactly answer your question but why in the world would you need this. Almost all shells store the commands issued in an individual user history file. (ie /home/bob/.bash_history stores the last 1000 of Bob's commands ). When logged in as bob typing history at the prompt will show all his history.

If you are trying to track users use of the root user to make changes I suggest you take a look at sudo. Sudo allows you to give users the ability to execute commands as if they were root and track every command by the user issuing it. For example if the user bob wanted to issue the command 'ls -la /root' he would type the following at the command line and it would prompt him for his password:

<< bob@server ~>>$ sudo ls -la /root

The /etc/sudoers file tracks which commands can be issued by whom.

This way you do not give the root password to anyone and can limit the commands they can issue.

You could always just echo the command before it's run, then run it. You could even put a prompt in there if you really wanted.

#!/bin/sh

prompt="<<root@server ~>>$ "

for command in "ls" "cd /tmp" "ls"; do
    echo "$prompt $command"
    $command
done

Sample output:

$ sh tmp.sh 
<<root@server ~>>$  ls
8ball.php  bot.php  test.php  test.pl  tmp.sh
<<root@server ~>>$  cd /tmp
<<root@server ~>>$  ls
gconfd-user orbit-user

Along the same lines as Gromit suggests, try this (I lay no claim to the name 'dupit') Here's a baby script:

output=script_out_file
rm -f $output
touch $output
dupit() {
  echo "$@" >> $output
  $@ >> $output
}
dupit echo "HELLO"
dupit ls -lrt
dupit echo "GBYE"
dupit sleep 2

Invoke the script as bash the_script; cat script_out_file It will appear to run for too long. That's ok, because we told it to sleep for 2 seconds. Minor syntax changes for shells other than bash.

commented: Griswolf always brings the good ideas. +3

script is working as it is meant to. What script does is start another shell, and record the putpot from that shell in the logfile. When you've finished exit the shell and script will close the logfile and exit, from there your shell script will continue. What is it that you are trying to achieve?

Script works as specified, but it doesn't do what OP needs: both the command and the output.

It does, both command and output. I suspect that the OP is running script in a shell script and expecting ti to log everything that happens after it start, which of course it won't.

Both commandand output are available, however, until the OP states exactly what he is trying to achieve there's not much else we can do.

user_id@directory googlecl-0.9.12]$ script
Script started, file is typescript
[user_id@directory googlecl-0.9.12]$ ls -l
total 64
-rw-r--r-- 1 user_id user_id   741 Jun 16  2010 INSTALL.txt
-rw-r--r-- 1 user_id user_id   821 Jan 21 23:49 PKG-INFO
-rw-r--r-- 1 user_id user_id  5198 Dec 17 00:50 README.config
-rw-r--r-- 1 user_id user_id  5241 Jan 18 01:14 README.new-usage
-rw-r--r-- 1 user_id user_id 14562 Nov 15 13:31 README.txt
drwxr-xr-x 4 root   root    4096 Jan 23 14:43 build
-rw-r--r-- 1 user_id user_id  5463 Jan 18 01:15 changelog
drwxr-xr-x 2 user_id user_id  4096 Jan 21 23:49 man
-rw-r--r-- 1 user_id user_id  3219 Jan 18 01:17 setup.py
drwxr-xr-x 3 user_id user_id  4096 Jan 21 23:49 src
-rw-r--r-- 1 user_id user_id     0 Feb 16 01:42 typescript
[user_id@directory googlecl-0.9.12]$ exit
exit
Script done, file is typescript
[user_id@directory googlecl-0.9.12]$ cat -v typescript
Script started on Wed Feb 16 01:42:06 2011
^[]0;user_id@directory:~/googlecl-0.9.12^G^[[?1034h[user_id@directory googlecl-0.9.12]$ ls -l^M
total 64^M
-rw-r--r-- 1 user_id user_id   741 Jun 16  2010 INSTALL.txt^M
-rw-r--r-- 1 user_id user_id   821 Jan 21 23:49 PKG-INFO^M
-rw-r--r-- 1 user_id user_id  5198 Dec 17 00:50 README.config^M
-rw-r--r-- 1 user_id user_id  5241 Jan 18 01:14 README.new-usage^M
-rw-r--r-- 1 user_id user_id 14562 Nov 15 13:31 README.txt^M
drwxr-xr-x 4 root   root    4096 Jan 23 14:43 ^[[0m^[[01;34mbuild^[[0m^M
-rw-r--r-- 1 user_id user_id  5463 Jan 18 01:15 changelog^M
drwxr-xr-x 2 user_id user_id  4096 Jan 21 23:49 ^[[01;34mman^[[0m^M
-rw-r--r-- 1 user_id user_id  3219 Jan 18 01:17 setup.py^M
drwxr-xr-x 3 user_id user_id  4096 Jan 21 23:49 ^[[01;34msrc^[[0m^M
-rw-r--r-- 1 user_id user_id     0 Feb 16 01:42 typescript^M
^[]0;user_id@directory:~/googlecl-0.9.12^G[user_id@directory googlecl-0.9.12]$ cat^H ^H^H ^H^H ^Hexit^M
exit^M

Script done on Wed Feb 16 01:42:14 2011
[user_id@directory googlecl-0.9.12]$

Both command and output are available from script, as shown, but until the OP lets us know what he is trying to achieve there's not much more that we can do. If he is try to pick this up from a script rather than keyboard input he'd probably do better if he was to try

set -v

and redirect the output.

user_id@server googlecl-0.9.12]$ script
Script started, file is typescript
[user_id@server googlecl-0.9.12]$ ls -l
total 64
-rw-r--r-- 1 user_id user_id   741 Jun 16  2010 INSTALL.txt
-rw-r--r-- 1 user_id user_id   821 Jan 21 23:49 PKG-INFO
-rw-r--r-- 1 user_id user_id  5198 Dec 17 00:50 README.config
-rw-r--r-- 1 user_id user_id  5241 Jan 18 01:14 README.new-usage
-rw-r--r-- 1 user_id user_id 14562 Nov 15 13:31 README.txt
drwxr-xr-x 4 root   root    4096 Jan 23 14:43 build
-rw-r--r-- 1 user_id user_id  5463 Jan 18 01:15 changelog
drwxr-xr-x 2 user_id user_id  4096 Jan 21 23:49 man
-rw-r--r-- 1 user_id user_id  3219 Jan 18 01:17 setup.py
drwxr-xr-x 3 user_id user_id  4096 Jan 21 23:49 src
-rw-r--r-- 1 user_id user_id     0 Feb 16 01:42 typescript
[user_id@server googlecl-0.9.12]$ exit
exit
Script done, file is typescript
[user_id@server googlecl-0.9.12]$ cat -v typescript
Script started on Wed Feb 16 01:42:06 2011
^[]0;user_id@server:~/googlecl-0.9.12^G^[[?1034h[user_id@server googlecl-0.9.12]$ ls -l^M
total 64^M
-rw-r--r-- 1 user_id user_id   741 Jun 16  2010 INSTALL.txt^M
-rw-r--r-- 1 user_id user_id   821 Jan 21 23:49 PKG-INFO^M
-rw-r--r-- 1 user_id user_id  5198 Dec 17 00:50 README.config^M
-rw-r--r-- 1 user_id user_id  5241 Jan 18 01:14 README.new-usage^M
-rw-r--r-- 1 user_id user_id 14562 Nov 15 13:31 README.txt^M
drwxr-xr-x 4 root   root    4096 Jan 23 14:43 ^[[0m^[[01;34mbuild^[[0m^M
-rw-r--r-- 1 user_id user_id  5463 Jan 18 01:15 changelog^M
drwxr-xr-x 2 user_id user_id  4096 Jan 21 23:49 ^[[01;34mman^[[0m^M
-rw-r--r-- 1 user_id user_id  3219 Jan 18 01:17 setup.py^M
drwxr-xr-x 3 user_id user_id  4096 Jan 21 23:49 ^[[01;34msrc^[[0m^M
-rw-r--r-- 1 user_id user_id     0 Feb 16 01:42 typescript^M
^[]0;user_id@server:~/googlecl-0.9.12^G[user_id@server googlecl-0.9.12]$ cat^H ^H^H ^H^H ^Hexit^M
exit^M

Script done on Wed Feb 16 01:42:14 2011
[user_id@server googlecl-0.9.12]$
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.