| Users Online |
There are: 0 registered users and 1 guests online now. | |
|
| Redhat Corner |
| |
The Korn
Shell Tutorial |
| |
| Korn Shell was created by David Korn |
|
This article was created to be used as a reference page for
writing scripts using Korn scripting or for the beginner just
starting to write Korn scripts.
Invoking the Korn shell type:
/bin/ksh
Bold text is the command input, red text is the output and orange
text are links. |
|
Section I - [ Process Execution, Input/Output Redirection,
File Name Substitution, & Command Substitution]
![]() |
![]() |
![]() |
| |
examples using string operators
from startup scripts for httpd, sendmail
& network |
|
# if variable $DEVICE is zero then run DEVICE=$i
if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi
# If we're in confirmation mode, get user confirmation
[ -n "$CONFIRM" ] &&
{
confirm $i
case $? in
0)
:
;;
2)
CONFIRM=
;;
*)
continue
;;
esac
}
action $"Bringing up interface $i: " ./ifup $i boot
fi
done
|
![]() |
![]() |
![]() |
![]() |
| |
command execution format |
|
command; command2 - runs command1 followed by command2
command & - execute command in background
command1|command2 - pass standard output of command1 to
standard input of command2
command1 && command2 - execute command2 if command1
returns zero (successful)
command1 || command2 - execute command2 if command1 returns
non-zero (unsuccessful)
command |& - execute command asynchronously with its
standard input and standard output
command \ - continue command on next line
{ command; } - execute command in current shell - remember
spaces!
(command) - execute command in its own shell - no spaces
required |
![]() |
![]() |
![]() |
![]() |
| |
command execution examples |
files in directory are "one
two three" |
|
1) ls; echo "files"
2) ls &
3) ls | wc -l
4) ls && echo true
5) ls || echo false
6) echo \abc
7) { date; }
8) (date) |
1) one two three
files
2) [1] 6034
one three two
3) 14
4) one three two
true
5) one three two
6) abc
7) Sat Jul 13 20:11:07 PDT 2002
8) Sat Jul 13 20:11:27 PDT 2002
|
![]() |
![]() |
![]() |
![]() |
| |
Quotes |
' .. ' " .. "
` .. ` |
|
' .. '
` .. `
" .. "
|
hide meanings of all special characters except another
'
back quotes are used to assign variables from output
double quotes hide all meanings except special characters
like $, ', and \ |
![]() |
![]() |
![]() |
![]() |
| |
Examples using Quotes |
|
|
echo 'date'
echo 'the 'date' today is'
echo "the 'weather' is rain"
echo "my prompt is $PS1"
REDHAT='uname`
echo $REDHAT
|
date
the 'date' today is
the 'weather' is rain
my prompt is [\u@\h \W]\$
Linux |
![]() |
![]() |
![]() |
![]() |
| |
Some I/O Redirection
Operators |
|
<file
>file
>>file
>|file
<>file
<&-
>&-
|
redirect standard input from file
redirect standard output to file
append out to file
redirect standard output to file
open file for reading and writing as standard input
close standard input
close standard output |
![]() |
![]() |
![]() |
![]() |
| |
Examples using Redirectors
|
|
|
1) # cat <hello.world -
hello world
2) # hello.world> - hello.world
(created)
3) # cat hello.world >> hello.world -
hello.world hello.world
4) # echo "hello world" >&- -
(nothing will be displayed)
5) # cat hello.world | wc -l <&- -
(input will not be displayed)
|
|
![]() |
![]() |
![]() |
![]() |
| |
Redirect Operators /
File Descriptors |
| |
<&n
>&n
n<file
n>file
n>>file
n>|file
n<&m
n>&m
n<>file
n<<word
n<<-word
|
|
redirect std.inp from file desc. n
redirect std.out to file desc. n
redirect file desc. n from file
redirect file desc. n to file
redirect file desc. n to file
redirect file desc. n to file
redirect file desc. n input from file desc.
redirect file desc. n output to file desc.
open file for reading and writing as file desc. n
redirect to file desc. n until word is read
redirect to file desc. n until word is read |
|
|
![]() |
![]() |
![]() |
![]() |
| |
Basic Pattern-Matching Characters |
| |
?
*
[xyz]
[a-c]
[a-ce-g]
[!abc]
[!a-c]
. |
|
match any single character
match zero or more characters
match any character or characters between brackets
match any character or characters in the range a to c
match any character or characters in the range a to c,
to g
match any characters or characters not between brackets
match any character or characters not in the range of
a-c
strings starting with . must be explicitly matched
|
|
|
![]() |
![]() |
![]() |
![]() |
| |
Examples using Pattern Matching |
|
using the file "a" |
| |
ls ?a
ls a*
ls [abc]
ls [a-c]
ls [a-ce-g]
ls [!z]
ls [!s-z]
ls [.a]
|
|
a
a
a
a
a
a
a
a |
|
|
![]() |
![]() |
![]() |
![]() |
| |
Advanced Pattern Matching |
|
|
| |
?(pattern)
*(pattern)
+(pattern)
@(pattern)
!(pattern)
pattern |
|
match zero or one occurrences of any pattern
match zero or more occurrences of any pattern
match on or more occurrences of any pattern
match exactly one occurrence of any pattern
match anything except any pattern
multiple patterns must be separate
|
|
|
![]() |
![]() |
![]() |
![]() |
| |
Examples using Adv. Pattern
Matching |
|
using files a aa aaa |
| |
ls ?(a)
ls *(a)
ls +(a)
ls @(a)
ls !(z)
ls (a|j|[1-3])
ls !(z|[1-3]|z)
|
|
a
a aa aaa
a aa aaa
a
a aa aaa
a aa aaa
a aa aaa |
|
|
![]() |
Section II - [ Variable Attributes, Special Parameters,
Variable Expansion, Array Variables ]
![]() |
![]() |
![]() |
| |
Variable Attributes with using typeset
|
| |
typeset -i var - Set var to be a integer
typset -l var - Set var to lower case
typeset -L var - Left justify var
typeset -Ln var - Left justify var, set field width
to n
typeset -r var - Set var to read-only
typeset -R var - Right justify var
typeset -Rn var - Right justify var, set field width
to n
typeset -RZn var - Right justify var, set field width
t on and fill as leading 0s
typeset -t var - Set the user-defined attribute forvar.
typeset -u var - Set var to upper case
typeset -x var - Automatically export var to environment
|
|
![]() |
![]() |
![]() |
![]() |
| |
Special Parameters |
| |
? - exit status of last command
$ - process id of current shell
- - current options in effect
! - process id of last background process
ERRNO - error number returned by most recently failed
sys call
PPID - process id of the parent shell
~ - home directory of current user |
|
![]() |
![]() |
![]() |
![]() |
| |
Examples of Special Parameters |
| |
1) echo $?
2) echo $$
3) echo $-
4) echo $PPID |
|
1) 0
2) 5459
3) ims
4) 5336
|
|
|
![]() |
![]() |
![]() |
![]() |
| |
Variable Expansion Formats |
| |
1) ${#variable} - length of variable
2) ${variable:-word} - value of variable if set and not
null, else print word
3) ${variable:=word} - value of variable if set and not
null, else set
4) ${variable:+word} - value of word is variable is set
and not null, else nothing
5) ${variable:?} - value of variable if set and not null
else print 'not set'
6) ${variable:?word} - value of variable if set and not
null, else print value of word
7) ${variable#pattern} - value of variable without the
smallest beginning portion
that matches pattern
8) ${variable##pattern} - value of variable without the
largest beginning portion
that matches pattern
9) ${variable%pattern} - value of variable without the
smallest ending portion
that matches pattern
10) ${variable%%pattern} - value of variable without the
largest ending portion
that matches pattern
11) ${variable||pattern1|pattern2} - replace all occurrences
of pattern1 with pattern2 variable |
|
![]() |
![]() |
![]() |
![]() |
| |
Examples of Variable Expansion
|
|
using variable PS1=# USERNAME=root |
| |
1) echo ${#PS1}
2) echo ${PS5:-NEW}
3) echo ${PS5:=NEW}
4) echo ${PS6:?}
5) echo ${PS5:?PS1}
6) echo ${USERNAME#r}
7) echo ${USERNAME##t}
8) echo ${USERNAME%t} |
|
1) 2
2) NEW
3) variable for PS5 is now NEW
4) /bin/ksh: PS6: parameter null or not set
5) /bin/ksh: PS7: PS1
6) oot
7) root
8) roo
|
|
|
![]() |
![]() |
![]() |
![]() |
| |
Array Variables |
|
|
| |
${array}
${array[n]}
${array[n+1]}
${array[$z]}
${array[*]}
${#array[*]}
${#array[n]}
${!array[*]}
${!array[*]:n:x}
${!array[@]:n} |
|
$array element zero
array element n
array element n + 2
array element $z
all elements of array
number of array elements
length of array element n
all initialized subscript values
x array elements starting with element n
all array elements starting with element n
|
|
|
![]() |
![]() |
![]() |
![]() |
| |
Assigning Variables to Arrays
|
|
|
| |
set -A DAY Mon Tues Wed
typeset DAY[0]=Mon DAY[1]=Tues DAY[3]=Wed
|
|
will assign Mon, Tues, and Wed to DAY array
will also assign Mon, Tues, and Wed to DAY array |
|
|
![]() |
![]() |
![]() |
![]() |
| |
Examples of arrays |
|
using the above sample |
| |
echo $DAY
echo ${DAY[1]}
echo ${DAY[*]}
echo ${DAY[0+1]}
echo ${#DAY[1]}
echo ${#DAY[*]}
|
|
Mon
Tues
Mon Tues Wed
Tues
4
3
|
|
|
![]() |
Section III - [ Manipulating Jobs, Checking Job Status, Background
Jobs and I/O, Job Names, Leaving Stopped Jobs ]
![]() |
![]() |
![]() |
| |
Job Commands |
|
|
| |
Ctlr-z
fg
fg %n
bg
bg %n
jobs
jobs -l
sleep 100
wait
kill -l
kill -signal %n
kill %n
stty tostop
stty -tostop
|
|
suspend current job
bring background job into the foreground
bring background job n into the foreground
send job into the background
send job n into the background
display current jobs and status
display current jobs and process ids
put into background for 100 seconds
wait for the job to finish
list all valid signal names
send specified signal to job n
kill job n
prevent background jobs from generating output
allow background jobs to generate output
|
|
|
![]() |
![]() |
![]() |
![]() |
| |
Job names |
|
|
| |
%n
%+, %%
%-
%string
%?string |
|
job n
current job
previous job
job whose name begins with string
job that matches part or all of string
|
|
|
![]() |
Section IV - [ Working with Arithmetic Operators ]
![]() |
![]() |
![]() |
| |
Arithmetic Operators |
anything between (( .. )) ( ...
) grouping (used to override precedence rules) |
|
-
!
~
*, /, %
+, -
<<,>>
<=,<
>=,>
==
!=
&
^
|
&&
||
=
*=, /=, %=
<<=, >>=
&=, ^=, |=
( ... )
|
unary minus
logical negation
bitwise negation
multiply, division, remainder
addition, subtraction
left shift, right shift
less than or equal to, less than
greater than or equal to, greater than
equal to
not equal to
bitwise AND
bitwise exclusive OR
bitwise OR
logical AND
logical OR
assignment
multiply assign, divide assign
left shift assign, right shift assign
bitwise AND assign, bitwise exclusive OR assign, bitwise
OR assign
grouping (used to override precedence rules) |
![]() |
Section V - [ case, select, function, while, until,
if, do, read, continue, echo, print ]
![]() |
![]() |
![]() |
| |
function |
a function will be passive until
called from a scripts |
|
functionname()
{
commands
}
or
function name()
{
commands
}
|
example from httpd startup
example from sendmail startup
if [ "1" != "2" ] ;
then
functionone
fi
functionone() {
echo this is function one
}
functionone
|
![]() |
![]() |
![]() |
![]() |
| |
elif function |
execute a set of commands that
are true |
|
if commands
then
commands
elif command
then
command
fi
|
if [ -d /deva ]; then
echo deva is a directory
elif [ -d /dev ]; then
echo dev is a directory not deva
fi |
![]() |
![]() |
![]() |
![]() |
| |
while function linux-mag |
looping syntax |
|
while command1
do command
done
|
while (($# != 0 ))
do
echo test
done |
![]() |
![]() |
![]() |
![]() |
| |
until function linux-mag |
similar to while except executes
when condition is false |
|
until command1
do command
done |
until (($# == 0))
do
echo true
done |
![]() |
![]() |
![]() |
![]() |
| |
echo function |
displays its arguments on standard
output |
|
-n
-e
-E |
don't output trailing line
enable interpretation of the backslash-escaped characters
listed below
disable interpretation of those sequences in STRINGs |
![]() |
![]() |
![]() |
![]() |
| |
exec function |
used to perform I/O redirection
with file descriptors 0-9 |
|
exec 1>std.out
exit |
all standard output is no going to file std.out
will allow you to exit the shell and view the output of
std.out |
![]() |
![]() |
![]() |
![]() |
| |
read function |
input from read is assigned to
variable |
|
read age |
when age is entered its assigned
to $age |
![]() |
Section VI - [ find, awk, grep, sed ]
![]() |
![]() |
![]() |
| |
Commands often used when
writing scripts |
|
| at |
Execute a command at a specified time |
| cat |
Display contents of a file |
| chmod |
Change file permissions |
| comm |
Compare files |
| cp |
Copy files |
| df |
Display disk space usage of filesystems |
| diff |
Compare text files |
| du |
Displays disk space usage of directories |
| echo |
Display text |
| eval |
Evaluate an expression |
| grep |
Find strings |
| kill |
Terminate a process |
| lpr |
Print data |
| less |
Page through a file |
| ls |
List file information |
| mail |
Send a mail message |
| mkdir |
Create a directory |
| mv |
Move a file or directory |
| nice |
Set process priority |
| nohup |
Run a command immune to hang-ups |
| od |
Display file contents in octal and other formats |
| passwd |
Set a user's password |
| pr |
Format data for printing |
| ps |
Display process information |
| rm |
Remove a file |
| rmdir |
Remove a directory |
| sh |
Launch a shell |
| sleep |
Pause a process |
| sort |
Sort data |
| spell |
Check spelling |
| tail |
Display last lines of file |
| time |
Time the execution of a script |
| times |
Displays CPU time used |
| uniq |
Eliminates duplicate data |
| w |
Displays logged in users |
| wait |
Waits for child processes to terminate |
| wc |
Counts characters, words, and lines |
| who |
Displays logged in users |
|
![]() |
Section VII [ default redhat 7.2 startup scripts httpd, sendmail,
and network ]
![]() |
![]() |
![]() |
| |
Links for Korn |
|
|
Korn Shell (ksh) Programming
Korn Shell 93 User Manual |
http://www.bolthole.com/solaris/ksh.html
http://dfrench.tripod.com/kshman93.html |
![]() |
|
|
| |
|
| |
|
|
|
| |
|
|