Escolar Documentos
Profissional Documentos
Cultura Documentos
U1
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.
U1
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
U1
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
U1
Example:
$ print ${color[100]} Blue
U1
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
U1
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
U1
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.
U1
Question: Why does ksh have two command substitution syntax? Hint: $ command $(command $(command))
U1
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
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
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
U1
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 ))
Operators: + - * / % ()
U1
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
U1
U1
U1
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.
U1
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.
U1
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
U1
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
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
cde $ csh % echo $a abc % echo $b b: Undefined variable % exit $ export d=ghi $ exit $ print $d $ exit
U1
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
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
U1
-r
U1
typeset
-x
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
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