Module 120 shift

Previous Table of Contents Next


Module 120
shift

DESCRIPTION

The internal shift command reassigns the positional parameters to the next lower positional number. It allows you to left shift your positional parameters. Left shifting the positional parameters causes parameter n to become n-1. For example, if you type echo $2 and then perform a shift, you can display the same value again by typing echo $1. The $2 parameter was shifted left one position to $1. The $0 parameter is not shifted because it is not considered a positional parameter.

COMMAND FORMAT

Following is the general format of the shift command.

 shift [ n ] 

Arguments

The following describes the argument that may be passed to the shift command.

n The number of positions to shift the parameters.
If no n is given then shift assumes the value of 1.

FURTHER DISCUSSION

Normally the shift command shifts parameters left by one position. But you can specify a number or a variable containing a number to shift multiple positions. The following example shows how the shift command affects the positional parameters. Use the set command to set positional parameters for your current shell. The first command is equivalent to executing a shell script and passing it the 12 positional parameters.

 cj> set  pp1 pp2 pp3 pp4 pp5 pp6 pp7 pp8 pp9 pp10 pp11 pp12       cj> echo $#: $*       12: pp1 pp2 pp3 pp4 pp5 pp6 pp7 pp8 pp9 pp10 pp11 pp12       cj> echo           pp11 pp7 pp3 pp1       cj> shift       cj> echo $#: $*       11: pp2 pp3 pp4 pp5 pp6 pp7 pp8 pp9 pp10 pp11 pp12       cj> shift 8       cj> echo $#: $*       3: pp10 pp11 pp12       cj> echo         pp10 pp12       cj> shift 3       cj> echo $#: $*       0: 

NOTE:    
The Korn shell added the feature to reference positional parameters higher than nine. The Bourne shell can only reference up to nine positional parameters; therefore, you were forced to use the shift command to access any parameters in positions greater than nine.

DIAGNOSTICS AND BUGS

If you attempt to shift parameters when there are none to shift, you will receive an error message from the shell resembling:

 ksh: prog: cannot shift 

RELATED COMMANDS

Refer to the ksh command described in Module 71 and Parameters and Variables discussed in Module 99.

APPLICATIONS

The shift command provides you a method of processing the positional parameters. If you are writing shell scripts, you may want to reassign your positional parameters to known variable names . By using the shift command in combination with a while loop and case statement, you can step through all the positional parameters, reassigning them as needed.

TYPICAL OPERATION

In this activity you use the shift command to process positional parameters in a while loop. Begin at the shell prompt.

1.    Type the following lines, pressing Return at the end of each line. You may want to store this in a file for later reference.
 cj> set  -x -y -z 30 afile myfile    cj> while [ $# -gt 0 ]   # when there are no more arguments stop loop    do    case  in     -x) XFLAG=yes ;;     -y) YFLAG=yes ;;     -z) ZFLAG=    ; shift ;; # shift once here for the -z      *) FILES=${FILES}  ;;    esac    shift  # shift the first positional parameter off the argument list.    done    echo ${XFLAG} ${YFLAG} ${ZFLAG} ${FILES} 

C Shell
 cj> set XFLAG; set YFLAG; set ZFLAG; set FILES    cj> set argv=(-x -y -z 30 afile myfile)    cj> while ($#argv > 0)    switch ($argv[1])     case -x:      set XFLAG=yes      breaksw     case -y:      set YFLAG=yes      breaksw     case -z:      set ZFLAG=$argv[2]; shift      breaksw     default:      set FILES=$FILES $argv[1]     endsw     shift     end     echo $XFLAG $YFLAG $ZFLAG $FILES 

The output looks like this.
 yes yes 30 afile myfile 

If you are using the ksh you can edit the code by pressing Escape and typing k . Repeat typing a k until the while appears on your command line; press v to enter vi to edit the file. When you finish editing the file type ZZ and the command is executed again.
2.     Turn to Module 121 to continue the learning sequence.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net