KSH - Korn Shell Tutorial


Old and unsupported site New Personal site: http://www.abspace.it/AlbertoBellina/ New Factory site: http://www.abspace.it/
Alberto Bellina http://b62.tripod.com/ alberto_b62@hotmail.com


The Web


Report Abuse

« Previous | Top 100 | Next »
share: del.icio.us | digg | reddit | furl | facebook

Take Our Survey!
Ads by Google

Web Scripting Program
Apply to The Art Institute Online - Earn a Web Design Degree.

VMM 2008 Free Trial
Download Microsoft Virtual Machine Manager & Centralize IT Management

KSH - Korn Shell Tutorial
Matching Patterns Conditional Statements Test Objects (Files, Directories, etc.) Format of flow control functions Positional Parameter Redirections Other functionalities Coprocess Examples Regular Expression Array

Matching Patterns
pattern: example: matches: not matched: -----------------------------------------------------------------* boo* boot,boo,booth ? [...] [!...] *(cc|cc) +(cc|cc) ?(cc|cc) @(cc|cc) !(cc|cc) {c,c,c} boo? [aeiou]* boo[!st] boo*(ze|r) boo+(ze|r) boo?(ze|r) boo@(ze|r) boo!(ze|r) a{b,c,d}e boot ark boor boo,boor,booze,boozer boor,booze,boozer boo,boor,booze booze,booth booth,boo,boot abe,ace,ade booth bark boot boot boo boozer boo booze,boor axe

Conditional Statements
format "true" if: --------------------------------------------------(( _num1_ == _num2_ )) numbers equal (( _num1_ != _num2_ )) numbers not equal (( _num1_ < _num2_ )) num1 < num2

1 of 5

1/30/2009 8:35 PM

KSH - Korn Shell Tutorial


(( _num1_ > _num2_ )) (( _num1_ <= _num2_ )) (( _num1_ >= _num2_ )) [[ [[ [[ [[ [[ [[ [[ [[ _str1_ == _str2_ ]] _str1_ != _str2_ ]] _str1_ < _str2_ ]] _str1_ > _str2_ ]] _str1_ = _pattern_ ]] _str1_ != _pattern_ ]] -z _str_ ]] -n _str_ ]]

num1 > num2 num1 <= num2 num1 >= num2 strings equal strings not equal str1 precedes str2 str1 follow str2 str1 = pattern str1 != pattern str is null str is not null or in expression and in expression

[ x=y -o k=j ] [ x=y -a k=j ]

Test Objects (Files, Directories, etc.)
test "true" if: ksh ----------------------------------object exist -a readable -r writable -w executable -x non-zero length -s zero length directory plain file symbolic link named pipe block special file character special file soft link socket owned by me owned by my group "sticky" bit set set-group-ID bit set set-user-id bit set opened on a terminal -d -f -h -p -b -c -L -S -O not -k -g -u not

Format of flow control functions
"if-then" if _expr_ then _cmd(s)_ elif _expr_ _cmd(s)_ else _cmd(s)_ fi case _word_ in _pattern1_) _pattern2_) *) esac while _expr_ do _cmd(s)_ done _cmd(s)_ _cmd(s)_ break ;;



2 of 5

1/30/2009 8:35 PM

KSH - Korn Shell Tutorial



for _variable_ in _list_ _cmd(s)_ done until _expr_ do _cmd(s)_ done


program, function or shell argument 1 through 9 nth argument number of positional parameters every positional parameter decimal value returned by last executed cmd pid of shell pid of last backgrounded command $0 $1 .. $9 ${n} $# $@, $* $? $$ $!

0 1 2 <&>&<>filename 2>&1 stdin stdout stderr close stdin close stdout open filename for read-write open 2 for write and dup as 1

Examples: cmd 2>/dev/null cmd >/dev/null 2>&1 exec 1<&exec 2<&exec 1< /dev/null exec 2< /dev/null

# # # #

close descriptor 1 close descriptor 2 open descriptor 1 open descriptor 2

cmd1 || cmd2 cmd1 && cmd2 V1=${V2:=V3} exec cmd2 if cmd1 fail exec cmd2 if cmd1 is OK Set V1 with the value of V2 if this is set else set the variable V1 with value of V3 (V3 could be a number). sh replacement: if [ $V2 ] ; then V1=$V2 else V1=$V3 Example: DisplaySize=${LINES:24} ; Command=${Command:"cat"} if V1 set & V1!=null ret $V1 else print word and exit : ${V1:?"variable V1 not set on null"} if V1 !set | V1==null set V1=$word if V1 set & V1!=null ret $V1 else ret word if V1 set & V1!=null ret word else ret nothing

${V1:?word} ${V1:=word} ${V1:-word} ${V1:+word} ${V1##patt}

3 of 5

1/30/2009 8:35 PM

KSH - Korn Shell Tutorial


${V1#patt} ${V1%%patt} ${V1%patt}

if patt are found at the begin of V1 return V1 whitout the patt else return V1 V1="lspwd" ; ${V1#"ls"} # exec pwd if patt are found at the end of V1 return V1 whitout the patt else return V1 V1="lspwd" ; ${V1%"pwd"} # exec ls

La ksh permette di lanciare uno o piu' comandi come processi in background. Questi processi sono chiamati coprocesses e sono utilizzati per comunicare con un programma. Un coprocess si crea mettendo l'operatore |& (pipe, ampersand) dopo un commando. Entrambi stdin e stdout del commando sono piped verso il tuo script. Un coprocess deve incontrare le seguenti restrizioni: · Includi un new-line alla fine di ogni messaggio · Manda ogni messaggio di output allo standard output · Pulisce il suo stdout dopo ogni messaggio L' esempio dimostra come l'input e' passato verso e ritornato da un coprocess: echo "Initial process" ./FileB.sh |& read -p a b c d echo "Read from coprocess: $a $b $c $d" print -p "Passed to the coprocess" read -p a b c d echo "Passed back from coprocess: $a $b $c $d" FileB.sh echo "The coprocess is running" read a b c d echo $a $b $c $d L'output risultante e' il seguente: Initial process Read from coprocess: The coprocess is running Passed back from coprocess: Passed to the coprocess Il comando 'print -p' ti permette discrivere verso il coprocess. Per leggere dal coprocess, lancia il comando 'read -p'.

- Explode a command for use parameters counter set `who -r` ; [ "$8" != "0" ] && exit - declare a variable for only uppercase/lovercase chars typeset -u VAR ; VAR="lower" ; echo $VAR -> LOWER typeset -l VAR ; VAR="UPPER" ; echo $VAR -> upper - exec - eval - esegue il comando dato come argomento - let - esegue le operazioni matematiche che passate come argomento let "x = x * 5" ((x = x * 5)) .. altra forma di let

- ritorna la prima lettera dopo il segno - all'inizio di una stringa

4 of 5

1/30/2009 8:35 PM

KSH - Korn Shell Tutorial




VAR="-ciao" RESULT=`expr "$VAR" : "-\(.\)"` echo $RESULT .. -c toglie il '-' iniziale VAR="-ciao" VAR=`expr "$VAR" : "-*\(.*\)"` echo $VAR .. ciao ritorna la lunghezza di una stringa VAR="ciao" echo `expr length $SHELL` .. 4 ritorna l'indice di dove incontra una substringa echo `expr index abcdef de` .. 4 ritorna 6 caratteri a partire dall'11 expr substr "Goodnight Ladies" 11 6 .. Ladies

- definisce un array set -A Week Sat Sun Mon Tue Wed Thu Fri - ritorna un elemento dell'array echo ${Week[3]} id=3 ; echo ${Week[id]} - stampa tutti gli elemti di un array echo ${Week[@]} - scandisce un array for day in ${Week[@]} do echo $day done - ritorna il numero di elementi in un array nelem=${#Week[@]} ; echo $nelem
Home Company Ads by Google Products Services Works Resources

.. Tue .. Tue .. Sat Sun Mon Tue Wed Thu Fri

.. 7
Sales Support News Search Map © Design by

Unix Shell Script
Save on Computer Books Compare & Buy from 1000s of Stores

Shell Scripting
Register Now & Get the Job You Want Dice - Career Hub for Tech Insiders

Learn shell by examples
Learn more from educational shell programming screencasts

Fine China Replacements
Replacements.com carries thousands of fine china patterns, old and new

5 of 5

1/30/2009 8:35 PM

Sign up to vote on this title
UsefulNot useful