Troubleshooting and Bash

Troubleshooting

General Steps

  1. Understand the problem
  2. Reproduce the problem (this sometimes loops back to step 1)
  3. Isolate the change that created the problem
  4. Test the fix
  5. Automate the fix to your entire infrastructure

Example Problem

Symptoms

Reproducing the problem

Understanding the problem

Complications

More symptoms

Temp Solution

Permanent Solution

Example Problem 2

Symptoms

Debugging

Cause

Fix

Bash

Basics

Useful Symbols

$ grep 'searchstring' files/* | less

$ true || echo 'never gets here'
$ false && echo 'never gets here'

$ echo 'this now an error message' 1>&2 | grep -v error
this is now an error message

!$ # last argument to last command
$ cat /dir
cat: /dir/: Is a directory
$ cd !$
cd /dir
$ pwd
/dir

More Useful Symbols

$ for x in 1 2 3; do echo $x; done # Use seq for longer sequences
1
2
3

$ var='this is a var'; echo ${var//this is } # Deletes 'this is '
a var

$ ls -l `which bash`
-rwxr-xr-x 1 root root 1029624 Nov 12 15:08 /bin/bash

Combining These Together

$ set -a blocks
$ blocks="10.0.0.0/24"
$ set -a ips
$ ips=`fping -g 10.0.0.0/24 2>&1 | grep unreachable | tr \\  \\n`
$ for ip in $ips; do
$   nmap -p 22 $ip && ips=`echo ${ips//$ip} \
    | tr -s \\n`
$ done
$ echo $ips

Function Definitions

name () {
# code goes here
}

Internal Variables

You should know the following:

Variable Meaning
$* All arguments passed
$? Return code of last command run
"$@" All arguments passed as a list
$CDPATH Colon-delimited list of places to look for dirs
$HOME Location of user homedir
$IFS Internal Field Seperator
$OLDPWD Previous PWD

Internal Variables

Variable Meaning
$PATH Colon-delimited list of places to find executables
$PWD Present Working Directory
$SHELL Path to running shell
$UID User ID
$USER Username

You should also read the EXPANSION section of the bash man page.

Useful Userland Utils

awk
cat
cd
cut
grep
ls
lsw
lsx
mtr
nc/netcat
pwd
rev
sed
seq
sort
tar
tr
uniq
w
wc

IFS

Every char in $IFS bash considers a seperator between words.

#!/bin/bash

var1=1-2-3
var2=2+3+4

IFS=-

echo $var1
echo $var2

IFS=+

echo $var1
echo $var2

Advanced Bash Scripting Guide

The advanced bash scripting guide is very useful.

In particular, part 5 contains a lot of useful information.

Readings