ksh

  1. startup
    The first process: init

init -> getty -> login -> ksh

initialization file: .profile
environment file: .kshrc # export ENV=.kshrc

  1. environment
    /etc/profile -> .profile -> .kshrc

  2. Variable

local variable

typeset name=”Tom”

global variable

export ORACLE_SID=ora11g

arguments

echo 1 1 2 3 echo 3echo *
echo $# # the number of arguments

  1. 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

  1. prompt
    export PS1=”(uname -n): (unamen): PWD $”
    export PS2=”>”

  2. type
    1) keywords
    2) aliases
    3) built-ins
    4) functions
    5) scripts and executables

  3. list 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

  1. 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

  1. 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.

  2. 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

  1. time
    time sleep 3
    time ps -ef | wc -l

  2. read
    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.

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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
“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值