I’ve been working with Linux/Unix for quite a while and today I came across something I hadn’t used in a long time… The script command.
The script command allows you to capture the output of a whole bunch of commands into a file which can be quite useful. Let’s say that you want to run a bunch of commands and send the output of those commands to someone else as a file. The script command makes it pretty easy!
Just start your script with the script command followed by a file name for your output and when you are done typing all your commands, just type exit.
The script command also adds script started and script ended times to the the start and end of your file.
See below for an example:
[richard@oraclelinux ~]$ script output_of_a_bunch_of_commands.log
Script started, file is output_of_a_bunch_of_commands.log
[richard@oraclelinux ~]$ id
uid=54323(richard) gid=54323(richard) groups=10(wheel),54323(richard)
[richard@oraclelinux ~]$ env
HOSTNAME=oraclelinux
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.254.46 65291 22
SSH_TTY=/dev/pts/1
USER=richard
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/richard/bin
MAIL=/var/spool/mail/richard
PWD=/home/richard
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
HOME=/home/richard
SHLVL=2
LOGNAME=richard
SSH_CONNECTION=192.168.254.46 65291 192.168.1.20 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
DISPLAY=localhost:10.0
G_BROKEN_FILENAMES=1
_=/bin/env
[richard@oraclelinux ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
[richard@oraclelinux ~]$ exit
exit
Script done, file is output_of_a_bunch_of_commands.log
[richard@oraclelinux ~]$
A quick look at the file shows that you’ve saved all that ouput.
[richard@oraclelinux ~]$ cat output_of_a_bunch_of_commands.log
Script started on Fri 24 Jul 2015 11:42:10 PM EDT
[richard@oraclelinux ~]$ id
uid=54323(richard) gid=54323(richard) groups=10(wheel),54323(richard)
[richard@oraclelinux ~]$ env
HOSTNAME=oraclelinux
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.254.46 65291 22
SSH_TTY=/dev/pts/1
USER=richard
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/richard/bin
MAIL=/var/spool/mail/richard
PWD=/home/richard
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
HOME=/home/richard
SHLVL=2
LOGNAME=richard
SSH_CONNECTION=192.168.254.46 65291 192.168.1.20 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
DISPLAY=localhost:10.0
G_BROKEN_FILENAMES=1
_=/bin/env
[richard@oraclelinux ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
[richard@oraclelinux ~]$ exit
exit
Script done on Fri 24 Jul 2015 11:43:05 PM EDT
[richard@oraclelinux ~]$
Leave a Reply