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.5 ksh: Command Line Editing

    There are two forms of command-line editing in the KornShell. Both use the command history in the file indicated by the KornShell variable HISTFILE (default is HISTFILE=$HOME/.history). Editing commands in the history file is accomplished with fc (fix command). To get a list of commands in the .history file, you enter the fc -l command at the dollar ($). By default, the alias history may also be used. Editing a command in the .history file with the fc command is controlled by the KornShell variable FCEDIT. FCEDIT determines the editor that the fc command will use. Example: $FCEDIT=/usr/bin/vi $fc The KornShell variable causes the fc command to use the vi editor. The fc command, by itself, will take you into the vi editor with the most recent command. In this example, upon exiting the vi editor, the edited command will be executed. Example: $ct .profile .profile: No such file or directory $fc -e - ct=cat c The first line is a deliberate mistake; notice the error message. The fc command executes the most recent command that starts with a "c" and changes the first occurrence of "ct" to "cat"; it doesn't enter the editor. Example: $ls / $fc -e - ls=cd The first command will list the contents of the root directory. The fc command changes that command from "ls" to "cd"; the "-" indicates that the line is to be edited instead of taken into the editor before execution. 9.6 ksh: Interactive Command Line Editing In this method of command-line editing, the EDITOR KornShell variable controls the editing. Example: $EDITOR=/usr/bin/vi This command will put the KornShell in the vi editing mode. To enter the edit history press Esc. NOTE: This example is for the vi edit mode only; emacs or gmacs edit modes use different key stokes. To move though the .history file use following keys: Select the previous command Select the next command Next letter to the left Next letter to the right When the command line that you desire to change is displayed on the screen, you can use the following commands to make changes: insert chars Esc Insert characters before the cursor
    append chars Esc Append characters at the end of the line replacement char Esc Change single character replacement word Esc Change single word Delete single character NOTE: A number can precede the command as a count, for example, "3x" deletes 3 characters. Delete single word NOTE: A number can precede the command as a count, for example, "2dw" deletes 2 words. (Ret) Execute the altered command When the (Ret) key is pressed, the edited command line will be executed. History is not changed, but the new command is entered into the .history file at the end. These are not the only commands available with the interactive command line editing. You will find that the arrow keys will not work while editing the command line. Most of the editor features will be available for you to use, depending upon which editor was selected by the EDITOR shell variable. 9.7 ksh: Functions Functions are similar to aliases, they run in the current KornShell process and define a set of actions. Positional parameters can be referenced; other previously defined functions can be referenced; nested function definitions are allowed with full visibility to all definitions. The function keyword is used to define KornShell functions. The functions are read in and stored internally. Any alias names are resolved when the function is read. Functions are executed like commands with arguments passed as positional parameters. Functions execute in the same process as the caller and share all files, traps, and present working directory with the caller. The format for functions is:
      Command Format:                                              
      function  identifier                                         
            compound list                                          
      identifier - name by which the function is called            
      compound list - The body of the function.                    
    The left bracket { and the right bracket } are considered to be reserved words. The body of the function must exist between the two brackets. Sample Session: $function k { cd /; ls -C; } $ To execute the function, simply type the name of the function on the command line. Sample Session: $k bck lib tmp user12 user5 bin lost tmp.ja user13 user6 bsd nbox unix user14 user7 . . .

    9.8 ksh: The Select Construct

    The "select" construct is unique to the KornShell. It allows the user to determine the action based on input from either the command line (without an in list) or from an automatically prompted input. PS3 controls the "select" prompt. The command format is as follows:
      Command Format:  select identifier [in list]                 
    In the select command: ksh will display the items in one or more columns on standard error, each preceded by a number. The PS3 prompt follows. The number of columns is determined by the values of COLUMNS and LINES. ksh will then read a selection line from standard input. If the line is the number of one of the displayed items, ksh sets the value of "identifier" to the item corresponding to this number. If the line is empty, ksh again displays the list of items; and the prompt is redisplayed. The "commands" are not executed. ksh saves the contents of the selection line read from standard input in the KornShell variable REPLY. ksh runs "commands" for each selection until ksh encounters a break, return, or exit command in the "commands" list. Sample Session: $cat select.ksh stty erase select myselection in fred wilma pebbles barney betty do case $myselection in fred) echo Fred was the selection ;; wilma) echo Wilma was the selection ;; pebbles) echo Pebbles was the selection ;; barney) echo Barney was the selection ;; betty) echo Betty was the selection ;; esac done $chmod 755 select.ksh $select.ksh 1) fred 2) wilma 3) pebbles 4) barney 5) betty #? 3 Pebbles was the selection #? 5 Betty was the selection #? 6 #? 4 Barney was the selection # Ctrl-C $

    9.9 ksh: Tracing and Conditional Execution

    A KornShell script that is not executable can be run implicitly with the ksh command. Tracing can be accomplished using the -v or the -x option. Sample Session: $ls -l select.ksh -rw-r--r-- 1 teacher class 390 Oct 16 09:21 select.ksh $ksh -x select.ksh + stty erase 1) fred 2) wilma 3) pebbles 4) barney 5) betty #? 3 + print - Pebbles was the selection Pebbles was the selection #? 5 + print - Betty was the selection Betty was the selection #? 6 #? 4 + print - Barney was the selection Barney was the selection #? Ctrl-C $ The -n option will trace execution of the script without execution.

    Workshop 9

    This workshop will reinforce your understanding of the ideas presented in Chapter 10. Login to the Multimax using the username and password given to you by the instructor. Each student is to complete the entire workshop. DESK EXERCISES 1. What command will invoke the KornShell? 2. What option will trace execution of a KornShell script? Continue on the next page 3. What do the following shell variables indicate: PATH CDPATH SHELL PWD IFS EDITOR FCEDIT TERM PS1 PS2 PS3 HOME ENV LOGNAME 4. What is an alias? COMPUTER EXERCISES (30 minutes) 5. Define a function k that will: Display the present working directory, display a message that a listing will follow, sleep for three seconds, and then list the contents. 6. Set up an alias to do the ls -C function. Use a name of your own choice. 7. Write a KornShell script using the select command to display the following choices: Apples Bananas Pears Jack Daniels After a choice has been made print the following: "Thanks, your choice was" (display the choice) 8. Logout

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