ksh 案例
Ksh Scripting
Principle of Script
? Principle of Script Defining the Shell Type Four Types of Lines #!/usr/bin/ksh # Commentary...... file=/path/file Start and End of Script Start and End of Command print -n "Name: "; read name; print ""
grep filename | sort -u | awk '{print $4}' | / Name and Permissions of Script File ? -------------------------------------------------------------------------------- ? Variables Filling in Using Arrays arrname[1]=4 To fill in Declaration ? -------------------------------------------------------------------------------- ? Branching if then fi if [[ $value -eq 7 ]] if [[ $value -eq 7 ]];then print "$value is 7";fi if then else fi if then elif then else fi case esac -------------------------------------------------------------------------------- Looping while do done until do done for var in list do done continue...break while read line One can also prematurely leave a loop with: "break". while read line;do -------------------------------------------------------------------------------- Command Line Arguments (Officially they are called "positional parameters") The number of command line arguments is stored in $# so one can check
The single Arguments are stored in $1,....$n and all are in $* as one string. The arguments cannot
One can iterate over the command line arguments with the help of the shift command. Shift indirectly removes the first argument.
One can also iterate with the for loop,the default with for is $*: for arg;do The program name is stored in $0 but it contains the path also! ? -------------------------------------------------------------------------------- ? Comparisons To compare strings one uses "=" for equal and "!=" for not equal. if [[ $name = "John" ]];then With "&&" for "AND" and "||" for "OR" one can combine statements: if [[ $price -lt 1000 || $name = "Hanna" ]];then ? -------------------------------------------------------------------------------- ? Variable Manipulations Removing something from a variable
? -------------------------------------------------------------------------------- ? Ksh Regular Expressions Ksh has it's own regular expressions. Especially in ksh there are quantifiers for whole patterns: ?(pattern) matches zero or one times the pattern. So one can question a string in a variable like: if [[ $var = fo@(?4*67).c ]];then ... ? -------------------------------------------------------------------------------- ? Functions Description
foo(){ Calling the Function
? -------------------------------------------------------------------------------- ? Data Redirection General standardinput,standardoutput and standarderroroutput. All of these can be redirected. Command Output to File For appending to a file do: command >> file Standard Error Redirection To discard the error alltogether do: command 2>/dev/null To put the error to the same location as the normal output do: command 2>&1 File into Command Combine Input and Output Redirection Commands into Program ( Here Document ) command <<EOF From eof to eof all is feeded into the above mentioned command. ? -------------------------------------------------------------------------------- ? Pipes For a serial processing of data from one command to the next do: ? -------------------------------------------------------------------------------- ? Coprocesses One can have one background process with which one can comunicate with read -p and print -p. It is started with command |&. If one uses: ksh |& then this shell in the background will do everything for us even telnet and so on: print -p "telnet hostname". ? -------------------------------------------------------------------------------- ? Read Input from User and from Files Read in a Variable Read into a File Line for Line { while read myline;do To catch the output of a pipeline each line at a time in a variable use: last | sort | { ? -------------------------------------------------------------------------------- ? Special Variables $# Number of arguments on commandline. ? -------------------------------------------------------------------------------- ? Action on Success or on Failure of a Command If one wants to do a thing only if a command succeded then: command1 && command2. If the second command has to be performed only if the first one failed,then: command1 || command2. ? -------------------------------------------------------------------------------- ? Trivial Calculations Simpe calculations are done with either a "let" in front of it or within (( ... )). One can increment a variable within the (( )) without a "$": (( a+=1 )) or let a+=1. ? -------------------------------------------------------------------------------- ? Numerical Calculations using "bc" For bigger caluculations one uses "bc" like: $result=$(print "n=1;for(i=1;i<8;i++)n=i*n;n"|bc) ? -------------------------------------------------------------------------------- ? "grep" Search for the occurence of a pattern in a file: grep 'pattern' file. If one just wants to know how often soemthing occurs in a file,then: grep -c 'pattern file. This can be used in a script like: ? -------------------------------------------------------------------------------- ? "sed" Sed means stream line editor. It searches like grep,but is then able to replace the found pattern. If you want to change all occurences of "poor" with "rich",do: sed -e 's/poor/rich/g' filename. Or what is often seen in software packages,that have to be compiled after getting a propper configuration,is a whole file stuffed with replacements patterns like: /@foo@/s;;king;g. This file with inumerable lines like that has to be given to sed with: sed -f sedscript filename. It then precesses each line from file with all the sed commands in the sedscript. (Of course sed can do much more:-)) ? -------------------------------------------------------------------------------- ? "awk" Awk can find and process a found line with several tools: It can branch,loop,read from files and also print out to files or to the screen,and it can do arithmetics. awk '$1 !~ /^#/ && $0 ~ /[^ ]/ {print $2+$3+$4,"/t",$1}' filename. This ignores lines with a "#" at the beginning of the first field and also blank lines. It then prints the desired sum and the $1 ist only printed after a tab. This is the most trivial use of awk only. Check my AWK programming introduction bye clicking on this sentence! ? -------------------------------------------------------------------------------- ? "perl" Perl is a much richer programming language then ksh,but still one can do perl commands from within a ksh script. This might touch Randal,but it's true. Let's say you want to remove all ^M from a file,then take perl for one line in your ksh script: perl -i -ep 's//015//g' filename. Perl can do an infinite amount of things in many different ways. For anything bigger use perl instead of a shell script. Check my PERL programming introduction bye clicking on this sentence! ? --------------------------------------------------------------------------------
? -------------------------------------------------------------------------------- (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |