Monthly Archives: July 2015

New command for today… script

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 ~]$
Advertisement

CTRL-r is your friend!

You already knew that the up and down arrows in a Linux terminal window allow you to navigate through previously typed commands, but did you know that you can also search through your command history?

CTRL-r is great for finding those commands that are a bit tedious to type.

When you type CTRL-R in a terminal window your whole prompt turns into this strange looking text:

(reverse-i-search)`':

As you type characters the first command in your history, starting from the most recent and working back towards the top will be displayed.

Let’s say you’ve typed the following list of commands:

sqlplus / as sysdba
rman target "'/ as sysbackup'"
ls
ls -als

At a command prompt you type CTRL-r and then type s. Your prompt will now look like this:

(reverse-i-search)`s': ls -als

And the s in the als will be highlighted. What you’ve done here is found the first s in your command history.

If you type Enter right now, you’ll rerun that command.

Things get more interesting when you start typing more letters. Instead of typing just s, type sq and you’ll get the following:

(reverse-i-search)`sq': sqlplus / as sysdba

Hitting Enter will rerun that sqlplus command.

What if you want to find something that you typed earlier than the last command that matched? Just type CTRL-r again. Each time you press CTRL-r you’ll find older and older strings that match. If you have a short search string (just s for example), then it’s not unusual to find that string in a single command multiple times. Once you’ve exhausted all the matches in one command, you’ll reach back into your history to find another that matches.

What if you want to find the last thing that you found with CTRL-r?

Start a new search (or just use backspace to remove any characters you’ve started typing) and press CTRL-r again. Let’s say that you used rm as your last successful search and retrieved the rman command from the above list of commands. You then ran a few more commands and decided to run rman again… Just type CTRL-r twice. The first CRTL-r says “I want to search through my history”, the second CTRL-r says “Remember the last successful search I used? I want that one again.”

What if you don’t want to run the command you found, but instead you just put the command at your prompt so you can edit it? Instead of hitting Enter after you find a command, just press Esc. This dumps the text of the command into your terminal at a command prompt.

Here’s where things get really cool… If you use rlwrap for various Oracle commands like sqlplus, rman, etc. (See this post to install rlwrap, and this post to configure the aliases to use with rlwrap) then CTRL-r will also work in those commands too!

Want to find the last alter user command that you ran? In sqlplus just type your good friend CTLR-r and start typing alter. You probably won’t have to actually type all the letters in alter and soon you’ll have the following:

(reverse-i-search)`al': alter user dbsnmp identified by oracle_4U account unlock;

Technically CTRL-r finds the spot in your history where the command you found lives, so once you’ve found a command, you can use the up and down arrows to cycle through the commands that are near that command. What was that command that I typed right after I did that alter user command? Just press the down arrow key and you’ve found it!

There are a few idiosyncrasies with CTRL-r. First it is case sensitive so you’ll have to remember if you used SELECT or select. Second it can be kind of easy to get it into a strange place with the characters you are searching for if you start typing characters that are not actually in your command history CTLR-r will beep at you a bunch and you may or may not be able to use the backspace key to get back to where you want to be… Just use Esc to get out of the CTRL-r search and try again.

Also you may want to give CTRL-r more history to work with. If you’d like to do this you’ll need export two different environment variables, HISTSIZE and HISTFILESIZE.

I added the following to my .bash_profile file in my home directory:

# Let's give the history command and CTRL-r more to work with!
export HISTSIZE=10000
export HISTFILESIZE=10000

CTRL-r has saved me much typing over the years, I hope it helps you too!