Please contact or Mt Xia for assistance with all your shell programming needs.


  • 9. OPTIONAL CHAPTER - KORNSHELL PROGRAMMING. Go to the bottom, first,previous,next, last section, table of contents.

    9. OPTIONAL CHAPTER - KORNSHELL PROGRAMMING

    The KornShell is a command-programming language that executes commands read from a terminal or file. The KornShell is close to being upward compatible with the standard BourneShell. For example, scripts written for the BourneShell (sh) will also work in the KornShell (ksh). The major enhancements are command-line reentry, in-line command editing, and aliasing. This chapter will deal with some, not all, of the features that differentiate it from the BourneShell. For your information, here is a list of features that have been enhanced from Bourne. Improvements have been made as a command language, including command-line editing, a command history mechanism, command-name aliasing, job control, new capabilities for cd, and tilde expansion. Improvements as a Programming Language include a more general I/O mechanism, Menu selection primitive, Built-in integer arithmetic, Substring operators, Array variables and attributes, More-general function facility, Co-process facility, Easier to debug, Better performance, and Better security. The KornShell is a high level programming language and a command line interpreter.
      Command Format: ksh [acefhikmnorstuvx] [ -o option]...[args] 
                                                                   
          See the appendix for a complete list of options          
    

    9.1 KornShell Variables

    The KornShell has variables that are initialized for you on login such as: PATH Determine which directories are searched, sequentially for shell commands. CDPATH The directories that are searched for arguments to the "cd" command. SHELL The current interactive shell. PWD The present working directory. IFS Valid inter-field separators used to separate commands from arguments, arguments from each other and commands from each other. EDITOR Command line edit mode. FCEDIT Editor entered when using fc TERM Defines terminal capabilities PS1 Primary shell prompt PS2 Secondary shell prompt PS3 Select command prompt HOME Home directory ENV File(s) to execute when entering this shell LOGNAME Login name of the user The command line argument variables are also available: $$, $?, $*, $#, and the positionals ($0, $1, $2...etc)

    9.2 User Defined Variables

    These variables are similar to the BourneShell. The general form is VARIABLE=value. No spaces are allowed around the =. You can enclose them in double quotes "=" or single quotes '=' for clarity. No spaces are allowed in "value". These can also be enclosed in double or single quotes. "value" can be a string or an expression. The value of a variable can be accessed by preceding the name of the variable with a dollar sign ($). Examples: $MyString'='"This is a string" $MyStatic=47 $readonly Mystatic The readonly command makes MyStatic read only (can't change the contents). Example: $typeset -i BIGINT=1492 This will make the variable integer for faster arithmetics: $typeset -i8 OCTINT=9 The output of OCTINT will be an octal integer; the assignment is decimal. Sample Session: $echo $OCTINT 8#11 $

    9.3 Values of Variables Between Child and Parent Processes

    Values in one shell are local only to that shell. If a child process or a subshell needs to have access to a value established in its parent, the value must be exported from the parent. $typeset -i8 -x OCTINT=19 The -x option exports the variable OCTINT; subshells can read it, but can't change the value in the parent. $MyString='Hi there' At this point, the variable MyString is local to the current process. It is not available to a child process. $export MyString Now the variable MyString is available to subshells. To allow a subshell to change the content of an exported variable and have that change be know to the parent, execute the child with ". program_name" Sample Session: $cat my.vars echo Variable coming into script: $PARENT PARENT='child value' echo Variable coming out of the script: $PARENT $$PARENT='parent value' $echo $PARENT parent value $my.vars Variable coming into script: Variable coming out of the script: child value $echo $PARENT parent value $ Sample session: $PARENT='parent value' $echo $PARENT parent value $. my.vars Variable coming into the script: parent value Variable coming out of the script: child value $echo $PARENT child value $

    9.4 ksh: Aliases

    The first word of each command can be replaced by the text of an alias if an alias for this word has been defined. The first character of an alias name can be any printable character, but the remaining characters must be the same as any valid identifier. Aliases can be created, listed, and exported with the alias command. They can be removed with the unalias command. Aliasing is performed when scripts are read but not while they are executed. Therefore, for an alias to take effect the alias command has to be executed before the command which references the alias is read. The format for the alias command is as shown below.
      Command Format:  alias [-tx] [name=value]                    
                                                                   
      -t = to set and list tracked aliases                         
      -x = to set or display exported aliases                      
                                                                   
      name = if specified, it must be a valid alias name           
                                                                   
      value = value to equate with name                            
    
    Example: $alias The command alone will display all aliases. Sample Session: $alias cd=c echo=print - false=let 0 functions=typeset -f hash=alias -t history=fc -l integer=typeset -i monitor=/usr/lbin/top nohup=nohup pwd=print - $PWD r=fc -e - true=: type=whence -v $ Suppose, instead of typing in the ls -la command to get a full, long listing of the contents of the current directory, we want to shorten the command to list. Enter the following command to set the alias. Example: $alias list='ls -la' Now when you type in the command list, the alias will substitute the command ls -la for list; and the long listing will be displayed. Sample Session: $list total 54 0 drwx------ 4 teacher class 2590 May 1 09:39 . drwxr-xr-x 63 teacher class 1536 Sep 9 13:11 .. -rw-r--r-- 1 teacher class 64 Jul 4 10:33 .assistrc . .

    Go to the top, first,previous,next, last section, table of contents.