150 likes | 294 Views
Agenda. Korn Shell Korn Shell Variables Korn Shell Variable Attributes Korn Shell Arithmetic Functions. Korn Shell. The Korn shell was developped to incorporate Bourne shell as well as incorporate many features of the C shell
E N D
Agenda • Korn Shell • Korn Shell Variables • Korn Shell Variable Attributes • Korn Shell Arithmetic • Functions
Korn Shell • The Korn shell was developped to incorporate Bourne shell as well as incorporate many features of the C shell • The Korn shell also has many features including improved math processing, aliases, variable attributes and history capabilities • The default shell for PHOBOS is the Korn shell.
Z & Bash Shells • The Z shell is Linux’s equivalent to the Korn shell - Z shell is available on TUX • The Bash shell has evolved to contain many of the features of both the Bourne shell and Korn shell
Running Shells • If your system allows you access to the shell, you can switch to a shell by issuing a command. • Eg. • Bash bash shell • zsh Z Shell • sh Bourne Shell • ksh Korn Shell
Setting a Default Shell • In some Linux and UNIX systems, you may be able to set your default shell upon logon by issuing the command: • chsh
Korn Shell Variables • The rules for Korn Shell variables are very similar as for Bourne Shell variables: • $1 - $9 are positional parameters (can use ${var} this allows for greater range • For user-created variables, variable name cannot begin with a digit and use assignment with an equal sign (no spaces on either side) • Variables can be removed using the unset command
Korn Shell Variables • Braces for variables can be handy to combine text together: • example: • SUFF=ization • echo org$SUFF • PREF=counter • echo ${PREF}clockwise Will display organization In order for text substitution to work in front of text, braces must be used. This example will display counterclockwise
Korn Shell Variable Attributes • One major way in which Bourne and Korn shells differ is that Korn shell can assign attributes to variables such as: • upper or lower case • integer • justification and width • The most useful attribute is integer since the variable does not have to be converted from text to an integer during processing
Korn Shell Variable Attributes • The typeset command is used to set an attribute for a variable • examples • typeset -u var (set var to be uppercase) • typeset -l var (set var to be lowercase) • typeset -i var (set var to hold an integer) • typeset -L3 var(set var as left justified with a width of 3) • typeset -R5 var(set var as right justified - width of 5)
Korn Shell Arithmetic • In the Korn shell, user-created variables can have an integer attribute. This makes it more efficient to perform math operations than the Bourne shell. • When performing calculations in the Korn shell using these variables, two expressions are used: • The let statement • The convention $((..))
Korn Shell Arithmetic • Example: • typeset -i x y • x=23 • y=37 • let result=x+y • echo $result • result=$((x+y)) • echo $result In first example, note how variables are set as integers. Also note with let command that no spaces are allowed In second example can use double-braces to perform math operations….
Z & Bash Shell Arithmetic • Syntax for Z shell arithmetic is the same as for Korn shell arithmetic • Bash Shell Arithmetic: • result=$[x+y] • echo $result • Note difference between Bash, Korn shell and Bourne shell syntax!
Command Substitution Syntax • The syntax rules for the Bourne, Korn (Z) and Bash shells are as follows: • Bourne Shell `command` • Korn (Z) Shell $(command) • Bash Shell $(command)
Functions • A function is a series of commands that are stored in computer’s main memory in order to allow quicker execution than executing a script. • Functions can also be contained within a shell script. As opposed to creating a function within the shell itself, a script containing a function will run slower since it is not stored in computer’s main memory
Functions After function entered (include brackets) type body of function at PS2 prompt - make sure to include braces. • Example: • who_is_here() • >{ • > date • > echo “Users Currently Logged On:” • > who -H | more • >} When end brace is entered, shell prompt will appear. To run function type who_is_here