Shells - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Shells

Description:

The Korn shell provides a random number generator ... The Korn shell's function feature is an expanded version of that found in the Bourne shell ... – PowerPoint PPT presentation

Number of Views:126
Avg rating:3.0/5.0
Slides: 43
Provided by: Kenneth4
Category:
Tags: korn | shells

less

Transcript and Presenter's Notes

Title: Shells


1
Shells
  • Five major shells
  • Bourne (sh)
  • C shell (csh)
  • Korn (ksh)
  • Bourne Again (bash)
  • T shell (tcsh)

2
Korn Shell
  • Invented by David Korn of ATT Bell Labs in mid
    1980s
  • Upwardly compatible with the Bourne shell
  • Became a standard part of Unix Systems Labs (USL)
    SVR4
  • Two major versions exist, ksh88 and ksh93
  • ksh88 is on clyde
  • ksh93 is also available as /usr/dt/bin/dtksh

3
Major Features
  • .profile
  • Command line editing
  • History
  • Environment file
  • Random numbers
  • Print
  • Whence
  • Built-in arithmetic
  • Aliases

4
  • Job control
  • Additional built-in variables
  • Additional pattern matching
  • Additional options for test
  • Typeset
  • Read
  • Parameter Expansion

5
.profile
  • Kept in your home (login) directory
  • Used for customizing the user interface of ksh
  • Used to store
  • Aliases
  • Shell variables
  • Options
  • Location of environment file
  • Only executed if ksh is a login shell

6
Options
  • Options alter the the shell's behavior
  • Over 20 options available - common ones are
  • bgnice
  • emacs
  • ignoreeof
  • markdirs
  • monitor
  • noclobber
  • noglob
  • trackall
  • vi
  • Options are set using "set -o optionname" and
    unset using "set o optionname"

7
Command Line Editing
  • Enabled in two different ways
  • Using the shell variable VISUAL
  • VISUAL(whence vi)
  • Using set option
  • set -o vi
  • Two different editors are available
  • vi and emacs
  • Note ksh does not use these editors, it has
    commands that emulate the functionality built in
    to the shell

8
Editing Commands in vi-mode
  • CTRL-W - Erase previous word
  • CTRL-V - "Quote" the next character
  • ESC - Enter control mode (edit mode)
  • Edit mode uses same movement commands as vi
  • h, l, w,b 0,,, etc.
  • Also uses same editing commands
  • i, a, r, R, x, d(movement command)

9
File Name Completion in vi-mode
  • File name completion is also supported
  • Type the command an the beginning of the file
    name
  • Touch ESC and the \
  • ksh will attempt to complete the file name for
    you
  • If no name matches, shell beeps and nothing
    happens
  • If there is exactly one way to complete the name,
    the shell does it, if it's a directory, it adds a
    /
  • If there are more than one match, the shell
    completes the longest common prefix
  • Using instead of \ lists all matches

10
History
  • This feature alone may make it worth moving to
    Korn shell (when combined with command line
    editing)
  • History is stored in a file (normally
    .sh_history) in your home directory
  • Location can be changed by defining HISTFILE
  • Note if you want history kept for each subshell,
    you can define HISTFILE.sh_history
  • Unfortunately, this leaves a bunch of history
    files laying around your directory that need
    cleaning
  • Size is set by HISTSIZE - default is 128
  • Please set to 50 in your .profile to conserve
    disk space

11
History Example
  • Given the command find / -name ribblestaff
    -print
  • And assuming it was command 36
  • C shell
  • !36 (re-execute command 36 in history)
  • !f ( re-execute the most recent command
    beginning with f)
  • !fp (to print but not re-execute, makes it last
    in buffer)
  • rira (to change "ribble" to "rabble")

12
Same Thing Using Korn
  • Korn shell
  • r 36 (to re-execute command 36 in history)
  • r f (to re-execute the most recent command
    beginning with f)
  • fc f (to fix command that started with an
    f)or fc cmd_no.
  • This will put you into the editor of your choice
    (defined by FCEDIT - default is ed!)
  • When you leave the editor, the command will be
    executed
  • fc l (lists history commands)
  • ksh automatically aliases history to fc -l

13
Moving Around the History File
  • Again, the vi commands work here
  • k - move backwards one line (command)
  • j - move forward one command
  • nG - move to command n
  • ?string - search backwards for string
  • /string - search forwards for string
  • n - repeat search in same direction
  • N - repeat search in opposite direction
  • The first two can be preceded by repeat counts
  • 3k - move backwards 3 commands

14
Environment File
  • Although predefined environmental variables will
    always be known to subshells, the shell must be
    explicitly told which other variables, aliases,
    and options should be communicated to subshells
  • One way to do this is to put them in an
    environment file
  • Do this by placing
  • export ENV/environmentfile in your .profile and
    then putting your data in environmentfile

15
Random Numbers
  • The Korn shell provides a random number generator
  • To initialize it, you type RANDOM (or a
    specific number if you want a repeatable
    sequence)
  • Each time you echo the RANDOM variable, you will
    get anew random number in the range 0 -32767

16
print
  • In other shells, you usually use awk when you
    want formatted printing
  • ksh provides print. It doesn't allow as much
    control over printing as awk but it does provide
    more control than echo
  • print has a variety of formatting options and
    parameters

17
print Formatting Options
  • \a - announce, ring the bell
  • \b - backspace
  • \c - print line without newline (rest of args
    ignored)
  • \f - formfeed
  • \n - newline
  • \r - carriage return
  • \t - tab
  • \v - vertical tab
  • \Ox - the 8 bit ASCII character with octal value
    x
  • \\ - backslash

18
print Parameters
  • - process anything that follows as an
    argument if it begins with a -
  • -R do not use the \ conventions, also anything
    except -n that follows is processed as an
    argument
  • -n do not add trailing newlines to output
  • -p send arguments via pipe to a co-process
  • -r don't use the \ conventions
  • -s direct the arguments to the history file
  • -un redirect arguments to the file descriptor
    n (default is 1 or stdout)

19
whence
  • Tells you where a command resides
  • Two options, -p and -v, control the format of the
    output
  • whence alone prints the path to the command
  • whence vi
  • /bin/ucb/vi
  • -v also lists the type of command
  • whence -v vi
  • vi is a tracked alias for /usr/ucb/vi
  • -p does a path search even if the name is a
    reserved word or alias

20
Built-In Arithmetic
  • In the Bourne shell, if you wanted to do math you
    had to use eval and command substitution
  • ksh lets you do arithmetic using either the let
    command or (( ))
  • (( )) good for True/False comparisons (( ))
    provides value of computation
  • let a 68
  • print a
  • 14
  • Note that there are no spaces in the expression.
    If you want to use spaces, you must single quote
    the expression

21
  • The (( )) is useful in as a conditional because
    it returns a 1 or 0
  • if test ((minmaxtop))
  • then
  • print "Ooh, a big one!"
  • else
  • print "Guess not"
  • fi
  • ksh can also work with bases other than 10
  • bn is the same as n to the base b
  • 817 is an octal 17 (15 decimal)

22
Aliases
  • ksh has aliases very similar to the C shell
  • It adds the concept of tracked aliases
  • These translate faster than normal aliases
  • When used, the shell searches the path the first
    time and then remembers where the command was
    found
  • Future uses of the alias go directly to the
    command
  • You can also unalias in the Korn shell

23
Job Control
  • Korn shell supports job control almost the same
    as C shell
  • CTRL-Z places a job in the background, as does
    command
  • fg brings a job back to the foreground
  • Referring to background jobs is also similar
  • n job n
  • string job whose command begins with string
  • ?string job whose command contains string
  • most recently invoked background
    job
  • same as
  • - second-most recently invoked bg job

24
Additional Built-In Variables
  • PS1, PS2, PS3, PS4 - prompt strings
  • LOGNAME - your login name
  • CDPATH - provides a path to use when performing
    cd
  • Normally, cd dirname looks for a directory in
    your current working directory
  • CDPATH provides a list of places to look for
    dirname
  • EDITOR - pathname of your preferred editor,
    various programs refer to this and it sets the
    mode for command line editing

25
  • VISUAL - similar to EDITOR, used if EDITOR not
    set
  • FCEDIT - editor to use with fc to edit commands
    in the history file
  • OLDPWD - previous directory before last cd
  • SECONDS - number of seconds since the current
    shell was invoked

26
Additional Pattern Matching
  • Korn shell supports additional pattern matching
    metacharacters
  • ?(patternpattern) matches zero or one
    occurrence of any of the patterns
  • phre?(drsxx) would match phred, phrer, phres,
    and phrexx, ?(x) would match null and 'x'
  • (patternpattern) matches zero or more
    occurrences of any pattern
  • phre(0-9) would match 'phre' or 'phre'
    followed by any number of digits

27
  • (patternpattern) matches one or more
    occurrences of any pattern
  • phre(0-9) would match 'phre' followed by one
    or more digits, (x) would match 'x', 'xx', 'xxx'
  • _at_(patternpattern) matches any occurrence of
    any pattern
  • Most useful when used as a logical OR.
    phre_at_(ddydsak) would match phreddy or phreds or
    phreak. _at_(xyz) matches 'x' or 'y' or 'z'

28
  • !(patternpattern) matches zero or more
    occurrences of any pattern
  • Not really a regular expression but it is handy.
    It matches any that is not a pattern.
  • phre!(d0-5) would match anything that started
    with 'phre' but did not end with a 'd' or the
    digits 0-5. !(x) would match any ASCCI character
    except 'x'

29
Additional Options for test
  • The Korn shell gives additional options for the
    test command
  • -a the file exists
  • -G file has same group as test process
  • -L file is symbolic (soft) link
  • -O file has same owner as test process
  • -o option true if shell option is on (set)
  • -S file is a socket
  • -z string is zero length
  • f1 -ef f2 the two files are linked (same inode)
  • f1 -nt f2 true if f1 is newer than f2
  • f1 -ot f2 true if f1 is older than f2

30
Typeset
  • In the Korn shell, you can assign types to
    variables
  • typeset -i count
  • makes count an integer, if you tried to assign a
    string to it, the shell would give you an error
    message
  • You can typeset with the following attributes
  • -u make uppercase (forces string to uppercase)
  • -l make lowercase
  • -in require integers and store internally as
    integer n specifies the base
  • -r make variable readonly
  • -x export the variable

31
  • -Ln left justify to remove leading spaces fill
    with spaces or truncate on right to n bytes
  • -Rn right justify to remove trailing spaces fill
    with spaces or truncate on left to n bytes
  • -Zn same as -R except pad with leading 0's
    instead of spaces
  • typeset o is used to turn the option off

32
Read
  • read functions much like it did in the Bourne
    shell
  • However, it can be smart and send the prompt to
    stderr without special processing
  • read variable?'prompt'
  • Note the location of the ? and the tick marks.
    These are critical!
  • This will send prompt to stderr if the input
    device is the terminal but if stdin has been
    redirected, no prompt will be generated.

33
Parameter Expansion
  • var - uses the value of var
  • var-value - if var is set (non-null) return
    it, otherwise return value
  • varvalue - if var has been set, return it,
    otherwise set var to value and return it
  • var?message - if var has been set, return it,
    otherwise print var and message (default is
    "parameter null or not set")
  • varvalue - if var has been set, return
    value, otherwise return null

34
Substrings
  • variablepattern - if pattern matches the
    beginning of variable's value, delete the
    shortest part that matches and return the rest
  • variablepattern - if pattern matches the
    beginning of variable's value, delete the longest
    part that matches and return the rest
  • variablepattern - if pattern matches the end
    of variable's value, delete the shortest part
    that matches and return the rest
  • variablepattern - if pattern matches the end
    of variable's value, delete the longest part that
    matches and return the rest

35
Command Substitution
  • In the Korn shell, command substitution is done
    with a different syntax
  • The Bourne shell syntax is supported but
    discouraged
  • (command arg1 arg2 ) is replaced with the
    result of the command
  • An example might be in your .profile file when
    you set up your VISUAL or EDITOR variable
  • EDITOR(whence vi)
  • This would evaluate to
  • EDITOR/bin/ucb/vi

36
Functions
  • The Korn shell's function feature is an expanded
    version of that found in the Bourne shell
  • A function is similar to a script except it is
    kept in the shell's memory. Because it is kept
    in memory, it runs faster than loading a script
  • Functions allow you to create your own groups of
    commands, which can make shell programming much
    easier since they can be used analogously to
    subroutines
  • Functions are not inherited by subshells unless
    you define them in the environment file with the
    typeset command typeset -fx functname

37
Syntax
  • function functname
  • shell command
  • shell command
  • You can delete a function using unset -f
    functname
  • You can find out what functions are defined using
    the command (actually an alias) "functions"
  • The shell will print the names and definitions of
    all functions

38
  • There are two important differences between
    functions and scripts
  • A functions does not run as a separate process
  • It shares variables with the process that invoked
    it
  • The present working directory is that of the
    caller
  • If the function changes it, it will also be
    changed for the caller
  • If a function has the same name as a script or
    executable program, the function takes precedence
  • The return statement returns the exit status of
    the last command executed
  • Functions can be recursive but you need to be
    careful

39
Autoloaded Functions
  • The simplest place to define functions is in your
    .profile or environment file
  • However, if you have a lot of definitions, the
    delay in reading these files each time you log in
    or invoking a subshell can become unacceptably
    slow
  • Autoload lets you tell the shell to only read the
    function definition when it is invoked the first
    time
  • "autoload functname" creates an undefined
    function, functname, that won't be loaded until
    the first time it is actually called

40
Finding Autoload Definitions
  • The FPATH variable tells the shell where to
    search for autoload functions
  • For example, if your .profile (or environment
    file) contains the following
  • FPATH/funcs
  • autoload my_function
  • When you invoke the function my_function the
    first time, the shell will search the directories
    in FPATH for a file called my_function which has
    a definition of function my_function and load it
    into the shell's memory space

41
Example Functions
  • function increment
  • typeset sum sum is a local variable
  • ((sum 1 1))
  • return sum
  • print -n "The sum is "
  • increment 5
  • print ? ? holds the return value of the
    last command executed
  • print sum sum was local to function and
    no longer is defined

"typeset var" with no options makes var a
local variable in a function
42
  • function square
  • ((sq11))
  • print "Number to be squared is 1."
  • print "The result is sq."
  • square 10
  • Number to be squared is 10.
  • The result is 100.
Write a Comment
User Comments (0)
About PowerShow.com