KSH - Korn Shell Tutorial
1 of 5
http://b62.tripod.com/doc/docksh.htm
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/
Search:
The Web
Tripod
Report Abuse
[email protected]
« Previous | Top 100 | Next » share: del.icio.us | digg | reddit | furl | facebook
Take Our Survey! Ads by Google
Web Scripting Program
VMM 2008 Free Trial
Apply to The Art Institute Online - Earn a Web Design Degree.
Download Microsoft Virtual Machine Manager & Centralize IT Management
www.aionline.edu
www.microsoft.com/systemcenter/VMM
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/30/2009 8:35 PM
KSH - Korn Shell Tutorial
2 of 5
http://b62.tripod.com/doc/docksh.htm
(( _num1_ > _num2_ )) (( _num1_ <= _num2_ )) (( _num1_ >= _num2_ ))
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
_str1_ == _str2_ ]] _str1_ != _str2_ ]] _str1_ < _str2_ ]] _str1_ > _str2_ ]] _str1_ = _pattern_ ]] _str1_ != _pattern_ ]] -z _str_ ]] -n _str_ ]]
[ x=y -o k=j ] [ x=y -a k=j ]
or in expression and in expression
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
-d -f -h -p -b -c -L -S -O not
"sticky" bit set set-group-ID bit set set-user-id bit set
-k -g -u
opened on a terminal
not
Format of flow control functions "if-then"
if _expr_ then _cmd(s)_ elif _expr_ _cmd(s)_ else _cmd(s)_ fi
"case"
case _word_ in _pattern1_) _pattern2_) *) esac
"while"
_cmd(s)_ _cmd(s)_ break ;;
while _expr_ do _cmd(s)_ done
1/30/2009 8:35 PM
KSH - Korn Shell Tutorial
3 of 5
http://b62.tripod.com/doc/docksh.htm
"for"
for _variable_ in _list_ _cmd(s)_ done
"until"
until _expr_ do _cmd(s)_ done
POSITIONAL PARAMETER 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} $# $@, $* $? $$ $!
REDIRECTIONS 0 1 2
stdin stdout stderr
<&>&<>filename 2>&1
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
OTHER FUNCTIONALITIES cmd1 || cmd2 cmd1 && cmd2
exec cmd2 if cmd1 fail exec cmd2 if cmd1 is OK
V1=${V2:=V3}
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"}
${V1:?word}
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##patt}
1/30/2009 8:35 PM
KSH - Korn Shell Tutorial
4 of 5
${V1#patt} ${V1%%patt} ${V1%patt}
http://b62.tripod.com/doc/docksh.htm
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
COPROCESS 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'.
EXAMPLES - 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
REGULAR EXPRESSION - ritorna la prima lettera dopo il segno - all'inizio di una stringa
1/30/2009 8:35 PM
KSH - Korn Shell Tutorial
5 of 5
-
-
http://b62.tripod.com/doc/docksh.htm
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
ARRAY - 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
Products
Services
Works
Resources
.. Tue .. Tue .. Sat Sun Mon Tue Wed Thu Fri
.. 7 Sales
Support
News
Search
Map
© Design by
Ads by Google
Unix Shell Script
Shell Scripting
Learn shell by examples
Fine China Replacements
Save on Computer Books Compare & Buy from 1000s of Stores
Register Now & Get the Job You Want Dice - Career Hub for Tech Insiders
Learn more from educational shell programming screencasts
Replacements.com carries thousands of fine china patterns, old and new
www.Shopping.com
www.Dice.com
shellshore.com
www.replacements.com/
1/30/2009 8:35 PM