Você está na página 1de 27

Chapter 12 Working with ksh

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 2

Variables - Why ?


Special values for the ksh, define a compatible work environment, according to the programmers needs. Shortcut of commands - aliases. There are some variables with predefined names and special tasks. Variables are used in scripts.

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 3

Variables
  

ksh allows variable definition and use. A variable can contain one single value. Referring to the value of a variable: $varname For example: $ variable=value $ print $variable

The list of variables maintained by the ksh may be viewed at any time.
$ set

Copyright: Sela Software Labs and Amdocs

U1

Single Value Variables and Multi Value Variables




Chapter 12 - Working with ksh - 4

A variable in ksh can contain list of values. This is called an array. Array cat contain of up to 1024 elements. Assigning multiple values:
one by one: $ color[0]=Orange $ color[1]=Red $ color[2]=Green all values at the same time: $ set -A varname val1 val2 val3

Example:
$ set -A color Orange Red Green

Copyright: Sela Software Labs and Amdocs

U1

Single Value Variables and Multi Value Variables contd




Chapter 12 - Working with ksh - 5

Accessing specific element in the array:


$ print ${var[x]} where x is the element's index (starting from 0).

Example:
$ print ${color[100]} Blue

Referencing all elements:


$ print ${color[*]}

Orange Red Green




Arrays can have holes.


Example: color[100]=Blue color[200]=Yellow
Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 6

Examples of Using Variables




Assigning a value to a variable: $ my_dictionary=hebrew.doc $ set -A dictionaries hebrew.doc english.doc Using variables value as a command line argument: $ cat $my_dictionary Using { } brackets: $ file=X $ ls -l ${file}files

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 7

String Length and Array Size


 

Using ${#var}, it is possible to know a strings length Example: $ NAME=TAMARA $ print ${#NAME} 6 You can know the size of an array (its number of elements) by using ${#varname[*]} Example: $ set -A x 111 222 333 4 5 $ print ${#x[*]} 5

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 8

Command Substitution


Allows the output of a command to be substituted in place of the command name itself. Command substitution occurs when the command is enclosed as follows: $(command) or `command`

The shell will expand the command substitution by executing command in a subshell environment and replacing the command (the text of the command plus the enclosing $( ) or backquotes) with the standard output of the command.

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 9

Using Command Substitution


$ command $(sub command) or $ command `sub command`

Question: Why does ksh have two command substitution syntax? Hint: $ command $(command $(command))

Copyright: Sela Software Labs and Amdocs

U1

Main Uses for Command Substitution




Chapter 12 - Working with ksh - 10

Inserting a command result into a variable: $ set -A X $(cat file) Passing parameters to the primary command: $ lpr $(egrep -l important *trash*) Remark: Pay attention to the difference: $ egrep -l important *trash* | lpr The command substitution $(cat file) can be replaced by the equivalent but faster $(<file)
Copyright: Sela Software Labs and Amdocs

U1

Command Substitution More Facts




Chapter 12 - Working with ksh - 11

You can get the result of sub-command as a set of strings or as a single string that contains spaces between the words. Example: % ls *.c caro.c shalom.c

main.c

Use of the command substitution with set -A $ set -A FILES $(ls *.c) $ print ${#FILES[*]} 3 Use of the command substitution without set -A $ FILES=$(ls) $ print ${#FILES[*]} 1
Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 12

More Examples


View the files that contain the word salary: $ cat $(grep -l salary *) View the content of the largest file:
set A arr $(ls -s | tail +2 | sort -n | tail 1)
The files list and their sizes Sort the file list and remove the -total- line

cat ${arr[1]}
View the files content

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 13

Arithmetic Expressions
 

Allows the result of an arithmetic expression be substituted in place of the expression. arithmetic expression substitution occurs when the command is enclosed as follows:
$(( expression ))

The arithmetic expression substitution provides only integer arithmetic. Examples:


$ 4 $ $ $ 5 print $(( 2 + 2 )) x=6 x=$(($x - 1)) print $x

Operators: + - * / % ()

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 14

Variable Expansion


We have seen simple variable expansion such as ${variable} ksh provides many more options for variable expansions. ${variable:-word} Use Default Values. If variable is unset or null, the expansion of word will be substituted. otherwise, the value of parameter will be substituted. $ print ${a:-empty} empty $print $a $a=value $print ${a:-empty} value
Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 15

Variable Expansion contd


${variable:=word} Assign Default Values. If variable is unset or null, the expansion of word will be assigned to variable. $ unset a $ print ${a} $ print ${a:=empty} empty $ print ${a} empty $ print ${a:=full} empty
Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 16

Variable Expansion contd


${variable:?[word]} Indicate error if null or unset. If variable is unset or null, the expansion of word (or a message indicating it is unset if word is omitted) will be written to standard error In a shell script, the script will exit with a non-zero exit status. $ print ${a}
$ a=full $ print ${a:?error} full $ unset a $ print ${a:?error} error $ b=invalid variable $ print ${a:?$b} invalid variable $ print ${a:?} ksh: a: parameter null or not set

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 17

Variable Expansion contd


${variable:+[word]} Use Alternate Value. If variable is unset or null, null will be substituted, otherwise, the expansion of word will be substituted. $ unset a $ print ${a:+full} $ a=value $ print ${a:+full} full $ print $a value

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 18

Variable Expansion contd




There are more options for variable expansion. There are ways to distinguish between unset variables and existing variables assigned a null value. Some of the operators can be used with other shell mechanisms such as wildcards and command substitution. There are operators that can cut suffixes and prefixes. Read man ksh for more information.

Copyright: Sela Software Labs and Amdocs

U1

Constant and Known Names of Variables


  

Chapter 12 - Working with ksh - 19

Their values are defined by default during the login process. You can change their values, but carefully. Viewing the set variables by using set. Important examples: HOME - The pathname of the home directory. You can go home by using cd. HISTSIZE - The number of commands remembered by the history mechanism. PATH - A list of directories in which ksh searches for executable programs. PS1 - Primary prompt string. In an non-interactive process, this variable is not initialized.

Copyright: Sela Software Labs and Amdocs

U1

Constant and Known Names of Variables contd




Chapter 12 - Working with ksh - 20

PS2, PS3, PS4 RANDOM -

Secondary prompts, will be discussed later. When used with other shell mechanisms. Each time this variable is referenced, a random integer, uniformly distributed between 0 and 32767, is generated Each time this variable is referenced, the number of seconds since shell invocation is returned.

SECONDS -

Examples:
$ print 24227 $ print 19401 $ print 13429 $ print 13431 $RANDOM $RANDOM $SECONDS $SECONDS

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 21

Environment Variables
   

The ksh saves a list of the environment variables. Accessing an environment variable the same as accessing ordinary ones. The environment variables are duplicated for a child process. You can transform a variable into an environment variable by using the command export. Example:
$ X=file $ export X $ export ABC=123

 

Viewing the environment variables and their values :


$ env

Its impossible to assign more than one value as an array to an environment variable.
( PATH = One string with more then one value )
Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 22

Environment Variables - Example


$ export a=abc $ b=abc $ ksh $ print $a abc $ print $b $ export c=cde $ ksh $ print $a abc $ print $b $ print $c
Copyright: Sela Software Labs and Amdocs

cde $ csh % echo $a abc % echo $b b: Undefined variable % exit $ export d=ghi $ exit $ print $d $ exit

U1

Chapter 12 - Working with ksh - 23

typeset


% typeset [ -HLRZfilrtux[n] ] [ name[=value ]] Sets attributes and values for shell variables and functions. We will discuss functions later on. -L Left justify and remove leading blanks from value. $ a=" alon" $ print $a alon $ print "$a" alon $ typeset -L a $ print "$a" alon
Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 24

typeset contd
-R -Z Right justify and fill with leading blanks. Right justify and fill with leading zeros if the first non-blank character is a digit. $typeset R10 c=abc $print $c abc Why do we need the $typeset Z5 d=42 around the variable? $print $d 00042 All upper-case characters are converted to lowercase. All lower-case characters are converted to uppercase.

-l -u

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 25

typeset Variable Types


-i Parameter is an integer. This makes arithmetic faster. The given names are marked read only and these names cannot be changed by subsequent assignment. $typeset -i a=10 $ a=value ksh: value: bad number $ b=value $ typeset r b $ b=new_value ksh: b: is read only

-r

Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 26

typeset
-x
 

same as the command export.

Using + rather than - causes these flags to be turned off. If no name arguments are given but flags are specified, a list of names (and optionally the values) of the variables which have these flags set is printed. (Using + rather than - keeps the values from being printed.) If no names and flags are given, the names and attributes of all variables are printed. Example: $ typeset -i a=10 x=100 :
Copyright: Sela Software Labs and Amdocs

U1

Chapter 12 - Working with ksh - 27

Joining Commands
; & |
separates commands in the same line for synchronous execution. grep apple fruit;egrep door house separates commands in the same line for asynchronous execution. grep apple fruit & egrep door house pipe, connects stdout to stdin sort file | lpr

Command Substitution - pass output as arguments $() lpr `ls *.c` lpr $(ls *.c)

() && ||

command grouping: executes the command/s in sub-ksh, and combine the output for joint posting through the pipe. (egrep apple fruit;egrep door house) | sort the command is executed if the preceding command was successfully executed. cc -c main.c && cc -o final main.o subs.o the command is executed if the preceding command failed. cc -c *.c 2> /tmp/a || mail $user < /tmp/a
Copyright: Sela Software Labs and Amdocs

Você também pode gostar