《理学kshell》PPT课件.ppt_第1页
《理学kshell》PPT课件.ppt_第2页
《理学kshell》PPT课件.ppt_第3页
《理学kshell》PPT课件.ppt_第4页
《理学kshell》PPT课件.ppt_第5页
已阅读5页,还剩226页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

LCU123H: Ksh for Programmers,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,2,Course objectives,Introduce students to the unique features of Korn shell. Familiarize students with the reference text, The New Korn Shell Command and Programming Language, so they can make effective use of the language. Introduce students to some of the UNIX commands that support use of Korn shell as a programming language (e.g., sed, cgrep). Use hands-on exercises to help students remember the capabilities of the language.,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,3,Prerequisites,Basic understanding of UNIX shell as a command language. We will be emphasizing the unique features of ksh, rather than teaching UNIX shell programming from scratch. Knowledge of basic UNIX commands. Programming experience. This course makes use of simple programming examples to illustrate language concepts, rather than to teach programming.,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,4,Instructor information,Vickie Klick Lee Herrbach IH 6K-321 IH 6K-317 (630) 713-7076 (630) 979-3232 ,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,5,Course information,Two days 8:30-4:30 Note: This course compresses a lot of information into a short time. If you do not have the prerequisites for this course or you want more class time to reinforce the concepts, the ksh-related LPC courses at /ksh/training.html may be a better match for your requirements. Class is “hands on“ - you will try ksh constructs as we go over them You are strongly encouraged to read the text and do additional exercises outside class time Questions and problems from your actual use of ksh are welcome,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,6,Course outline Basic ksh,Overview of ksh book Why use ksh? Setting up your environment to use ksh Customizing ksh in your .profile Ksh command line options Basic ksh command line processing Command line editing Basic ksh notation and concepts,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,7,Ksh as a programming language,Coding style Variables and parameters Some ksh-defined variables Positional parameters Special parameters Parameter expansions Control constructs Patterns Functions Getopts,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,8,More ksh programming concepts,Set/unset Typeset Arrays I/O Arithmetic Cgrep Sed Regular expressions,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,9,Advanced concepts,Command line processing Eval Exec Co-processes IFS,Ksh for Programmers,Basic Ksh,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,11,Basic ksh,Overview of ksh book Why use ksh? Setting up your environment to use ksh Customizing ksh in your .profile Ksh command line options Basic ksh command line processing Command line editing Basic ksh notation and concepts,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,12,Overview of ksh book,We will be looking up topics in the book on a regular basis throughout the course Table of contents Back cover Index It is most important to be able to FIND what you need in the book, not to remember it all When in doubt, try it out!,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,13,Why use ksh?,A better command language A flexible and powerful programming language,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,14,A better command language,Command-line editing (vi or emacs) Command history User-defined aliases for commonly used commands Enhanced job control (available on SUNs in our environment),LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,15,A flexible and powerful programming language,Interpretive Portable across most UNIX environments Substring operators Built-in arithmetic Arrays Functions Flexible control constructs Menu primitives Faster than Bourne shell,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,16,Setting up your environment to use ksh,In our environments, use the chsh command with /bin/ksh, /usr/bin/ksh, or /usr/lbin/ksh (depending on your machine) To use ksh93 on ihgp, do chsh and set to /bin/ksh93 You can also use ksh by just typing ksh on the command line,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,17,Customizing ksh in your .profile,VISUAL/EDITOR/FCEDIT (editor selection) ENV (user environment setup file, often set to .env) HISTFILE (history file name, defaults to .sh_history) HISTSIZE (# of commands in history file, default is system dependent) Both HISTFILE and HISTSIZE take effect when ksh first accesses the history file (e.g, when you log in), rather than when the variables are modified,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,18,Ksh command line options,Can be set in current ksh environment with set command Use - to turn on, + to turn off Example: set -x turns on execution tracing set +x turns off execution tracing -n noexec - syntax check only Usage: ksh -n set -n does not turn execution off in an interactive shell Dont do ksh -n without a filename - commands will no longer be executed in your interactive shell!,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,19,Ksh command line options (continued),-m monitor - display job numbers for background processes and report when they complete -x xtrace - display each line as it is executed by ksh, useful for debugging -v verbose - display each line as it is read by ksh -o (without options) - list currently active options -o ignoreeof - must type exit to quit, not ctrl-d,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,20,Ksh command line options (continued),-o noclobber - redirection to with the operator will fail if exists -u nounset - will give an error if an unset variable is used Do not use this option when running automated tests! This will cause unexpected failures. -f or -o noglob - turn off pathname expansion There are many other options - see book for more information,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,21,Basic ksh command line processing,Basic command format: command e.g., ls -l .profile,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,22,A simple programming example,for file in * do print $file wc -l $file file $file/头一个是命令 done,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,23,Basic pattern characters,* - matches zero or more occurrences of any characters ? - matches any single character . - matches any one of the set of characters inside the . If the first character inside is !, matches any one character NOT included in . /match 序列中的任何一个,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,24,Basic file descriptors,stdin (file descriptor 0) stdout (file descriptor 1) stderr (file descriptor 2) /标准的输入输出,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,25,Command line editing,Set and export VISUAL to set your preferred editor (emacs or vi) Some tools may still expect to find EDITOR set to emacs or vi - safer to set both in your .profile Can also do set -o vi or set -o emacs to change the editing mode for a particular ksh instance There are additional commands for each editing mode that dont exist in the editors themselves - see emacs and vi chapters in book,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,26,Command line editing (continued),In vi editing mode, a history search brings back the entire command (with ctrl-js separating lines) In emacs mode, a history search brings back the matching line, and ctrl-o enters that line and brings up the next one for editing,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,27,Basic ksh notation and concepts, for command line or programming use,Tilde Aliases Search order (how ksh finds commands) Whence/查找命令的属性 I/O redirection Job control /前台或者后台控制 Quoting论/引用 /一些基本的kshell的标记,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,28,Tilde, by itself expands to $HOME Note: $ is generally equivalent to just $ in front of a variable name to evaluate it, but is unambiguous when the variable name is not followed by whitespace. Special expansions of variable values require use of $ expands to the home directory of login + expands to $PWD when the command is executed - expands to $OLDPWD (last used directory) when the command is executed tilde expansion is attempted up to a /,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,29,Aliases,Shorthand for frequently used commands - also allows you to customize commands with options you always want to use alias e=emacs alias lc=“wc -l“ alias netscape=“netscape -install &“ Only replaces the leftmost part of the command, so you cant use it to insert characters between parameters, etc.,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,30,Aliases (continued),To make an alias available across ksh instances (e.g. separate windows), put it in the $ENV file If the trackall (set -h) option is set, ksh also defines aliases itself (called tracked aliases) to speed up command execution Even without trackall set, some /bin commands are tracked Tracked aliases can be listed with alias -t,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,31,Search order (how ksh finds commands),Aliases (substituted, then the process starts over) Pathnames (/ in name) Reserved words (e.g., while) - then keeps reading until command is complete, an error is found, or processing is interrupted by the user Built-in commands (e.g., print),LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,32,Search order (continued),Functions (already defined in the current ksh environment, or in FPATH and defined to be autoloaded) Tracked aliases Search down PATH Search down FPATH for a file with the specified name / function path,kshell 有一个特殊的变量invoke,用来获取运行时间,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,33,Search order (continued),NOTE: It is important to have the directories in FPATH not be included in PATH, and/or the files containing function definitions must not be executable. Otherwise, the executable file will be found in the PATH, executed (which will define the function in a subshell).and nothing will happen in the current environment.,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,34,Whence,A ksh builtin command that gives the absolute pathname to the command as it will be executed (if it occurs more than once in PATH, the first occurrence is listed). whence -v displays what type of item the command is: reserved word, alias, built-in, program, etc.,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,35,Whence (continued),type is a built-in alias for whence -v: type for for is a keyword type ls ls is a tracked alias for /bin/ls type type type is an exported alias for whence -v,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,36,Whence (continued),where is an exptool that returns the location(s) of a command or file in PATH or another pathlist. where is useful if you need to find out if a command is defined in multiple directories, but it does not know about aliases, ksh builtins, functions, etc.,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,37,Example,In your current working directory, create a file, “tryit“, containing a function by the same name: function space tryit print space “Executed tryit“ return space 0 Make sure that tryit isnt already in your environment: whence -v tryit tryit not found,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,38,Example (continued),Put your current working directory in your PATH and FPATH: PATH=$PATH:+ FPATH=$FPATH:+ See how tryit appears in the environment now: whence -v tryit tryit is an undefined function Make the file tryit executable: chmod +x tryit See how tryit appears now: whence -v tryit tryit is /home/tryit,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,39,Example (continued),Run tryit by just typing the filename (nothing should happen): tryit Turn off execute permissions for tryit: chmod -x tryit Verify how tryit appears now: whence -v tryit tryit is an undefined function Run tryit again (this time it should run successfully) tryit Executed tryit,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,40,I/O Redirection,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,41,Example,Print the current date and redirect the output to a file named datefile: date datefile Look at the contents of datefile: cat datefile Turn on the noclobber option so that ksh will not allow the “ redirection operator to truncate an existing file: set -o noclobber,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,42,Example (continued),Try to write the date to datefile again: date datefile /bin/ksh: datefile: file already exists Use the “|“ redirection operator to force ksh to truncate the existing file and overwrite with the current date and time: date |datefile Look at the contents of datefile and see that it changed: cat datefile Turn the noclobber option back off: set +o noclobber,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,43,Job control,The following four commands are only available on systems that support enhanced job control. In our environment, the SUN networks (ihgp, etc.) support them, but the MAXIs (ihlpq, etc.) do not. bg - restarts a stopped job in the background fg - brings a job into the foreground ctrl-z stops the current foreground job kill -STOP stops the specified background job (or it will stop on its own when it needs keyboard input) /前台foreground后台background的各种命令,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,44,Job control (continued),The following commands are generally available: jobs - lists background jobs started by this ksh process wait - wait for a specified job to terminate kill - terminate a specified job All of these commands (except jobs) either take a specific process ID or a job number (%). The process ID of the last background process started is in variable !, and the current process ID is in variable $ /Bg %num 将job号为num放到后台,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,45,Example (only works on systems ihgp*, etc. with enhanced job control),Start a program that will take a few seconds and then ask for user input: pg /.profile IMMEDIATELY stop the program (so it doesnt get to the page prompt before you stop it): ctrl-z 1 + Stopped pg /.profile Look at the job status: jobs 1 + Stopped pg /.profile,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,46,Example (continued),Put the job in the background: bg %1 1 pg /.profile& Look at the job status again: jobs 1 + Stopped(SIGTTOU) pg /.profile (The abbreviation for the reason why the job is stopped may be different on different systems, but the meaning is the same: the background job needs keyboard input),LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,47,Example (continued),Bring the job back into the foreground to give it input: fg %1 pg /.profile :q Execute a command in the foreground that normally should be backgrounded: xv Stop the program: ctrl-z 1 + Stopped xv,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,48,Example (continued),Put the program into the background, as originally intended: bg %1 1 xv& Terminate the backgrounded command: kill %1,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,49,Quoting,Double quotes “ Used primarily to make items separated by whitespace (spaces, tabs, or newlines) appear to be a single item to ksh Variables are evaluated within double quotes: var1=frog print “The $var1 jumped over the log” The frog jumped over the log /重要,变量有扩展,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,50,Quoting (continued),Single quotes Also used to make items separated by whitespace appear to be a single item, BUT no other special characters (e.g., $) are treated as special within single quotes (Try the previous print with single quotes instead of double quotes) /注意区别于Double quoting,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,51,Quoting (continued),Backslash Removes the special meaning of a single character, e.g., “ enters a double quote as a regular character instead of the start of a quoted string. Particularly useful with eval (to be covered later in the course) Also used to denote special characters, e.g., n for newline, t for tab - understood by print, some other commands,Ksh for Programmers,Ksh as a programming language,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,53,Ksh as a programming language,Coding style Variables and parameters Some ksh-defined variables Positional parameters Special parameters Parameter expansions Control constructs Patterns Functions Getopts,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,Coding Style,Indent code inside compound commands (while, if/then/else/fi, case, etc.): while ( counter 0 ) do done # begins a comment (can follow code on a line): # This is the way to do a block comment # in ksh. There is no separate delimiter # for block comments typeset var1=“ # comment about var1,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,55,Variables and parameters,Three types of parameters:/记忆 Variables are named parameters which may be defined by the user or by ksh itself Positional parameters are numbered, and are usually set automatically by ksh on entry to a script or function Special parameters: * # ? - $ ! To expand any parameter (get its value), use $ around the name of the parameter,LCU123H: Ksh for Programmers Lucent Technologies - Proprietary,56,Variables and parameters (continued),Valid characters in a variable name are A-Z

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论