- startup
The first process: init
init -> getty -> login -> ksh
initialization file: .profile
environment file: .kshrc # export ENV=.kshrc
environment
/etc/profile -> .profile -> .kshrcVariable
local variable
typeset name=”Tom”
global variable
export ORACLE_SID=ora11g
arguments
echo 1
1
2 3 echo
3echo
*
echo $# # the number of arguments
- set
set -a # Causes set variables to be automatically exported
set -o allexport
set -e # If a command returns a nonezero exit status, execute the ERR trap, if set, and exits. Not set when reading initialization files
set -o errexit
set -n
set -o noexec
set -f # disables pathname expansions, turn off wildcards
set -o noglob
set -p # Don’t read the .profile and ENV file; used with setuid script
set -o privileged
set -x
set -o xtrace
prompt
export PS1=”(uname -n): (uname−n): PWD $”
export PS2=”>”type
1) keywords
2) aliases
3) built-ins
4) functions
5) scripts and executableslist variables
set: all variables, local and global
env: only global variables
typeset: all variables, integers, functions, and exported variables
set -o: list all built-in variables that are set on or off
- variable expression and expansion modifiers
Expression
Function
{variable:–word} If variable is set and is nonnull, substitute its value; otherwise, substitute word. v {variable:=word}
If variable is not set or is null, set it to word; the value of variable is substituted permanently. Positional parameters may not be assigned in this way.
{variable:+word} If variable is set and is nonnull, substitute word; otherwise substitute nothing. {variable:?word}
If variable is set and is nonnull, substitute its value; otherwise, print word and exit from the shell. If word is omitted, the message parameter null or not set is printed.
fruit=peach
echo {fruit:-plum} # peach echo {newfruit:-plum} # plum
echo {fruit:+plum} # plum echo {newfruit:+plum} # NA
echo {fruit:?”no set”} # peach echo {newfruit:?”no set”} # -ksh: newfruit: no set
Variable expression of substrings
Expression
Function
{variable%pattern} Matches the smallest trailing portion of the value of variable to pattern and removes it. {variable%%pattern}
Matches the largest trailing portion of the value of variable to pattern and removes it.
{variable#pattern} Matches the smallest leading portion of the value of variable to pattern and removes it. {variable##pattern}
Matches the largest leading portion of the value of variable to pattern and removes it.typeset
typeset -u name=”john doe” echo $name # JOHN DOE
typeset -l name
echo $name # john doe
typeset -L4 name
echo $name # john
typeset -R2 name
echo $name # hn
name=”John Doe”
typeset -Z15 name
echo “$name” # John Doe
integer n=25
typeset -Z15 n
echo $n # 000000000000025
typeset -lL1 answer=Yes
echo answer # y
typeset -f # list functions and their definitions
typeset -f # list just function names
time
time sleep 3
time ps -ef | wc -lread
1) read -un: Reads from file descriptor n. the default fd is 0, or standard input.
cat names.txt
Jhon Dan
Tom Smith
exec 3< names.txt
read -u3 name1
echo $name1
Jhon Dan
read -u3 name2
echo $name2
Tom Smith
exec 3<&-
while read -u3 line1 && read -u4 line2
do
print “line1 : line2”
done 3<1 4<2
Options
Meaning
–p
Reads a line of input from a coprocess.
–r
Treats newline character, the \n, as a literal.
–s
Copies a line into the history file.
–un
Reads from file descriptor n; the default is fd 0, or standard input.
On Versions of ksh Newer Than 1988
–A
Stores the fields as an array, index starting at zero.
–d char
Used as an alternate delimiter for terminating input; newline is the default.
–t sec
Puts a limit of seconds on the user’s response time.
- Arithmetic
typeset -i <=> integer
typeset -i# # is the base number for the integer
typeset -F
typeset -E <=> Float
integer num
num=”abc” # wrong
num=1 + 2 # wrong
num=”1 + 2” # correct, 3
num=1+2 # correct, 3
num=2.142 # correct, 2
num=12
typeset -i2 num # 2#1100
typeset -i8 num # 8#14
typeset -i16 num # 16#c
i=5
let i=i+1 # 6
let i=”i + 1” # 7
- Array
1)
array[0]=tom
array[1]=dan
array[2]=bill
echo {array[*]} # tom dan bill echo {#array[*]} # 3
2)
set -A fruit apples pears peaches
echo {fruit[*]} # apples pears peaches echo {#fruit[*]} # 3
- trap
1) Pseudo or Fake Signal
DEBUG Execute trap commands after every script command
ERR Execute trap commands if any command in the script returns a nonzero exit status
0/EXIT1 Execute trap commands if the shell exits
2) Resetting Signals
trap 2
trap INT
3) Ignoring Signals
trap ” 1 2
trap ” HUP INT
4) Listing traps
trap ‘print “Ctrl-C will not terminate $PROGRAM.”’ INT
trap ‘print “Ctrl-\ will not terminate $PROGRAM.”’ QUIT
trap ‘print “Ctrl-Z will not terminate $PROGRAM.”’ TSTP
- Coprocesses
A coprocess is a special two-way pipeline that allows shell scripts to write to the standard input of another command and to read from its standard output.
bc |& # open coprocess
print -p scale=3 # write to the coprocess
read num1 op num2 # read from standard in
1 + 2
print -p “num1" "op” “num2” # write to the coprocess read -p result # read from the coprocess echo result
3
- DEBUG
export PS4=’$LINENO ’ # print line number
ksh -x script # Display each line of the script after variable substitution and before execution
ksh -v script # Display each line of the script before execution, just as you typed
ksh -n script # Interprets but does not execute
set -x
set -o xtrace
trap ‘print LINENO ’ DEBUG trap ‘print BAD input’ ERR trap ‘print Exiting from 0’ EXIT
typeset -ft # Trace execution in a function
17 control statement
if (( numeric expression ))
if [[ string expression ]]
while (( numeric expression ))
while [[ string expression ]]
until (( numeric expression ))
until [[ string expression ]]
for name in Tom Dick Harry
PS3=”Select an name from the menu:”
select name in Tom Dick Harry
“`