Unix PPT

134
UNIX TRAINING PROGRAM

Transcript of Unix PPT

UNIX TRAINING PROGRAM

What is Unix? A multi-user networked operating systemOperating System Handles files, running other programs, input/output Looks like DOSbut more powerful The internet was designed on it, thus networking is an intrinsic part of the system

Multi-user Every user has different settings and permissions Multiple users can be logged in simultaneously

Logging in and home directories

Connecting: - Connect to the Unix boxes via telnet or ssh (ssh preferred) - After connection, you have a shell (command prompt) Shells: - Instead of clicking, you type long, cryptic commands - After login, you begin in your home directory - tab completion is nice

The Command Prompt Commands are the way to do things in Unix A command consists of a command name and options called

flags Commands are typed at the command prompt In Unix, everything (including commands) is case-sensitive

[prompt]$ localhost:~$ ls l -a unix-tutorialCommand Prompt Command (Optional) arguments (Optional) flags

Note: Many Unix commands will print a message only if something went wrong. Be careful with rm and mv.

Getting help with man man (short for manual) documents commandsman retrieves detailed information about man k searches the man page

summaries (faster, and will probably give better results) man K searches the full text of the man pagesfiji:~$ man k password passwd (5) - password file xlock (1) - Locks the local X display until a password is entered fiji:~$ passwd

Directories In Unix, files are grouped together in other files called

directories, which are analogous to folders in Windows Directory paths are separated by a forward slash: /Example: /homes/trainees/troo1

The hierarchical structure of directories (the directory

tree) begins at a special directory called the root, or /Absolute paths start at / Example: /homes/ Relative paths start in the current directory Example: troo1 (if youre currently in trainees)

Your home directory ~ is where your personal files are located, and where you start when you log in.

Directories (contd) Handy directories to know~ Your home directory .. The parent directory . The current directory

lsLiSts the contents of a specified files or directories (or the current directory if no files are specified) Syntax: ls [ ] Example: ls backups/

pwdPrint Working Directory

Directories (contd further) cd Change Directory (or your home directory if unspecified) Syntax: cd Examples: cd backups/unix-tutorial cd ../class-notes

mkdir MaKe DIRectory Syntax: mkdir Example: mkdir backups class-notes rmdir ReMove DIRectory, which must be empty first Syntax: rmdir Example: rmdir backups class-notes

Files Unlike Windows, in Unix file types (e.g. executable

files, data files, text files) are not determined by file extension (e.g. foo.exe, foo.dat, foo.txt) Thus, the file-manipulation commands are few and simple Many use only 2 letters rmReMoves a file, without a possibility of undelete! Syntax: rm Example: rm tutorial.txt backups/old.txt

Files (contd) cpCoPies a file, preserving the original Syntax: cp Example: cp tutorial.txt tutorial.txt.bak

mvMoVes or renames a file, destroying the original Syntax: mv Examples: mv tutorial.txt tutorial.txt.bak mv tutorial.txt tutorial-slides.ppt backups/

Note: Both of these commands will over-write existing files without warning you!

Shell Shortcuts Tab completionType part of a file/directory name, hit , and the shell will finish as much of the name as it can Works if youre running tcsh or bash

Command historyDont re-type previous commands use the up-arrow to access them

WildcardsSpecial character(s) which can be expanded to match other file/directory names * Zero or more characters ? Zero or one character Examples: ls *.txt rm may-?-notes.txt

Commands - Basic The basics: ls lists your files cp copies a file mv moves (can be used to rename) a file rm remove a file mkdir makes a directory (synonymous to folder) rmdir removes a directory cd changes your directory ps list the current processes in the shells kill kill processes (-9 if you want to kill them violently)

Editing Text Which text editor is the best is a holy war. Pick one

and get comfortable with it. Three text editors you should be aware of: pico Easy! Comes with pine (Dantes email program) emacs/xemacs A heavily-featured editor commonly used in programming vim/vi A lighter editor, also used in programming

Unix File Hierarchy

/ root. The source of everything /bin Where executables (programs) are run /sbin executables generally of interest only to the super user /home where your home directories are /tmp same as c:\temp in windows. Just for temporary files /var system logs and other stuff (probably not intersting) /usr layed out like /, but with less system-critical stuff /dev has files that represent different devices on the system /proc has runtime system info (try: cat /proc/cpuinfo)

Commands Less Basicless basic: ln creating links (kind of like shortcuts, but they work right) less, more keeps your text from scrolling off your screen man, info gives you help on commands grep search in files for a string find search for a filename finger get some information on other users (like last login) which locate where a command is pwd print the current directory diff find the difference between two (text) files

Wildcards and Redirection Wildcards: ? matches a single character * matches any number of characters [xy] matches either x or y (note: you can use other letters too!) Redirection: > pipes standard output (printf, stdout, cout) to a file < pipes a file into standard input (scanf, stdin, cin) | connects the output of one command to the input of another

Ownership Files have two ownersEvery file has exactly one user owner Every file has exactly one group owner

Everyone is a userUsers are in at least one group

Processes have owners, too (known as an id)Every process has exactly one user id Every process has at least on group id

Who am I? Commands that tell you who you are:whoami id groups displays your username displays your username and

Commands that tell you who others are:finger [] displays info for id [] displays info for

Commands that change who you are:su login switch user to login as a different user

Types of Files Regular Files Directories DevicesCharacter Devices Block Devices

Sockets and Named Pipes Links ( Symbolic And Hard)

Files and Directories Everything is referenced via a file DirectoriesList of files & inodes . Reference to the current directory .. Reference to parent directory Root (/) . and .. are the same

Link Files Multiple names for same file Hard LinkPointer to Inode Cant cross partitions File removed when all links deleted

Symbolic (Soft) LinksPointer to file path name Dangling symlink Real file which no longer exists

ln [-s]

File AttributesInode Permissions Ownership Time StampsChange Modification Access

File Size Link Count Pointers to data

Viewing File Attributesls l: Long listing (includes attributes) File Type Permissions Link Count Ownership File Size/Device # Modification Date File Name

stat: Lists all attributes

File Type Attribute# ls -ld /home /etc/passwd /dev/console crw--w--w-rw-r--r-drwxr-xr-x 1 root 1 root 3 root root root root 5, 1 Sep 27 11:27 /dev/console 559 Sep 22 13:14 /etc/passwd 0 Sep 26 10:42 /home

File Type d l b c p s

Meaning Regular File Directory Symbolic Link Block Device Character Device Named Pipe Domain Socket

Ownership# ls -ld /home /etc/passwd /dev/console crw--w--w1 root root 5, 1 Sep 27 11:27 /dev/console -rw-r--r-1 root root 559 Sep 22 13:14 /etc/passwd root 0 Sep 26 10:42 /home drwxr-xr-x 3 root

UserOwner of file User names/UIDs defined in /etc/passwd

GroupOrganization of users accessing the file Group names/GIDs defined in /etc/group

Permissions# ls -ld /home /etc/passwd /dev/console crw--w--w1 root root 5, 1 Sep 27 11:27 /dev/console -rw-r--r-1 root root 559 Sep 22 13:14 /etc/passwd 3 root root 0 Sep 26 10:42 /home drwxr-xr-x

Operation File Read Read file Write Execute

Directory List files

Delete/Modify file Create/Delete file Run program Access file

3 levels of access Owner, Group, Other

Changing Access Users & Groupschown [-R] user file chgrp [-R] group file

Permissionschmod [-R] file

Numeric/Symbolic PermissionsOctal 0 1 2 3 4 5 6 7 Binary 000 001 010 011 100 101 110 111 Symbolic ----x -w-wx r-r-x rwrwx

Default Permissions umask Shell Environment Variable Defines permissions to removeNumeric 0 1 2 3 4 5 6 7 Binary 000 001 010 011 100 101 110 111 Effective Perms rwx rwr-w r--wx -w--x ---

Searching the Filesystemfind: Command line search tool Searches through directory hierarchy Search by any combination of file names and attributes Display files or perform operations on them Examples:find /var mtime -1 find / -name core exec rm f {} \;

File Permissions Every file has three access levels:user group other read write execute (the user owner of the file) (the group owner of the file) (everyone else) (looking at the contents) (altering the contents) (executing the contents)

At each level, there are three access types:

Examining Permissions A long ls listing shows file permissions:[zanfur@odin zanfur]$ id uid=8774(zanfur) gid=100(users) groups=100(users),101(mp3) [zanfur@odin zanfur]$ ls -l total 524 -rwxr-xr-x 1 zanfur users 512668 Jul 31 00:18 bash prw-r--r-1 zanfur users 0 Jul 31 00:24 fifo -rw-r--r-1 zanfur users 0 Jul 31 00:18 file drwxr-xr-x 2 zanfur users 4096 Jul 31 00:16 normal drwxrws--T 2 zanfur mp3 4096 Jul 31 00:14 shared drwxrwxrwt 2 zanfur users 4096 Jul 31 00:14 tmp drwxrwxr-x 2 zanfur www 4096 Jul 31 00:15 www [zanfur@odin zanfur]$ _

Permissions Listing Breakdown Field Descriptio Valid Valuesn?--------?--------?--------?--------?--------?--------?--------?file type user read user write user execute group read group write group execute other read - (normal file), d (directory), p (fifo), b, c, s - (no read permissions), r (read permissions) - (no write permissions), w (write permissions) - (no exec), x (exec), S (setuid), s (both) - (no read permissions), r (read permissions) - (no write permissions), w (write permissions) - (no exec), x (exec), S (setgid), s (both) - (no read permissions), r (read permissions)

What You Can Do With PermissionsPermissio nr (read) w (write)

FileRead a file Write a file

DirectoryList files in Create a file in Rename a file in Delete a file in Read a file in Write to a file in Execute a file/shell script in

x (execute)

Execute a file (eg shell script)

Changing Ownership Changing user ownership:The command is change owner: chown but, you can only do it if you are root so just copy it instead

Changing group ownership:The command is change group: chgrp but, you can only do it if you are in group and you must be the user owner of the file

Changing Permissions The change mode command:chmod [,] string of: u, g, o, a (user, group, other, all) one of +, -, = (gets, loses, equals) string of: r, w, x, s, t, u, g, o (read, write, execute, set-id, text, same as user, same as group, same as other),

Examples:chmod u+rwx,go-w foobar chmod g=u,+t temp/ chmod u=rwx,g=rwxs,o= shared/

Login Scripts (what happens at startup) .login, .bash_login, .mylogin, .profile, .bash_profile

One of these scripts get run at startup. If you use bash, you should create a .bash_profile so that you know what is getting run.

.logout, .bash_logout One of these gets run when you logout or quit a shell .cshrc, .mycshrc, .bashrc One of these get run you start a new shell (as opposed to logging in. An example would be when you start a new xterm). .cshrc and .mycshrc are run if you use tcsh and .bashrc is run if you use bash. .xsession This gets run if you login via xdmcp. This is the only thing that gets run. It must be set as executable (chmod u+x) to work.

What is input/output redirection? Normally, a programs standard output is displayed

on users terminal, and its standard input comes from the keyboard. Redirecting the output of a program means asking the shell to put the programs output (stdout [C++s cout]) into a file. Redirecting the input of a program means asking the shell to feed a file as the programs standard input (stdin [C++s cin]). Note: redirection works with files.

Why redirect programs output? You may want to save output and examine it at

your leisure:if the program generates a lot of output if the program takes a long time to run

You may want to present the output to another

person Having the program write to standard output may make the program simpler to write

Standard output vs Standard error By convention, normal output of a program is

sent to standard output (stdout [C++s cout]), while debugging or error output is sent to standard error (stderr [C++s cerr]).

How to redirect programs To redirect just the standard output: output?

> To redirect just the standard error: sh/ksh/bash: 2> csh/tcsh:( > STDOUT ) >& STDERR

To redirect both standard output and standard error:

csh/tcsh/bash: >& sh/ksh/bash: > 2>&1

> vs. >> Both > and >> will create the output file, if it

doesnt already exist If the file does exist, then:Using > to redirect output will overwrite the output file: ls > newlisting printenv > my_environment

Using >> to redirect output will append to the output file cat ch1 ch2 ch3 > book cat ch4 ch5 ch6 >> book

Why redirect programs input? To run the program repeatedly with the same (or

similar input) Having the program read from standard input may make the program simpler to write.

How to redirect programs input? Simple!

< Example sort < my_grades.txt head < really_long_book.txt

Piping Piping is connecting programs together by using the

output of one program as the input to the next. Syntax: | | |

A simple example (view a sorted file-listing a page at a

time): ls | sort | less Note: piping deals with the input/output of programs (that is, stdin, stdout, stderr)

Why piping? Because the whole is bigger than the sum of its

parts. By combining Unix utilities in a pipeline, you can build tools on-the-fly as you need them.

Piping examples How many .c files are in this directory?

ls *.c | wc l What files were modified most recently? ls t | head What processes am I running? ps auxw | grep Make an alias for the above, for other Linux boxen too:

alias myps= ps auxw | grep `id un`

The cat utility Especially useful: catWhen used with pipes, copies stdin to stdout cat can be used to redirect out of a pipe! Example: make a file containing currently running processes ps aux | grep evgenyr | cat > my_processes

When used with a file, copies the file into stdout cat can be used to place a files contents into a pipe! Example: list all the items in a list in alphabetical order cat my_grocery_list.txt | sort | uniq

Command substitution(aka whats up with the backquotes?) Command substitution means that the shell

substitutes a commands output for the command itself. To get the shell to perform command substitution, enclose the command in backquotes (`)

Next stop: utilities (No, not electricity, water, and sewer) diff and patch grep find and xargs

diff and patch You have two versions of a file; how do you see whats

changed between the two versions?diff shows the differences between two files; youll want to use the u or c option to diff to produce output in human-friendly format:diff u my_file my_file.orig | less

patch applies differences to a filediff u my_file my_file.orig > my_patch patch my_file < mypatch my_file is now the same as my_file.orig

grep search for patterns grep searches for patterns in texts:

grep grep uses regular expressions to describe the string(s) to search for In a regular expression, most characters are treated as-is, but a few are magicOrdinary characters just represent themselves Magic characters do special things

greps magic characters A few different kinds of magic characters:Some characters anchor (parts of) a regular expressions to specific places in the string: ^ - The beginning of a line $ - The end of a line A dot (.) matches any one character whasoever (except for newline) [ ] form a character class: [aeiou] any single vowel [a-zA-z0-9] Any single letter or digit Here, ^ means not [^0-9] Any single character that isnt a digit

Even more of greps magic characters Quantifiers say how many times the

preceeding thing should be repeated:* means zero or more times ? Means zero or one time

Frequently used grep options-i : do case-insensitive search -n : print line numbers -v : print lines that do not match -l : only list the files from which output would have been printed

grep examples Print all non-blank lines: grep v ^$ my_file.txt Print all the lines on which my_function is

called (or declared):grep n my_function *( my_code.c

Show all the xterms I am running: ps auxw | grep `id un` .*xterm

findfind Traverse the tree(s) rooted at , and for each

thing found, evaluate the until the result of is known. The evaluation of short-circuits, just like expressions in C/C++.false && some_expression can never be true

Options apply to the entire find command, not the

individual expression

Components of a find expression An expression is zero or more primaries connected byTests: just return true or false Actions: do something, in addition to returning true or false Operators work just as they do in C/C++. ! / -not -a / -and -o / -or , (comma)

operators Two kinds of primaries:

Parentheses are used for grouping, just as in C/C++.

find examples Print all the *.c* and *.h* files in and below the current directory find . name *.[ch]* a print Show line numbers myfunction calls in the above *.c* files find . name *.c* | xargs grep n myfunction.*( Change permissions on files under your home directory so theyre

inaccessible to everyone but you (except for files in the www directory)cd; find . path ./www* prune o exec chmod go-rwx {} \;

How big are my *.c* files? expr `find name *.c* printf

%k + ` 0

Read the find manual (info find) for more examples

xargs: combine argumentsxargs Feeds standard input as arguments to Often used with find

(find | xargs grep) But it doesnt have to be used with find:cat filelist | xargs cat {} > concatenated

Process Management What can you do with it?Start programs in the background Run more than one program per terminal Kill bad and/or crashing programs Suspend programs mid-execution List all jobs running in a shell Move foreground jobs to the background More

Three States of a Process ForegroundAttached to keyboard Outputs to the screen Shell waits until the process ends

Background, runningNot attached to keyboard Might output to the screen Shell immediately gives you another prompt

Background, suspendedPaused mid-execution Can be resumed in background or foreground

Background Processes Listing jobs:jobs ps % lists background jobs and job #s lists processes and their process id expands to the process id of the job

Stopping foreground jobsPress ^Z (Ctrl-Z) in the terminal window

Starting a process in the background Append a & character to the command lineExamples: ls lR > ls-lR.out & xemacs my_program.cc & fg [] bg []

Resuming a stopped jobIn the foreground: In the background:

Killing Processes The kill command:kill [-] Send to process

The killall command:killall [-] Send to all processes that start with

Useful signals (kill l for the complete list):TERM nicely KILL HUP STOP the default, terminate, kills things will kill anything, but not nicely hangup, used to reload configurations stops (suspends) a running process

Programs & Processes Program file which can be runBinary executables Shell scripts

Process A running programLoaded in physical memory Stored in virtual memory (swap)

Virtual Memory Extends amount of physical memory UNIXSize of swap = amount of usable memory Physical memory (RAM) mirrored in swap

Linux: Memory = swap + RAM SwappingMoving pages to and from memory Page block (unit) of RAM

Swap AllocationUNIX:RAM: Swap:

Linux:RAM: Swap:

Which works better when?

Process ComponentsAddress space map Status

Running Runnable

Sleeping Stopped

Zombie

Owner Blocked Signals Priority Resources used

Process Ownership Real User (User starting the process)UID: User ID Number GID: Group ID Number

Effective User (Permissions for process)EUID: Effective User ID Number EGID: Effective Group ID Number

Process Lifecycle Parent process forks a child Process identifiersPID: Process ID Number PPID: Parent Process ID Number

Child finishesSends status message to parent Zombie waits for parent acknowledgment Orphan init (PID 1) becomes parent

Running a command Type in the command name Shell searched for the command If found, shell forks the command How is the command found?PATH environment variable which Displays full path to command Beware of . in your PATH

Background Jobs Foreground processReturns control to shell after it finished Most common usage

Background processReturns control to shell immediately & typed after command name Runs concurrently

Daemons Special programs which performs a some

task without intervention Started in the backgroud

(often at system startup)

Name of most ends in dDaemon is actually a much older form of demon ; daemons have no particular bias towards good or evil, but rather serve to help define a person's character or personality. ~Evi Nemeth

fg, bg, & jobs Can switch between modes Send signal to stop a process Commandsfg Run process in foreground bg Run process in background jobs List shell child processes

Signals Used to communicate with processes Terminal signals - Terminate process - Stop (suspend) process

kill [-signal] pidSends signal to process PID Default is to terminate the process

Common Signals# 1 2 3 9 15 Name HUP INT QUIT KILL TERM STOP Description Hangup Interrupt Quit KILL Software Termination Stop

Process Priority How much CPU time granted relative to other

processes nice Sets the priority value at start renice Changes priority RangesSystem Solaris RedHat Range 0 to 39 -20 to 20

System Load Average number of Runnable processes Measure of how busy the system is Performance deteriorates at loads of 6+ Good for creating a baseline uptime Load average

(5, 10, 15 minutes)

Monitoring Processes psBSD Sorts by %CPU Usage SVR4 Sorts by PID

topFull terminal screen display Sortable Can kill and renice processes

gtop Gnome top

Information from ps PID USER PRI/NI - Priority/Nice level RSS/SIZE - Resident/Total Memory used STAT - Process State %CPU/%MEM - % of System CPU/Memory TIME - CPU time used Command

/proc Filesystem Pseudo filesystem Interface to memory-related kernel data

structures Each PID has its own directorycwd Symlink to current working directory exe Symlink to program cmdline command line options environ command line environment stat process status

Runaway Processes Can eat up your resources Lists users and PIDs accessing a resourcefuser lsof

Kill the process and clean up

Processes 1: suspend, background, foreground When you run a command, it starts a new process Each process has a unique number called the PID (Proccess ID) Unix is a multitasking operating system, so you can run multiple processes simultaneously After you start a process, it is usually in what is called the foreground. That is, it takes over your shell. You can suspend processes in the foreground with Ctrl-Z The process is now frozen. You can pull it to the foreground again by typing fg. Alternately, you can make it keep running, but put it in the background so can still use the same shell by typing bg. You can also start a task in the background by putting a & at the end of the command.

Processes 2: ps, kill, kill -9 You can list all the processes that have been run from the current shell with ps To list all the processes on the system, do ps awuxroot awong awong 3723 0.0 0.1 3092 988 pts/20 3724 0.0 0.1 1406 712 pts/17 3725 0.0 0.1 2334 716 pts/17 S 16:21 0:00 bash R 16.35 0:00 -bash R 16.36 0:00 ps awux

If you want to end a process, you can do kill eg. kill 3724 If that doesnt kill it, you can do the super kill, kill -9 eg. kill 9 3724 If a process of yours freezes, you can login to the same machine again, do a ps awux and find the process number and then kill it.

System State: uptime, top, nice Sometime you want to know what process is taking up the most cycles on a machine. The program that tells you this is top Another measurement utility for system load is uptime. uptime tells you how long the system has been running and how many processes are concurrently screaming for attention. The last three numbers tell you how the average number of processes screaming for attention over the last 1 minute, 5 minutes and 10 minutes, respecetively.4:49pm up 1:15, 19 users, load average: 0.10, 0.11, 0.09

If you are going to run a long, cpu intensive process, it is often a good idea to nice it. This puts it at a slighly lower priority so that quick, interactive tasks (like checking e-mail) wont get starved. To do nice a process, just do nice

What are environment variables? The environment is an array of strings of the form

NAME=VALUE Each process (program) has its own copy of the environment Processes started by the shell get a (deep) copy of the shells current environment Each process can examine and modify its own (and only its own) environment The meaning of environment variables is merely a matter of established conventions

Viewing your shells environment To view a single environment variables value:echo $

For example:echo $HOME

will display/homes/iws/evgenyr

To view all environment variables at once:tcsh/csh/bash: printenv sh/ksh: set

Setting environment variables tcsh/csh:setenv Example: setenv PRINTER ps329

bash/ksh:

!

export = There is no space on either side of =! Example: export PRINTER=ps329

sh:=; export

Appending to environment variables Appending /uns/bin/ to your PATHbash/ksh: export PATH=$PATH:/uns/bin tcsh/csh: setenv PATH=$PATH:/uns/bin

Prepending is similar:bash/ksh: export INFOPATH=/uns/info:$INFOPATH tcsh/csh: setenv INFOPATH /uns/info:$INFOPATH

Common environment variables The meaning of environment variables is

purely a matter of convention. However, these environment variables are quite common:PATH, INFOPATH, MANPATH, EDITOR, VISUAL, PAGER, HOME, MAIL, USER

The man page for a program will usually list the

environment variables the program pays attention to.

Environment VariablesWhat are environment variables? Think of them as parameters that get passed to your programs. All operating systems have them. They give information about the context within which a program runs. The most commonly used environment variable is the PATH variable which tells the shell where to look for programs. To create an environment variable in bash, type: export VARNAME=value To create an environment variable in tcsh type: setenv VARNAME value Dont forget the export or the setenv, otherwise you create a Shell variable instead of an environment variable.

Useful shell features (a preview of upcoming attractions) Aliases Redirecting input and output Command substitution Scripts

Shell as a work-saver: aliases substitution, similar to Aliases: textualbash/ksh: alias = tcsh/csh alias

C-preprocessor macros. Syntax:

When you type , the shell substitutes

Typing alias by itself lists all your current aliases unalias removes the alias Check the csh/tcsh man page for extra features

Alias examples Always print postscript files double-sidedtcsh/csh: alias lpr lpr Zduplex bash/ksh: alias lpr=lpr Zduplex

Shell as a work-saver: scripts Instead of typing the same series of commands over

and over again, put them in a file and have the shell execute the (commands in the) file! The file must have execute permission The first line of the file should be: #! (e.g., /bin/bash) There must be no space (or any other character) between # and !. Whether to put space after ! is a matter of style

Why Learn Scripting? /bin/sh always there Understand/Modify Existing System ScriptsRC Startup Scripts Daemon Startup Scripts

Automate repetitive tasks Great for small tools

Writing/Debugging Scripts Know how to do job manually first Write in small (logical) parts Test each part before continuingPrint modified variables Print command lines to be executed Never work on production data

Comment code!!!

Starting Scripts Passed as input to a shellExample: /bin/bash mysript

Run directly from the command lineFirst line of script (i.e: #!/bin/bash) Script must have execute bits set $PATH considerations

Commenting code Anything following # is considered a

comment and ignored Script HeaderTells what the script does Common Entries:Author Date written Program name Command line usage Purpose Files used Notes/features Revision History

Argument VariablesVariable $0 $1 - $9 $# $* $@ Purpose Name of the program Command line arguments Number of Arguments All arguments as 1 word Each argument is a word

Other Special VariablesVariable $$ $! $? Purpose PID number of script PID number of parent process Exit status of script (Checked by parent)

Basic Inputread variable [ < filename ] Examples:read ANS read LINE < file(only reads first line)

Basic outputecho [ -n ] [ -e ] string -n : No new line -e : Allow backslash options Example:echo n Enter your name:

Backslash OptionsCharacter \a \b \c \n \r \t \v \\ \### Alert (bell) Backspace Dont display trailing newline Newline Carriage return Horizontal Tab Vertical Tab Backslash ###: ascii value of character Purpose

Expression Evaluation expr commandMath expressions ( +, -, *, /, % ) String expressions (match, substring, etc.) Logic expressions ( &, |, =, etc.)

Example:COUNT=`expr $COUNT + 1`

Flow Control Need to determine which commands to run

based on one or more conditions Useful to catch errors Provides flexibility in code Classic if-then-else model

If-Then conditionBasic decision construct if condition then commands fi

String TestsExpression -z string -n string string1 = string2 True if if Length of string is 0 Length of string is not 0 If the 2 strings are identical

string1 != string2 If the 2 strings are different string If string is not NULL

Numeric TestsExpression int1 eq int2 int1 ne int2 int1 gt int2 int1 ge int2 int1 lt int2 int1 le int2 True if if int1 equals int2 int1 and int2 not equal int1 > int2 int1 >= int2 int1 < int2 int1 0 bytes

Logic OperatorsExpression ! -a -o Purpose Negates the expression AND OR

( expression ) Groups the expression

If-Then-Else conditionif condition then commands else commands fi

If-Then-Else If conditionif condition then commands elif condition then commands fi

Case conditionPerform a range of tests on a variable case variable in pattern1) commands ;; pattern2) commands ;; esac *) matches all other cases

Loops Need to run block of code more than once ModesRepeat a set number of times Repeat while a condition is true Repeat until a condition is true

for loopRepeat for each variable in list for variable in variable_list do commands done

while loopRepeat until condition is false while [ condition ] do commands done

Reading FilesRead a file one line at a time while read LINE do echo $LINE done < $FILENAME

do-until loopRepeat until condition is true until [ condition ] do commands done

Escaping out of loops Sometimes you need to exit loop earlyError conditions Special conditions

break command Example:while [ 1 ] do echo n Done? read ANS if [ $ANS = y ] then break fi done

Other scripting languages PerlVery popular among sysadmins Many modules available

TCL/TK Expect Python

Quoting examples List all the files in the current directory:ls *

List the file or directory called * (or complain if its

not there):ls \*

Print $HOME:echo $HOME

Print the path to your home directory:echo $HOME

Print `id un`:echo `id un`

Print your login:echo `id un`

Debugging The ideal:

do it right the first time The reality:

bugs happen The goal:

exterminate, quickly and efficiently

Debugging Methods The best way: read the codeIf you dont understand it, itll happen again

Consistent use of error checking & handlingmost uncaught errors are not where they appear to be

Examine the state at key points in the codewrap debug output with #ifdef DEBUG use a debugger, like gdb, to view the state

Efficiency in Debugging The goal is to isolate the problem and fix it Dont try random things, looking for a solutionif you dont understand ititll be back half the time, this takes an insanely long time you dont learn anything from it

Look for the problem, not the solutionfigure out two points in code that the problem is between, and close the gap from there once you know the line the bug is on, its easy

Breakpoints What are they?A break point: a point in the code where execution pauses, or breaks, then continues

What are they for?To examine the state of the running program

How do you make them?modifying code to print information and pause use a debugger to interactively step through code

Debuggers Advantages over the old fashioned way:you can step through code as it runs you dont have to recompile to set a breakpoint you can examine the entire state of the program call stack, variable values, scope, etc.

you can modify values in the running program you can view the state of a crash using core files

GDB, the GNU DeBugger Text-based, invoked with:gdb [ [|]]

Argument descriptions: program executable program file core dump of program process id of already running

Example:

gdb ./hello Compile with g for debug info

Basic GDB Commands General Commands:file [] run [] attach kill quit help [] selects as the program to debug runs selected program with arguments attach gdb to a running process kills the process being debugged quits the gdb program accesses the internal help documentation

Stepping and Continuing:c[ontinue] s[tep] n[ext] finish continue execution (after a stop) step one line, entering called functions step one line, without entering functions finish the function and print the return value

GDB Breakpoints Useful breakpoint commands:b[reak] []

[r]watch catch

info break[points] clear [] d[elete] []

sets breakpoints. can be a number of things, including a hex address, a function name, a line number, or a relative line offset sets a watchpoint, which will break when is written to [or read] breaks on , which can be used to catch many events, including the throwing and catching of C++ exceptions prints out a listing of all breakpoints clears a breakpoint at deletes breakpoints by number

Playing with Data in GDB Commands for looking around:list [] prints out source code at search searches source code for backtrace []prints a backtrace levels deep info [] prints out info on (like local variables or function args) p[rint] [] prints out the evaluation of

Commands for altering data and control path:set sets variables or arguments return []returns from current function jump jumps execution to

More and More GDB For the most complete and up-to-date

documentation on GDB, use the GDB help subsystem from within gdb. A close second is the GDB info node (info gdb). There is also an emacs interface to GDB, which

allows you to keep the source code open in a separate frame while debugging (documentation for how to use this is available in the GDB info node)

Finding more info on the web http://www.faqs.org (comp.unix questions FAQ) http://www.google.com http://www.deja.com The support web page:

http://www.cs.washington.edu/lab/ about/ugradcomputing.html http://www.cs.washington.edu/people/acm/tutorial s/