I was recently debugging an issue with a shell script, and noticed that the shell was exiting with an exit code greater than 100 when it received a SIGTSTP signal:

$ cat test

#!/bin/bash
sleep 60

# Window one
$ ./test
[1]+ Stopped ./test
Home:~ matty$ echo $?
146

# Window two
$ kill -18 4667

I was curious where the exit value of 146 came from, so I did a bit of digging. It turns out that when a shell exits due to an uncaght signal, the signal number is added to 128 and that is the value that is returned. So in the case above, the exit code 146 was returned. I digs me some random shell knowledge.

Posted by matty, filed under UNIX Shell. Date: November 20, 2008, 12:19 am | No Comments »

If you’re a frequent user of the bash shell, I would suggest taking a peek at the GNU reference guide next time you have a chance.  There are a lot of cool built in functions/commands within bash that are pretty neat.  To get an idea of what these built in commands are:

$ ps
PID TTY          TIME CMD
15997 pts/2    00:00:00 bash
23625 pts/2    00:00:00 ps

$ which enable
/usr/bin/which: no enable in (/bin:/usr/bin:/usr/sbin:/sbin)

$ enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable continue
enable declare
enable dirs
enable disown
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable fg
enable getopts
enable hash
enable help
enable history
enable jobs
enable kill
enable let
enable local
enable logout
enable popd
enable printf
enable pushd
enable pwd
enable read
enable readonly
enable return
enable set
enable shift
enable shopt
enable source
enable suspend
enable test
enable times
enable trap
enable true
enable type
enable typeset
enable ulimit
enable umask
enable unalias
enable unset
enable wait

Some of these do have binaries within the /usr or /bin namespace, while others do not.  Bash's internal built in definition of these commands is what actually gets executed...

$ which cd
/usr/bin/which: no cd in (/bin:/usr/bin:/usr/sbin:/sbin)
$ which echo
/bin/echo
$ which eval
/usr/bin/which: no eval in (/bin:/usr/bin:/usr/sbin:/sbin)
$ which exit
/usr/bin/which: no exit in (/bin:/usr/bin:/usr/sbin:/sbin)
$ which kill
/bin/kill

Some of these are pretty intuitive why they should be built-in commands (cd, exit, etc.)  Looking through the GNU reference guide describes what all of these do.  There is also a built-in called "help" which describes some of this without going to the GNU reference guide.

$ which help
/usr/bin/which: no help in (/bin:/usr/bin:/usr/sbin:/sbin)
$ help cd
cd: cd [-L|-P] [dir]
Change the current directory to DIR.  The variable $HOME is the
default DIR.  The variable CDPATH defines the search path for
the directory containing DIR.  Alternative directory names in CDPATH
are separated by a colon (:).  A null directory name is the same as
the current directory, i.e. `.’.  If DIR begins with a slash (/),
then CDPATH is not used.  If the directory is not found, and the
shell option `cdable_vars’ is set, then try the word as a variable
name.  If that variable has a value, then cd to the value of that
variable.  The -P option says to use the physical directory structure
instead of following symbolic links; the -L option forces symbolic links
to be followed.
I found the “bind” built in to be super useful.  Bash has shortcuts that allow you to move the cursor around, search history, etc. and I always find myself forgetting what the keyboard shortcut / sequence is.  When this happens, I just use the “bind” (not DNS!) built in to find what I’m looking for…

$ bind -p

Displays all “bindings” of keyboard shortcuts to variables.   Here, I want to see what keyboard shortcut I can use to search through my command history to find a command…

$ bind -p | grep reverse-search-history
“\C-r”: reverse-search-history

Ah, so its Ctrl-r..  Sure enough, hitting ctrl-r at a command prompt brings up the reverse search prompt through my history and I can look for any command…  Looking for the command “cat” seems to have found me looking through /etc/passwd.

$

(reverse-i-search)`cat’: cat /etc/passwd

Hitting ctrl-r multiple times once command is found will continue to search backwards through the history.

(reverse-i-search)`cat’: cat /proc/kallsyms | more

Reverse history search is pretty useful, but there are so many other neat features of bash that I find invaluable.  What if I have created some super long command with multiple pipes and I want to move the cursor to the beginning of the line?

$ bind -p | grep beginning-of-line
“\C-a”: beginning-of-line
“\eOH”: beginning-of-line
“\e[1~": beginning-of-line
"\e[H": beginning-of-line

Well, it looks like there are a few defined shortcuts, but ctrl-a seems to be a winner.  Sure enough..

$ cat /etc/passwd | awk -F: '{print $7}' | sed 's,bin,mike,'[]

$ [c]at /etc/passwd | awk -F: ‘{print $7}’ | sed ’s,bin,mike,’

Take a peek!  You might find yourself saving some keystrokes.

$ help bind
bind: bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
Bind a key sequence to a Readline function or a macro, or set
a Readline variable.  The non-option argument syntax is equivalent
to that found in ~/.inputrc, but must be passed as a single argument:
bind ‘”\C-x\C-r”: re-read-init-file’.
bind accepts the following options:
-m  keymap         Use `keymap’ as the keymap for the duration of this
command.  Acceptable keymap names are emacs,
emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
vi-command, and vi-insert.
-l                 List names of functions.
-P                 List function names and bindings.
-p                 List functions and bindings in a form that can be
reused as input.
-r  keyseq         Remove the binding for KEYSEQ.
-x  keyseq:shell-command  Cause SHELL-COMMAND to be executed when
KEYSEQ is entered.
-f  filename       Read key bindings from FILENAME.
-q  function-name  Query about which keys invoke the named function.
-u  function-name  Unbind all keys which are bound to the named function.
-V                 List variable names and values
-v                 List variable names and values in a form that can
be reused as input.
-S                 List key sequences that invoke macros and their values
-s                 List key sequences that invoke macros and their values
in a form that can be reused as input.

Want to see if your current shell has history enabled, or some other key bash features?  The GNU Bash Reference guide has details on what all of these variables are.. Like checkwinsize.

$ shopt
cdable_vars     off
cdspell         off
checkhash       off
checkwinsize    on
cmdhist         on
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
gnu_errfmt      off
histappend      off
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
interactive_comments    on
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off
$ help shopt
shopt: shopt [-pqsu] [-o long-option] optname [optname...]
Toggle the values of variables controlling optional behavior.
The -s flag means to enable (set) each OPTNAME; the -u flag
unsets each OPTNAME.  The -q flag suppresses output; the exit
status indicates whether each OPTNAME is set or unset.  The -o
option restricts the OPTNAMEs to those defined for use with
`set -o’.  With no options, or with the -p option, a list of all
settable options is displayed, with an indication of whether or
not each is set.

checkwinsize
If set, Bash checks the window size after each command and, if
necessary, updates the values of LINES and COLUMNS.

Ah nice.  My shell wont send a SIGHUP to jobs while i’m logging out of an interactive shell.  =)

Posted by mike, filed under UNIX Shell. Date: July 9, 2008, 8:57 am | 1 Comment »

I had to do some pattern matching last week, and needed a way to print the two lines that occurred after each line that matched a specific string. Since awk provides robust pattern matching, I came up with the following awk command line to grab the information I needed:

$ cat test
foo
1
2
3
foo
1
2
3
$ awk ‘BEGIN { i=0 } /foo/ { while (i < 2 ) { getline; print $0; i++ } i=0}' test
1
2
1
2

I digs me some awk!

Posted by matty, filed under UNIX Shell. Date: December 3, 2007, 8:52 pm | 4 Comments »

09  Feb
Not grep!

While reviewing some shell scripts last week, I saw the infamous find | grep:

$ /usr/bin/find /foo -type f | egrep -v \*.inp

I am not real sure why more people don’t leverage the logic operations build into find:

$ /usr/bin/find /foo -type f -not -name \*.inp

This saves a fork() and exec(), and should be a bit faster. I am curious if folks use grep because it’s easier to read, or because they don’t know about the logic operations built into find. I shall need to investigate …

Posted by matty, filed under UNIX Shell. Date: February 9, 2007, 10:11 pm | 5 Comments »

I periodically need to format text data on my Linux desktop for printing, and have always gone about this in one of two ways. If I want to add margins and make the data conform to a specific page length, I use the pr utility. Here is an example that formats FILE with a #4 margin, a #72 width (the default), and a length of 60 lines:

$ pr -o 4 -l 60 FILE

If I have a text document that I want to format to 80-characters per line, I typically use fmt with the width option. The following example will force all lines to 80 characters, and will gracefully wrap lines if they exceed this limit:

$ fmt -80 FILE

If you have any other methods for formatting text, I would love to hear them.

Posted by matty, filed under UNIX Shell. Date: January 6, 2007, 12:08 pm | 2 Comments »

I use bash as my primary shell, and have come to rely on the following bash short cuts:

alt-f   -- move forward one word
alt-b  -- move backwards one word
ctrl-a  -- takes you to the begining of the command you are currently typing.
ctrl-b  -- move backwards one character
ctrl-c  -- kills the current command or process.
ctrl-d  -- kills the shell.
ctrl-e  -- takes you to the end of the command you are currently typing in.
ctrl-f  -- move forward one character
ctrl-h  -- deletes one letter at a time from the command you are typing in.
ctrl-l  -- clear screen
ctrl-r  -- does a search in the previously given commands so that you don't have to repeat long command.
ctrl-u  -- clears the typing before the hotkey.
ctrl-z  -- puts the currently running process in background
esc-b  -- takes you back by one word while typing a command.
esc-p  -- like ctrl-r lets you search through the previously given commands.
esc-.  -- gives the last command you typed.

Posted by matty, filed under UNIX Shell. Date: December 1, 2006, 3:41 pm | No Comments »

GNU date has some nifty options, and is a time keepers toolbox rolled up into an ELF executable. One really cool option in GNU date is the ability to print a date in the past using the the “–date” option, and the “days ago” format string:

$ date –date=”30 days ago”
Wed Aug 30 15:15:51 EDT 2006

$ date –date=”10 years ago”
Sun Sep 29 15:15:56 EDT 1996

Now if only more vendors would include GNU date with their operating system. :)

Posted by matty, filed under UNIX Shell. Date: October 30, 2006, 7:30 pm | No Comments »

Bash is a great shell, and has numerous capabiltiies to make peoples life easier. One super nifty feature is the ability to search through the shell’s history by hitting cntrl-r (control then the letter ‘r’):

$ cntrl-r
(reverse-i-search)`meta’: metastat

Once you locate the command you want to run, you simply hit enter to execute it. Good stuff!

Posted by matty, filed under UNIX Shell. Date: June 17, 2006, 9:25 pm | 1 Comment »

« Previous Entries