Chapter 4 The UNIX Shells (Bourne shell, Korn shell, C shell)
The Korn Shell (ksh)
description
Transcript of The Korn Shell (ksh)
![Page 1: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/1.jpg)
The Korn Shell (ksh)
CS465 – Unix
![Page 2: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/2.jpg)
ksh Shell• The Korn (ksh) shell
– Scripting syntax is compatible with the standard Bourne (sh) shell
– Included in Unix operating systems from most vendors (Sun Solaris, SGI IRIX, Linux, etc.)
– Provides some extra features:• Command aliasing• Easier user input and math calculations• Command history• Command-line editing
![Page 3: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/3.jpg)
Command AliasesAliases allow you to define your own commands• Format:
$ alias [-x] name=definition
• Examples:$ alias ll="ls -la"$ alias dir="ls -F"$ alias –x home="cd;ls"$ alias –x rm="rm -i"
Using the –x option “exports” the alias.
![Page 4: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/4.jpg)
Command Aliases
• To remove aliases:$ unalias name
• To show all aliases:$ alias
• If you put the alias commands in your .profile or .kshrc file, you can use them every time you login.
![Page 5: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/5.jpg)
Displaying Alias Values
• To determine which command a specific alias will run, use either alias OR whence:
$ alias ll="ls -la"$ alias llll='ls –la'$ whence llls –la$
![Page 6: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/6.jpg)
The Dash (-) Character• Dash (-) represents the previous working directory. When used to cd, it
automatically displays the current directory path.
• Example: Switching between one of your subdirectories and the system bin directory:
$ pwd/home/user1/sub1$ cd /bin$ pwd/bin$ cd -/home/user1/sub1$ cd -/bin$
![Page 7: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/7.jpg)
The let command• let provides built-in integer handling that
easier to use and 10-30 times faster than expr• Syntax:
$ let math-expressionNOTE: Use = to assign values
• Examples$ let i=i+1$ let "prod = 12 * 6" Double
quotes allow spaces and no backslash on *
![Page 8: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/8.jpg)
The let commandCan use (( instead of let )):
x=3y=4((z = x * y)) Result: z = 12
((z = z + 1)) Result: z = 13
Notes: - No dollar sign ($) needed to access variable values
- Double parentheses act as quotes, so you can add spaces and don’t need to backslash metacharacters
![Page 9: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/9.jpg)
let Operators– Integer operators:
+, -, *, /, %
– Integer comparisons: <, <=, ==, !=, >=, >
– Compounds&& (and), || (or), ! (not)
NOTE: No backslashes needed inside (( ))
![Page 10: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/10.jpg)
Bourne vs Korn Comparison
• Korn Shell
• Bourne Shell
[ $num -ge 0 ]
(( num >= 0 ))
[ $num1 -gt 0 -a $num2 -lt 100 ]
(( num1 > 0 && num2 < 100 ))
result=`expr calculation`
(( result = calculation ))
![Page 11: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/11.jpg)
let example
i=1total=0while (( i <= 100 ))do
(( total = total + i ))(( i = i + 1 ))
doneecho $total
![Page 12: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/12.jpg)
Korn Additional test Operators
• The Korn shell also extends the test expression from the Bourne shell.
• By using two sets of square brackets, instead of one set, the system uses the Korn shell (instead of Bourne) to test specific conditions.
[[ test-condition ]]
![Page 13: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/13.jpg)
Korn Additional test OperatorsFile Operators:-a file file exists-L file file exists and is a symbolic linkf1 -ef f2 file1 and file2 are linkedf1 –nt f2 file1 is newer than file2f1 –ot f2 file1 is older than file2
Logical operators:&& logical AND|| logical OR
![Page 14: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/14.jpg)
Korn test example
$ cat lsdir#! /bin/ksh# lists directory contents if yoursif [[ -d $1 && -O $1 ]]then ls -l $1
else echo Error - not a directory or not yours!
fiexit$
![Page 15: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/15.jpg)
Command History• The Korn shells supports a history feature
that lets you recall previous commands, performing a substitution if necessary.
• The history command displays the previous 16 commands:
$ history
• Common Alias:
$ alias h="history"
![Page 16: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/16.jpg)
r (recall/rerun command)r alone repeats the last command.r 11 repeats command number 11r -2 repeats the command before the last
one.r d repeats the last command that
started with a “d”.r sort one=two repeats previous sort
command using two instead one.
![Page 17: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/17.jpg)
History Variables
• HISTFILE contains name of your history file
HISTFILE=$HOME/.myhist• If you do not provide a name, then the Shell uses:
$HOME/.sh_history
• HISTSIZE contains how many commands to save– Default (to save) is 128 (but show only last 16)
HISTSIZE=50
![Page 18: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/18.jpg)
In-Line Command Editing
• You can perform vi in-line editing of the command line
• In-line edit mode enables you to edit a previous command on the current command line
• Use vi commands to move and edit the previous command line
![Page 19: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/19.jpg)
Turning On/Off Editing
$ set [-+]o vi
set -o vi turns command-line editing on
set +o vi turns it off
Once turned on, ESC key activates the in-line editor
To use:
Press the ESC key to activate in-line editing.
Press – (or k) until the desired command appears.
Edit using vi commands, then press ENTER.
![Page 20: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/20.jpg)
Using Command Line Editing• Your command line now becomes a single line
editor window into the command history file.
• The single line you start viewing is the current Shell command line.
• You can move to other lines by using the editor move commands (- moves backwards, + moves forwards).
• Editor search commands can also be used to select the line being viewed.
![Page 21: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/21.jpg)
<ESC> Enter command modei/a Enter insert mode and add text before/after cursorl Move cursor righth Move cursor leftfc Move to character cx Delete characterdw Delete one word$ Move to end of line<Enter> Execute the command
Some vi for Command Line Editing
![Page 22: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/22.jpg)
Using Filename Completion• Automated completion of a filename used as an
argument to a command.• To initiate, use the <ESC> key and a backslash
• Example:$ lsfile1 file2 fileofstuffnotes subdir whynot$ vi n<ESC>\
Uses vi to edit the file notes
Note: Only works when file completion is unique
![Page 23: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/23.jpg)
• If the filename is NOT unique, you can get a list of choices by using: <ESC>=
• Example:$ cat f<ESC>=1) file12) file23) fileofstuff$ cat f_
Now when you press a number (1-3), that file is chosen and used to complete the command.
Filename Completion Choices
![Page 24: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/24.jpg)
Additional Korn Shell Variables$PPID the shell's parent process' PID$_ last parameter of previous command$RANDOM randomly generated integer (0-Max)$ENV pathname of Korn shell environment
startup file$OLDPWD working directory set before current one$EDITOR pathname of editor to use for line editing$PS3 prompt for select loops (default is #?)$TMOUT number of seconds to wait before exiting
shell if no command is given
![Page 25: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/25.jpg)
OLDPWD example$ pwd/home/smith123/cprogs$ cd /etc$ pwd/etc$ cd $OLDPWD$ pwd/home/smith123/cprogs
![Page 26: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/26.jpg)
New Pattern Matching
~ home directory(equivalent to $HOME)
~username username’s home
~+ current working directory(equivalent to $PWD)
~- previous working directory(equivalent to $OLDPWD)
![Page 27: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/27.jpg)
Using Tilde Substitution$ pwd/home/smith123/progs/cprogs$ cd$ pwd/home/smith123$ cd ~-$ pwd/home/smith123/progs/cprogs$ cd ~jones456$ pwd/home/jones456
![Page 28: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/28.jpg)
Reading User Input• Korn shell provides one command that will
BOTH “echo” and “read”:
• Syntax:
$ read 'varname?prompt'
• Examples:
$ read 'name?Enter your name: '
$ read 'year?Current year? '
![Page 29: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/29.jpg)
read Example
$ mulEnter a number: 5Enter another number: 85 times 8 is 40$
$ cat mulread 'num1?Enter a number: 'read 'num2?Enter another number: '(( prod = num1 * num2 ))echo $num1 times $num2 is $prod$
![Page 30: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/30.jpg)
until statement
until [ condition ]do
command(s)done
• Same condition syntax as if statement
• Begin and end of command block defined by keywords do…done
• Loops UNTIL condition is TRUE
![Page 31: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/31.jpg)
until ExampleRead in a number from a user, and verify the number is
positive.
$ cat posnum#! /bin/sh# Read positive number from usernum=0until [ num -gt 0 ]doecho Enter a positive non-zero number:read num
doneecho You entered $numexit 0$
![Page 32: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/32.jpg)
until ExampleRead in a number from a user, and verify the number is
positive.
$ cat posnum#! /bin/sh# Read positive number from usernum=0until (( num > 0 ))doecho Enter a positive non-zero number:read num
doneecho You entered $numexit 0$
![Page 33: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/33.jpg)
until Example Execution
$ posnumEnter a positive non-zero number:-50Enter a positive non-zero number:0Enter a positive non-zero number:12You entered 12$
![Page 34: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/34.jpg)
select statement
select var in listdo
command(s)done
• Implements a menu from within a loop. Automatically displays a numbered list of choices, and interpret the number that the user enters.
• Begin and end of command block defined by keywords do…done
![Page 35: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/35.jpg)
select statement
• You must still use the case control structure to evaluate the choice chosen, but the structure will loop automatically unless you have chosen to exit.
• If your menu options consist of multiple words, they must be enclosed in double quotes.
![Page 36: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/36.jpg)
select example$ cat junkit#!/bin/ksh# Menu-driven junkit scriptjunk=$HOME/junkdir## If junkdir directory doesn't exist, create itif [[ ! (-d $junk) ]]then 'mkdir' $junk
fi#select choice in "List junk" "Delete junk" "Junk files" "Exit"
do case $choice in "List junk") ls -lgF $junk;;
![Page 37: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/37.jpg)
select example"Delete junk") rm $junk/*;; "Junk files") read 'filelist?Enter files to junk: ' mv $filelist $junk;;"Exit") break;;*) echo Invalid choice; please try again;;
esacdoneexit 0$
NOTE: The break command is used to exit the loop.
![Page 38: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/38.jpg)
select execution
#? 3Enter files to junk: p2.c#?
$ pwd/export/home/jmsmith$ junkdir1) List junk2) Delete junk3) Junk files4) Exit#? 1total 0#?
#? 1total 2-rw------- 1 jwsmith 122 May 7 13:19 p2.c#? #? 2#? 1total 0#? 4$
![Page 39: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/39.jpg)
Other Korn Additions:String Length
• Length of string: ${#varname}– Returns length of string stored in variable
• Example:$ cat namelenname="Pam Smallwood"echo ${#name}$ namelen13$
![Page 40: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/40.jpg)
Other Korn Additions• Forcing Command Execution
– In addition to the back quotes (graves)`command`
– In Korn you can use: $(command)
• Example:$ name=$(whoami)$ echo $namesmall000$
![Page 41: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/41.jpg)
Sample .kshrc file
# Set command aliasesalias rm='rm -i 'alias rename='mv 'alias c clear# Set environment variablesPATH=$PATH:.PS1="$PWD[!] $ " EDITOR=vi# Export global variablesexport PATH EDITOR PS1# Set history variablesHISTSIZE=40
![Page 42: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/42.jpg)
Default .profile
• Review handout
![Page 43: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/43.jpg)
Capturing a Terminal Session
• You can capture what your terminal display to a file using the command script
• Syntax: $ script filename
• Everything that appears on your screen will be captured in the file, until you enter <ctrl-d>
![Page 44: The Korn Shell (ksh)](https://reader033.fdocuments.in/reader033/viewer/2022061506/56815e76550346895dccf938/html5/thumbnails/44.jpg)
Terminal Session Example$ script session1Script started, file is session1$
$ cat session1$ pwd/export/home/small000$ dateThu May 22 19:34:25 MDT 2003script done on Thu May 22 19:34:29 2003$
pwd/export/home/small000$ dateThu May 22 19:34:25 MDT 2003$ <CTRL-d>Script done, file is session1$