Unix

Recommended Books (click to access Amazon link)

   

 

 

Programmer’s Guide

Terminal, File System, Users and Editors

* Reference at bottom

1. Command-line “Terminal”

Programmers uses command-line to enter commands to the operating system, instead of graphical user interface (GUI). This is because command-line is much more flexible than graphical interface, e.g., you can provide additional options to a command; you can pipe the output of one command into another command; you can put a set of commands in a script to automate a task.

A shell (or command interpreter) is a program that lets you issue commands to interact with the operating system, via a text-based interface. It provides a set of commands and utilities. It also has its own programming language for writing shell scripts. There are many shell programs available, from the legacy and obsoletedsh (Bourne Shell), csh (C Shell), ksh (Korn Shell), to newer bash (Bourne Again Shell), tcsh (Tenex C Shell).

Terminal is an application that runs a shell program. By default, the Terminal in Ubuntu and Mac OS X runs the bash shell, although you are free to switch into another shell program, such as tcsh.

To launch a Terminal:

  • In Mac OS X: Open “Finder” ? Go ? Utilities ? Select “Terminal”. Drag the “Terminal” to your dock since you need to use it frequently.
  • In Ubuntu: Open “Dash” ? type “Terminal”; or choose “Applications” lens ? Installed ? Select “Terminal”. Drag the “Terminal” to your Launcher since you need to use it frequently.

A Terminal displays a command prompt ending with “$” sign, in the form of:

  • In Mac OS X: “ComputerName:CurrentDirectory Username$
  • In Linux: “Username@ComputerName:CurrentDirectory$

You can enter commands after the command prompt. For example, enter “pwd” to print the current working directory:

$ pwd
.......

In this article, I shall denote the command prompt simply as “$“.

2. Unix File System

Root Directory

Everything in Unix is a file – data files, program files, even input and output device files. Files are organized in directories (aka folders). The directories are organized in a hierarchical tree structure, starting from the root directory. A directory may contain sub-directories and files. A sub-directory may contain sub-sub-directories and files. A file is identified via the directories and filename, e.g., “/usr/lib/jvm/jdk1.7.0_07/bin/javac“. The leading/” (forward slash) denotes the root directory. The sub-directories are also separated by a “/“.

There is only one root directory for the entire Unix’s file system.

Notes: Windows use “\” (back slash) as the directory separator, and may contain multiple root directories – one for each drive (e.g., c:\, d:\).

Home Directory

Unix is a multi-user operating system (although most of you, in particular the Mac users, use it as a single-user personal computer). Each user on the system is allocated a directory for storing his files, known as home directory. The users’ home directories are allocated under /home for Ubuntu, or /Users for Mac OS X, with a sub-directory name the same as the username (or login ID). The home directory of the current login user is denoted as “~“. It contains sub-directories such as ~/Desktop, ~/Downloads, ~/Documents, ~/Movies, ~/Music, and etc.

Pathname and Filename

To reference a file, you need to provide the pathname (directory and sub-directories names) and the filename. For example, in “/usr/lib/jvm/jdk1.7.0_07/bin/javac“, the pathname is “/usr/lib/jvm/jdk1.7.0_07/bin/” and the filename is “javac“.

The pathname can be specified in two ways:

  1. Absolute Pathname: An absolute path begins from the root directory (starts with “/“), and contains all the sub-directories (separated with “/“) leading to the file, e.g., “/usr/lib/jvm/jdk1.7.0_07/bin/“. An absolute path can also begin with the current user’s home directory (starts with “~”), e.g., “~/Downloads/jdk“. Suppose the current user is “peter”, it maps to “/home/peter/Downloads/jdk” (in Linux) or “/Users/peter/Downloads/jdk” (in Mac).
  2. Relative Pathname: A relative path is relative to the so-called current working directory. A relative path does not begin with “/” or “~“. For example, if the current working directory is “/usr/lib/jvm“, then the relative pathname “jdk1.7.0_07/bin” refer to “/usr/lib/jvm/jdk1.7.0_07/bin“.

Unix system is case sensitive, a rose is NOT a Rose, and is NOT a ROSE.

3. Basic Commands

3.1 pwd (Print Current Working Directory)

The Terminal session maintains a so-called current working directory. All relative pathnames/filenames are relative to the current working directory. To display the current directory, issue command “pwd” (print working directory):

// Print Current Working Directory
$ pwd
......

When a Terminal is launched, it sets the initial working directory to the home directory of the current login user (denoted as “~“).

The current working directory is often included as part of the command prompt.

3.2 cd (Change Working Directory)

To change current working directory, issue command “cd new-pathname” (change directory). You can specify new pathname in two ways: absolute or relative. An absolute path begins with a “/” (root directory) or “~” (home directory). A relative path is relative to the current working directory and does NOT begin with “/” or “~“. For example,

$ cd /// Change directory (absolute) to the root
$ cd /usr/local// Change directory (absolute) to "/usr/local"
$ cd mysql// Change directory (relative) to mysql of the current directory
$ cd myproject/bin// Change directory (relative) to myproject/bin of the current directory

You can cd in multiple stages (e.g., one cd for each sub-directory – recommended), or cd in a single stage with the full pathname.

$ cd /// "/"
$ cd usr// "/usr"
$ cd local// "/usr/local"
$ cd mysql// "/usr/local/mysql"
$ cd bin// "/usr/local/mysql/bin"
 
// Same As
$ cd /usr/local/mysql/bin

You can use “/” to denote the root; “~” to refer to the home directory of the current login user; “..” (double-dot) to refer to the parent directory; “.” (single-dot) to refer to the current directory; and “” (dash) to refer to the previous directory. For example,

$ cd ~// Change directory to the home directory of the current user
$ cd// same as above, default for "cd" is home directory
$ cd ~/Documents// Change directory to the sub-directory "Documents" of the home directory of the current user
$ cd ..// Change directory to the parent directory of the current working directory
$ cd -// Change directory to the previous working directory (OLDPWD)

Setting proper working directory can greatly simplify your work. For example, to compile a Java program called “Hello.java” in “~/myproject/java“:

  1. Set the working directory to “~/myproject/java“, and reference the file with filename only (without the path):
    $ cd ~/myproject/java// Set the working directory
    $ javac Hello.java// Filename only, in current directory
  2. You can also refer to a file with its full pathname in any working directory:
    // Any working directory
    $ javac ~/myproject/java/Hello.java// Using fully-qualified filename

Although setting working directory is NOT really a necessity, it can be very messy at times without the correct working directory (e.g., the output file “Hello.class” is created in the current directory). You should always set a proper working directory before issuing any command with file references.

3.3 ls (List Directory’s Contents)

You can use command ls to list the contents of the current working directory, e.g.,

// List contents of current working directory in short format
$ ls
Desktop    Downloads         Music     Public     Videos
Documents  examples.desktop  Pictures  Templates
 
// List in long format
$ ls -l
total xx
drwxr-xr-x 2 myuser myuser 1024 Mar 22 21:32 Desktop
drwxr-xr-x 2 myuser myuser 1024 Mar 22 21:32 Documents
drwxr-xr-x 2 myuser myuser 1024 Mar 22 21:32 Downloads
-rw-r--r-- 1 myuser myuser 8445 Mar 22 17:30 examples.desktop
......
 
// Append file indicator (e.g., trailing / indicates a directory)
$ ls -F
Desktop/    Downloads/        Music/     Public/     Videos/
Documents/  examples.desktop  Pictures/  Templates/
Wildcard *

You can list selected files using wildcard *, which matches 0 or more (any) characters. For example,

$ ls *.java// List files ending with ".java" in short format (default)
$ ls -l *.java// List files ending with ".java" in long format
$ ls -ld my*// List files and directories beginning with "my" in long format

You could, of course, view the contents of a directory using a File Manager (such as “Finder” in Mac, or “Home Folder” in Ubuntu) more conveniently.

3.4 cat, less (Viewing File Contents)

You can use commands cat, less or more to display contents of a text file on console. cat shows the entire file, which can be inconvenient for long file. less shows one page of the file. For example,

$ cat /proc/cpuinfo// Display the entire file
  // Use window's scroll-bar to scroll
 
$ less /proc/cpuinfo// Display one page of the file
  // Use Up|Down|PgUp|PgDown key to scroll, and press "q" to quit

3.5 Shortcut Keys – IMPORTANT

Previous Commands in Command History: You can use the up/down arrow keys to retrieve the previous/next command in the command history.

Auto-Complete: You can type the first few characters for the pathname or filename, and press TAB key to auto-complete.

Copy/Paste:

  • In Mac OS X: use Cmd+C and Cmd+V.
  • In Ubuntu: use Shift+Ctrl+C and Shift+Ctrl+V. You can also use middle mouse-button to copy/paste the highlighted text on the command-line. (TheCtrl+C is used as interrupt signal; Ctrl+D for end of text input; Ctrl+Z for suspending current process.)

3.6 Other Frequently-used Commands

$ clear// Clear Screen
$ who// List all login users
$ whoami// Show the current login user
$ which program-name// Show the location of the program, e.g., "which javac"

3.7 Getting Helps

$ help// Show all the available commands
$ help command-name// Show help for a specific command, e.g., "help cd"
$ man command-name// Show the manual page for a specific command, e.g., "man cd"

4. If you need to Install Software or Perform System tasks…

4.1 Regular Users vs. Superuser

Unix is a multi-user operating system. Each user is identified via an username (or login ID). It is allocated a home directory, typically called /home/username (for Ubuntu) or /Users/username (for Mac OS X). Each user is also assigned to a group.

User and group are used for access control. A “regular” user can only modify resources specific to his account, and cannot modify other user’s resources or perform system-wide changes.

On the other hand, superuser (or privileged user) has unrestricted access to the entire system. Superuser can carry out any task, even destructive ones. Using superuser to perform daily operations can be dangerous. A special superuser, called root, is created during the installation. However, in many system (such as Ubuntu and Mac OS X), the root account is disabled by default for safety and security.

You should perform daily works with a regular user. However, at times, you need the superuser privilege to perform a task, such as install a new software or launch a system program.

An authorized user can run a command with superuser privilege by prefixing it with sudo (superuser do). The system will prompt you for password. Reply with the login (authorized) user’s password. The entered password will not be shown (not even showing asterisks) for maximum security. The password entered is stored for 15 minutes (by default) for all subsequent sudo commands. After that, you will need to enter the password again. For example,

$ cat /etc/shadow
cat: /etc/shadow: Permission denied  // regular user has no permission to view this file
$ sudo cat /etc/shadow
[sudo] password for myuser:          // entered password will not be shown
......

To enable a user to sudo:

  • In Ubuntu: System Settings ? User Accounts ? Select the user ? Unlock ? Set the account type to “Administrator” (instead of “Standard User”), which adds the user to “sudo” group.
  • In Mac OS X: System Preferences ? Users and Groups ? Select the user ? Unlock ? Check “Allow users to administer this computer”.

4.2 Processes

Unix is a multi-process, multi-user operating system. It can run many processes concurrently.

You can use GUI applications to view all running processes and terminate a particular process (similar to “Task Manager” in Windows).

  • In Mac OS X: launch “Activity Monitor” (Under /Applications/Utilities) and select “All Processes”.
  • In Ubuntu: launch “System Monitor” (search from Dash) and select “Processes”.

On command-line, you can use command ps to list all the processes and kill to terminate a particular process:

$ ps// Print processes of current user
$ ps -e// Print all processes
$ ps -ef// Print all processes in full-listing
$ ps -ef | grep "mysql"// Print processes containing mysql
$ ps aux | grep "mysql"// same as above
$ top// Display resource usage and top processes
 
$ kill pid// Kill a particular possess with the given processID
$ kill -9 pid// Force kill

5. Programming Text Editors

A program editor (or source code editor) is programming language sensitive and context-aware. It highlights the syntax elements of your programs; and provides many features that aid in your program development (such as auto-complete, compile/build/run, help menu, etc.). On the other hand, a plain text editor is not language-sensitive and, therefore, is NOT suitable for writing programs. For full-scale software development, you should use an appropriate IDE (Integrated Development Environment).

It is important to use a mono-space font (such as “Courier”, “Consola”) for programming, so that the columns are properly aligned.

5.1 Graphical Programming Text Editors – gedit, jedit, sublime and others

A good graphical programming text editor, which provides syntax highlighting, is crucial in programming and development.

  • In Ubuntu, the default “gedit” is an excellent programming text editor.
  • In Mac OS X, you can use the default “TextEdit” to create plain text file by choosing the option “Make Plain Text” (under “Format”). However, TextEdit is NOT a programming text editor and does not provide syntax highlighting. I strongly strongly suggest that you install gedit, jedit, sublime or other programming text editors.

Other popular graphical editors include kate (KDE text editor) and kedit (another KDE editor).

5.2 Display Text Files in Command-line – cat, less, head and tail

If possible, use a graphical text editor to display a text file. Nonetheless, you can use the following commands to display a text file in command-line:

  • cat filename: Concatenate file or print the file to console.
  • less filename: Display the file in pages. You can scroll the display with Up, Down, PageUp, PageDown keys. less replaces the legacy more.
  • head|tail filename: Display the first|last part of the file.

For example, let’s display the /proc/cpuinfo file with these commands:

$ cat /proc/cpuinfo// Show entire file
   // Use the window's scroll-bar to scroll
 
$ less /proc/cpuinfo// Show one page
   // Use the Up|Down|PgUp|PgDown key to scroll
 
$ head /proc/cpuinfo// Show first page of the file
 
$ tail /proc/cpuinfo// Show last page of the file
More on cat

The command cat has many usages. You can use cat to concatenate (join) a few files. For example,

$ cat file1 file2 file3// display file1, file2 and file3 on the console
 
$ cat file1 file2 file3 > file4// Concatenate file1, file2 and file3 as file4

You can also use cat command to create a small text file. cat without argument uses standard input as its input. You could use Ctrl+D to signal EOF. For example:

$ cat > out.txt
The quick brown fox jumps over the lazy dog[Ctrl+D]
$ cat out.txt
The quick brown fox jumps over the lazy dog

5.3 Console-based Text Editors – nano, vim, emacs

You should use graphical text editor if possible. Use console-based text editor as the last resort.

Many console-based text editors are available, such as: ed, jed, joe, mcedit, nano, red, sed, vi, vim, emacs, and etc.

nano

nano is small, simple and easy-to-use text editor, that is suitable for creating/editing small files. nano is a GNU text editor available for Mac and Unix Systems.nano replaces its predecessor called pico.

To start nano, open a Terminal and issue:

$ nano// Create a new file
$ nano filename// Edit an existing file

To exist nano, press Ctrl+X, followed by “y” (yes) to save the file.

You can run nano with sudo for editing restricted system files, as follows:

$ sudo nano filename// Run nano with superuser to edit an existing system file
vim (or vi)

An improved version of the classical vi (visual) text editor. Launch via “vim filename“.

vim operates in two mode: insert mode (for text editing) and command mode (for issuing commands such as quit and save). Press “i” to enter insert mode and start editing. Press ESC to switch to command mode.

The frequently-used commands are:

  • :q” to quit; “:q!” to quit without saving.
  • :w” to write (save); “:w filename” to save to the filename.
  • :s/foo/bar/g” to replace bar with foo for the current line, all occurrences (with option g, you can also use option c to ask for comfirmation, and i for case-insensitive).
  • :%s/foo/bar/gci” to replace bar with foo for the all line (option g for global, option c to ask for comfirmation, and option i for case-insensitive).
  • :/word to search forward and :?word to search backward.

You could run vimtutor to learn about vim.

emacs

The comprehensive GNU emacs editing environment.

 

Reference

Quick
—————————————

Directory Copy
cp -R Directory1/ DIR2

File Copy
cp /path/to/filename /path/to/your/folder_name

Delete Directory
rm -rf DirectoryName

Delete File
rm filename.txt

—————————————

Full
# 0. Shortcuts.

CTRL+A # move to beginning of line
CTRL+B # moves backward one character
CTRL+C # halts the current command
CTRL+D # deletes one character backward or logs out of current session, similar to exit
CTRL+E # moves to end of line
CTRL+F # moves forward one character
CTRL+G # aborts the current editing command and ring the terminal bell
CTRL+J # same as RETURN
CTRL+K # deletes (kill) forward to end of line
CTRL+L # clears screen and redisplay the line
CTRL+M # same as RETURN
CTRL+N # next line in command history
CTRL+O # same as RETURN, then displays next line in history file
CTRL+P # previous line in command history
CTRL+R # searches backward
CTRL+S # searches forward
CTRL+T # transposes two characters
CTRL+U # kills backward from point to the beginning of line
CTRL+V # makes the next character typed verbatim
CTRL+W # kills the word behind the cursor
CTRL+X # lists the possible filename completefions of the current word
CTRL+Y # retrieves (yank) last item killed
CTRL+Z # stops the current command, resume with fg in the foreground or bg in the background

DELETE # deletes one character backward
!! # repeats the last command
exit # logs out of current session

# 1. Bash Basics.

export # displays all environment variables

echo $SHELL # displays the shell you’re using
echo $BASH_VERSION # displays bash version

bash # if you want to use bash (type exit to go back to your normal shell)
whereis bash # finds out where bash is on your system

clear # clears content on window (hide displayed lines)

# 1.1. File Commands.

ls # lists your files
ls -l # lists your files in ‘long format’, which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified
ls -a # lists all files, including hidden files
ln -s # creates symbolic link to file
touch # creates or updates your file
cat > # places standard input into file
more # shows the first part of a file (move with space and type q to quit)
head # outputs the first 10 lines of file
tail # outputs the last 10 lines of file (useful with -f option)
emacs # lets you create and edit a file
mv # moves a file
cp # copies a file
rm # removes a file
diff # compares files, and shows where they differ
wc # tells you how many lines, words and characters there are in a file
chmod -options # lets you change the read, write, and execute permissions on your files
gzip # compresses files
gunzip # uncompresses files compressed by gzip
gzcat # lets you look at gzipped file without actually having to gunzip it
lpr # print the file
lpq # check out the printer queue
lprm # remove something from the printer queue
genscript # converts plain text files into postscript for printing and gives you some options for formatting
dvips # print .dvi files (i.e. files produced by LaTeX)
grep # looks for the string in the files
grep -r # search recursively for pattern in directory

# 1.2. Directory Commands.

mkdir # makes a new directory
cd # changes to home
cd # changes directory
pwd # tells you where you currently are

# 1.3. SSH, System Info & Network Commands.

ssh user@host # connects to host as user
ssh -p user@host # connects to host on specified port as user
ssh-copy-id user@host # adds your ssh key to host for user to enable a keyed or passwordless login

whoami # returns your username
passwd # lets you change your password
quota -v # shows what your disk quota is
date # shows the current date and time
cal # shows the month’s calendar
uptime # shows current uptime
w # displays whois online
finger # displays information about user
uname -a # shows kernel information
man # shows the manual for specified command
df # shows disk usage
du # shows the disk usage of the files and directories in filename (du -s give only a total)
last # lists your last logins
ps -u yourusername # lists your processes
kill # kills (ends) the processes with the ID you gave
killall # kill all processes with the name
top # displays your currently active processes
bg # lists stopped or background jobs ; resume a stopped job in the background
fg # brings the most recent job in the foreground
fg # brings job to the foreground

ping # pings host and outputs results
whois # gets whois information for domain
dig # gets DNS information for domain
dig -x # reverses lookup host
wget # downloads file

# 2. Basic Shell Programming.

# 2.1. Variables.

varname=value # defines a variable
varname=value command # defines a variable to be in the environment of a particular subprocess
echo $varname # checks a variable’s value
echo $$ # prints process ID of the current shell
echo $! # prints process ID of the most recently invoked background job
echo $? # displays the exit status of the last command
export VARNAME=value # defines an environment variable (will be available in subprocesses)

array[0] = val # several ways to define an array
array[1] = val
array[2] = val
array=([2]=val [0]=val [1]=val)
array(val val val)

${array[i]} # displays array’s value for this index. If no index is supplied, array element 0 is assumed
${#array[i]} # to find out the length of any element in the array
${#array[@]} # to find out how many values there are in the array

declare -a # the variables are treaded as arrays
declare -f # uses funtion names only
declare -F # displays function names without definitions
declare -i # the variables are treaded as integers
declare -r # makes the variables read-only
declare -x # marks the variables for export via the environment

${varname:-word} # if varname exists and isn’t null, return its value; otherwise return word
${varname:=word} # if varname exists and isn’t null, return its value; otherwise set it word and then return its value
${varname:?message} # if varname exists and isn’t null, return its value; otherwise print varname, followed by message and abort the current command or script
${varname:+word} # if varname exists and isn’t null, return word; otherwise return null
${varname:offset:length} # performs substring expansion. It returns the substring of $varname starting at offset and up to length characters

${variable#pattern} # if the pattern matches the beginning of the variable’s value, delete the shortest part that matches and return the rest
${variable##pattern} # if the pattern matches the beginning of the variable’s value, delete the longest part that matches and return the rest
${variable%pattern} # if the pattern matches the end of the variable’s value, delete the shortest part that matches and return the rest
${variable%%pattern} # if the pattern matches the end of the variable’s value, delete the longest part that matches and return the rest
${variable/pattern/string} # the longest match to pattern in variable is replaced by string. Only the first match is replaced
${variable//pattern/string} # the longest match to pattern in variable is replaced by string. All matches are replaced

${#varname} # returns the length of the value of the variable as a character string

*(patternlist) # matches zero or more occurences of the given patterns
+(patternlist) # matches one or more occurences of the given patterns
?(patternlist) # matches zero or one occurence of the given patterns
@(patternlist) # matches exactly one of the given patterns
!(patternlist) # matches anything except one of the given patterns

$(UNIX command) # command substitution: runs the command and returns standard output

# 2.2. Functions.
# The function refers to passed arguments by position (as if they were positional parameters), that is, $1, $2, and so forth.
# $@ is equal to “$1” “$2″… “$N”, where N is the number of positional parameters. $# holds the number of positional parameters.

functname() {
shell commands
}

unset -f functname # deletes a function definition
declare -f # displays all defined functions in your login session

# 2.3. Flow Control.

statement1 && statement2 # and operator
statement1 || statement2 # or operator

-a # and operator inside a test conditional expression
-o # or operator inside a test conditional expression

str1=str2 # str1 matches str2
str1!=str2 # str1 does not match str2
str1str2 # str1 is greater than str2
-n str1 # str1 is not null (has length greater than 0)
-z str1 # str1 is null (has length 0)

-a file # file exists
-d file # file exists and is a directory
-e file # file exists; same -a
-f file # file exists and is a regular file (i.e., not a directory or other special type of file)
-r file # you have read permission
-r file # file exists and is not empty
-w file # your have write permission
-x file # you have execute permission on file, or directory search permission if it is a directory
-N file # file was modified since it was last read
-O file # you own file
-G file # file’s group ID matches yours (or one of yours, if you are in multiple groups)
file1 -nt file2 # file1 is newer than file2
file1 -ot file2 # file1 is older than file2

-lt # less than
-le # less than or equal
-eq # equal
-ge # greater than or equal
-gt # greater than
-ne # not equal

if condition
then
statements
[elif condition
then statements…]
[else
statements]
fi

for x := 1 to 10 do
begin
statements
end

for name [in list]
do
statements that can use $name
done

for (( initialisation ; ending condition ; update ))
do
statements…
done

case expression in
pattern1 )
statements ;;
pattern2 )
statements ;;

esac

select name [in list]
do
statements that can use $name
done

while condition; do
statements
done

until condition; do
statements
done

# 3. Command-Line Processing Cycle.

# The default order for command lookup is functions, followed by built-ins, with scripts and executables last.
# There are three built-ins that you can use to override this order: `command`, `builtin` and `enable`.

command # removes alias and function lookup. Only built-ins and commands found in the search path are executed
builtin # looks up only built-in commands, ignoring functions and commands found in PATH
enable # enables and disables shell built-ins

eval # takes arguments and run them through the command-line processing steps all over again

# 4. Input/Output Redirectors.

cmd1|cmd2 # pipe; takes standard output of cmd1 as standard input to cmd2
> file # directs standard output to file
< file # takes standard input from file >> file # directs standard output to file; append to file if it already exists
>|file # forces standard output to file even if noclobber is set
n>|file # forces output to file from file descriptor n even if noclobber is set
<> file # uses file as both standard input and standard output
n<>file # uses file as both input and output for file descriptor n
<

# 5. Process Handling.

# To suspend a job, type CTRL+Z while it is running. You can also suspend a job with CTRL+Y.
# This is slightly different from CTRL+Z in that the process is only stopped when it attempts to read input from terminal.
# Of course, to interupt a job, type CTRL+C.

myCommand & # runs job in the background and prompts back the shell

jobs # lists all jobs (use with -l to see associated PID)

fg # brings a background job into the foreground
fg %+ # brings most recently invoked background job
fg %- # brings second most recently invoked background job
fg %N # brings job number N
fg %string # brings job whose command begins with string
fg %?string # brings job whose command contains string

kill -l # returns a list of all signals on the system, by name and number
kill PID # terminates process with specified PID

ps # prints a line of information about the current running login shell and any processes running under it
ps -a # selects all processes with a tty except session leaders

trap cmd sig1 sig2 # executes a command when a signal is received by the script
trap “” sig1 sig2 # ignores that signals
trap – sig1 sig2 # resets the action taken when the signal is received to the default

disown <PID|JID> # removes the process from the list of jobs

wait # waits until all background jobs have finished

# 6. Tips and Tricks.

# set an alias
cd; nano .bash_profile
> alias gentlenode=’ssh admin@gentlenode.com -p 3404′ # add your alias in .bash_profile

# to quickly go to a specific directory
cd; nano .bashrc
> shopt -s cdable_vars
> export websites=”/Users/mac/Documents/websites”

source .bashrc
cd websites

# 7. Debugging Shell Programs.

bash -n scriptname # don’t run commands; check for syntax errors only
set -o noexec # alternative (set option in script)

bash -v scriptname # echo commands before running them
set -o verbose # alternative (set option in script)

bash -x scriptname # echo commands after command-line processing
set -o xtrace # alternative (set option in script)

trap ‘echo $varname’ EXIT # useful when you want to print out the values of variables at the point that your script exits

function errtrap {
es=$?
echo “ERROR line $1: Command exited with status $es.”
}

trap ‘errtrap $LINENO’ ERR # is run whenever a command in the surrounding script or function exists with non-zero status

function dbgtrap {
echo “badvar is $badvar”
}

trap dbgtrap DEBUG # causes the trap code to be executed before every statement in a function or script
# …section of code in which the problem occurs…
trap – DEBUG # turn off the DEBUG trap

function returntrap {
echo “A return occured”
}

trap returntrap RETURN # is executed each time a shell function or a script executed with the . or source commands finishes executing