1 / 85

Advanced UNIX

Advanced UNIX. 240-491 Special Topics in Comp. Eng. 1 Semester 2, 2000-2001. Objectives of these slides: introduce the C Shell concentrate on features not in the Bourne Shell (but many are in Bash). 7. The C Shell (Chapter 11, Sobell). Overview. 1. Background 2. History 3. Alias

avidan
Download Presentation

Advanced UNIX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced UNIX 240-491 Special Topics in Comp. Eng. 1Semester 2, 2000-2001 • Objectives of these slides: • introduce the C Shell • concentrate on features not in the Bourne Shell (but many are in Bash) 7. The C Shell(Chapter 11, Sobell)

  2. Overview 1. Background 2. History 3. Alias 4. Job Control 5. Home Directory Short Forms continued

  3. 6. Filename Completion 7. Directory Stacks 8. Variables 9. Automated Scripts 10. Script Programming

  4. 1. Background • Originally part of Berkeley UNIX • More tolerant of mistakes than Bourne • e.g can avoid accidental logouts, overwrites • Easier to configure • e.g. alias, history, job control • Some scripting improvements over Bourne: • better variables, arrays, expressions continued

  5. Almost all the good ideas from the C Shell have been added to Bash: • e.g. history, aliases, job control, directory stacks • I use Bash for scripting and as my command interpreter.

  6. Entering & leaving the C Shell • Check your default shell with ps • Change shell using chsh or type csh • Exit with: exit, logout(control-D maybe) csh is actually tcsh on most Linuxes

  7. 2. History • A list of recently used command lines (events) that can be reused. • $ set history = 25 (record 25 events)$ set savehist = 20 (remember 20 events after logout)$ history (see history list)

  8. Using the history list • $ !! (execute last event) • $ !<number> (execute event no. <number>) $ !3 • $ !<initial-text> (execute most recent command that starts with initial-text) $ !gcc $ !loc

  9. Bash History • history [ number ] • lists the last number commands! • Bash supports the same history list features: • !!, !<number>, !<text> • Relevant environment variables: • HISTFILE=/home/ad/.bash_history • HISTFILESIZE=500 • HISTSIZE=500 continued

  10. In Bash, you can modify the prompt to include the history number. • Inside .bashrc: PS1="\!.\u@\h$ " \! includes the history number

  11. 3. Alias • Define new commands by using string substitution. • Format: $ alias new-command executed-command(s) • e.g. $ alias ll ls -lg$ ll

  12. Examples redefining commands is okay • $ alias ls ls -lg • $ alias mi mv -i • $ alias who ’who ; date’

  13. Argument Substitution • \!^the first argument of the new command • \!*all the arguments of the new command • $ alias wid ’who | fgrep \!^’ • $ wid igoris the same as:$ who | fgrep igor

  14. Check your aliases • $ alias (list the current aliases) • $ alias hd (check hd alias) • $ unalias hd (cancel hd alias) • $ \ls (use original meaning of ls) sysAdmin safety style

  15. Bash Alias • Bash alias works the same way at the command line as in csh: alias, unalias, \command • Defining an alias in .bashrc is different: alias name=value • alias ls='/bin/ls -F' • alias ll='ls -l' • alias h="history 30" continued

  16. Aliases that require multiple commands or arguments are defined as functions: sgrep(){ ps aux | grep $1 | grep -v grep}cs(){ cd $1 ls}

  17. 4. Job Control • Move commands between foreground and background; suspend commands. • Each background job has a PID and a job number.

  18. Example • $ spell glossary > glossary.out &[1] 26025$ date &[2] 26028Fri Jun 6 16:56:11 GMT+7 2000[2] Done date$ gcc big.c &[2] 26041 finished already continued

  19. one before last the last one • $ jobs[1] - Running spell glossary.out > glo[2] + Running gcc big.c • Other status messages: • Stopped • Stopped (tty input) • Done

  20. Background to Foreground • $ fg %job-number • Example: $ fg %2

  21. Foreground to Background • $ control-Z (suspends job and puts it into the background) • $ bg (resumes the job) • $ bg %job-number (resumes job-number)

  22. Stopping • $ stop %job-number • $ kill %job-number (kills job-number) • A job stopped for tty input must be brought to the foreground (with fg).

  23. State Change • The C Shell prints a message at the next prompt when the state of a job changes. • Use notify to make the shell report a change immediately: $ notify %job-number

  24. 5. Home Directory Short Forms • ~ (your home directory) • ~name (home directory of name) e.g. ~ad • Use: $ cp idea.txt ~ $ ls ~ad/teach

  25. 6. Filename Completion • $ set filec (‘switch on’ file completion) • $ cat trig1A <press esc>$ cat trig1A.txt

  26. Control-D for help sometimes <tab> • $ ls h*help.hist help.text help.trig.01 • $ cat h <press esc>$cat help. <beep><press control-D>help.hist help.text help.trig.01$ cat help.

  27. adv-unix ~ 7. Directory Stacks • Store a list of directories you are using on a stack. • Switch between directories by referring to the stack. cops_104

  28. adv-unix ~ push the directory onto the stack and do a cd • $ pwd/home/ad/$ pushd ~/teach/adv-unix~/teach/adv-unix ~$ pwd/home/ad/teach/adv-unix$ pushd ~/cops_104~/cops_104 ~/teach/adv-unix ~$ pwd/home/ad/cops_104 cops_104

  29. adv-unix cops_104 ~ Change directories quickly push the top directory down and do a cd • $ pushd~/teach/adv-unix ~/cops_104 ~$ pwd/home/ad/teach/adv-unix/

  30. adv-unix ~ push the top directory down and do a cd • $ pushd~/cops_104 ~/teach/adv-unix ~$ pwd/home/ad/cops_104 cops_104

  31. adv-unix ~ Popd pop the top directory and cd to the new top • $ popd~/teach/adv-unix ~$ pwd/home/ad/teach/adv-unix/

  32. Other Stack Commands • $ dirs (lists the directories on the stack) • $ pushd +number • move the directory in position number to the top (0), and cd to that directory • the stack is numbered 0, 1, 2, ... continued

  33. $ popd +number • pop off the directory at position number • if number is not 0 then there is no change to the present directory

  34. 8. Variables 8.1. String Variables 8.2. Numerical Variables 8.3. Special Forms of User Variables 8.4. Shell Variables

  35. 8.1. String Variables • $ set name = fred (include spaces in older csh's)$ echo $namefred$ set (list set variables)$ unset name (delete name var)

  36. setenv (environment vars) no = • $ setenv name fred$ echo $namefred$ setenv$ unsetenv name • setenv makes the variable visible to any scripts called from the shell (or from the script containing the setenv).

  37. Arrays of String Vars • $ set colours = (red green blue orange)$ echo $coloursred green blue orange$ echo $colours[3]blue$ echo $colours[2-4]green blue orange continued

  38. $ set shapes = (’’ ’’ ’’ ’’ ’’)$ echo $shapes$ set shapes[4] = square$ echo $shapes[4]square

  39. Braces • Use {...} to distinguish a variable from surrounding text. • $ set prefix = Alex$ echo $prefix is short for{$prefix}ander.Alex is short for Alexander. • Can also write $prefix{ander}

  40. 8.2. Numeric Variables • Use the @ command for variables holding numbers. • $ @ count = 0 (or set count = 0)$ echo $count0$ @ count = ( 5 + 2 )$ echo $count7$ @ result = ( $count < 5 )$ echo $result0 Remember the space after the @ continued

  41. $ @ count = $count + 5$ echo $count12 • Could write: $ @ count += 5 • $ @ count++$ echo $count13

  42. Operators • Most of the C numerical operators: • Arithmetic: + - * / % • Relational: > < >= <= != == • Logical: && || ! • etc.

  43. Arrays of Numeric Vars • $ set ages = (0 0 0 0 0)$ @ ages[2] = 15$ @ ages[3] = ( $ages[2] + 4 )$ echo $ages[3]19$ echo $ages0 15 19 0 0

  44. Bourne Shell Equivalents • The Bourne Shell only has string variables. • It uses expr to switch string variables into numbers: $ number=5 (Bourne)$ number=‘expr $number + 2‘ $ @ number = 5 (C Shell)$ @ number = $number + 2

  45. 8.3. Special Forms of User Variables • $#VAR (returns number of elements in VAR array) • $?VAR (returns 1 if VAR is declared; 0 otherwise)

  46. $ set days = (mon tues wed thurs fri)$ echo $#days5$ echo $?days1$ unset days$ echo $?days0 days is declared days is not declared

  47. 8.4. Shell Variables • Shell variables can be initialised in three ways: • by the shell • by the environment • by the user with setor setenv continued

  48. There are two types of shell variable: • shell variables that take values; • shell variables that act as switches • they have the value 0 or 1

  49. Shell Vars that take Values • $argv[0] or $0 command name • $argv[1] or $1 first argument of command • Also $2,$3,... • no upper limit; no need for shift • $argv[*] or $* array of all arguments • $#argv number of arguments continued

  50. $HOME pathname to user’s home directory • $PATH array of pathnames to commands $ setenv PATH (/usr/bin /usr/ucb /bin ~/bin) • $status exit status of last command • etc...

More Related