Ch12-Working With Ksh

Embed Size (px)

Citation preview

  • 8/3/2019 Ch12-Working With Ksh

    1/27

    Copyright: Sela Software Labs and Amdocs

    Chapter 12

    Working with ksh

  • 8/3/2019 Ch12-Working With Ksh

    2/27

    U1

    Chapter12 - Working with ksh - 2

    Copyright: Sela Software Labs and Amdocs

    Variables - Why ?

    Special values for the ksh, define a compatible workenvironment, according to the programmers needs.

    Shortcut of commands - aliases.

    There are some variables with predefined names and specialtasks.

    Variables are used in scripts.

  • 8/3/2019 Ch12-Working With Ksh

    3/27

  • 8/3/2019 Ch12-Working With Ksh

    4/27

    U1

    Chapter12 - Working with ksh - 4

    Copyright: Sela Software Labs and Amdocs

    Single Value Variables and

    Multi Value Variables

    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

  • 8/3/2019 Ch12-Working With Ksh

    5/27

    U1

    Chapter12 - Working with ksh - 5

    Copyright: Sela Software Labs and Amdocs

    Single Value Variables and

    Multi Value Variables contd 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

  • 8/3/2019 Ch12-Working With Ksh

    6/27

    U1

    Chapter12 - Working with ksh - 6

    Copyright: Sela Software Labs and Amdocs

    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

  • 8/3/2019 Ch12-Working With Ksh

    7/27

    U1

    Chapter12 - Working with ksh - 7

    Copyright: Sela Software Labs and Amdocs

    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) byusing ${#varname[*]}

    Example:$ set -A x 111 222 333 4 5

    $ print ${#x[*]}

    5

  • 8/3/2019 Ch12-Working With Ksh

    8/27

    U1

    Chapter12 - Working with ksh - 8

    Copyright: Sela Software Labs and Amdocs

    Command Substitution

    Allows the output of a command to be substituted in place of thecommand name itself.

    Command substitution occurs when the command is enclosedas follows:

    $(command)

    or

    `command`

    The shell will expand the command substitution by executingcommand in a subshell environment and replacing thecommand (the text of the command plus the enclosing $( ) or

    backquotes) with the standard output of the command.

  • 8/3/2019 Ch12-Working With Ksh

    9/27

    U1

    Chapter12 - Working with ksh - 9

    Copyright: Sela Software Labs and Amdocs

    Using Command Substitution

    $ command $(sub command)

    or

    $ command `sub command`

    Question:

    Why does ksh have two command substitution syntax?

    Hint:

    $ command $(command $(command))

  • 8/3/2019 Ch12-Working With Ksh

    10/27

    U1

    Chapter12 - Working with ksh - 10

    Copyright: Sela Software Labs and Amdocs

    Main Uses for Command

    Substitution

    Inserting a command result into a variable:

    $ set -A X $(cat file)

    Passing parameters to the primary command:$ lpr $(egrep -limportant *trash*)

    Remark:

    Pay attention to the difference:

    $ egrep -limportant *trash* | lpr

    The command substitution $(cat file) can be replaced by theequivalent but faster$(

  • 8/3/2019 Ch12-Working With Ksh

    11/27

    U1

    Chapter12 - Working with ksh - 11

    Copyright: Sela Software Labs and Amdocs

    Command Substitution

    More Facts

    You can get the result of sub-command as a set of strings or asa 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

  • 8/3/2019 Ch12-Working With Ksh

    12/27

    U1

    Chapter12 - Working with ksh - 12

    Copyright: Sela Software Labs and Amdocs

    More Examples

    View the files that contain the word salary:

    $ cat $(grep -l salary *)

    View the content of the largest file:$ set Aarr $(ls -s | tail +2 | sort -n | tail 1)

    $ cat ${arr[1]}

    Thefileslistandtheirsizes

    Sort the file list andremove the -total- line

    View thefilescontent

  • 8/3/2019 Ch12-Working With Ksh

    13/27

    U1

    Chapter12 - Working with ksh - 13

    Copyright: Sela Software Labs and Amdocs

    Arithmetic Expressions

    Allows the result of an arithmetic expression be substituted inplace of the expression.

    arithmetic expression substitution occurs when the command isenclosed as follows:

    $(( expression ))

    The arithmetic expression substitution provides only integerarithmetic.

    Examples:$ print $(( 2 + 2 ))

    4

    $ x=6

    $ x=$(($x - 1))

    $ print $x

    5

    Operators:

    + - * / % ()

  • 8/3/2019 Ch12-Working With Ksh

    14/27

    U1

    Chapter12 - Working with ksh - 14

    Copyright: Sela Software Labs and Amdocs

    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 expansionof word will be substituted. otherwise, the value of parameterwill be substituted.

    $ print ${a:-empty}

    empty

    $print $a

    $a=value

    $print ${a:-empty}

    value

  • 8/3/2019 Ch12-Working With Ksh

    15/27

    U1

    Chapter12 - Working with ksh - 15

    Copyright: Sela Software Labs and Amdocs

    Variable Expansion contd

    ${variable:=word}

    Assign Default Values. If variable is unset or null, theexpansion of word will be assigned to variable.

    $ unset a

    $ print ${a}

    $ print ${a:=empty}

    empty

    $ print ${a}empty

    $ print ${a:=full}

    empty

  • 8/3/2019 Ch12-Working With Ksh

    16/27

    U1

    Chapter12 - Working with ksh - 16

    Copyright: Sela Software Labs and Amdocs

    Variable Expansion contd

    ${variable:?[word]}

    Indicate error if null or unset.

    If variable is unset or null, the expansion of word (or amessage indicating it is unset if word is omitted) will bewritten to standard error

    In a shell script, the script will exit with a non-zero exitstatus.

    $ a=full

    $ print ${a:?error}

    full

    $ unset a

    $ print ${a:?error}

    error

    $ print ${a}

    $ b=invalid variable

    $ print ${a:?$b}

    invalid variable

    $ print ${a:?}

    ksh: a: parameter null or not set

  • 8/3/2019 Ch12-Working With Ksh

    17/27

    U1

    Chapter12 - Working with ksh - 17

    Copyright: Sela Software Labs and Amdocs

    Variable Expansion contd

    ${variable:+[word]}

    Use Alternate Value. If variable is unset or null, null will besubstituted, otherwise, the expansion ofwordwill besubstituted.

    $ unset a

    $ print ${a:+full}

    $ a=value

    $ print ${a:+full}full

    $ print $a

    value

  • 8/3/2019 Ch12-Working With Ksh

    18/27

    U1

    Chapter12 - Working with ksh - 18

    Copyright: Sela Software Labs and Amdocs

    Variable Expansion contd

    There are more options for variable expansion.

    There are ways to distinguish between unset variables andexisting variables assigned a null value.

    Some of the operators can be used with other shell mechanismssuch as wildcards and command substitution.

    There are operators that can cut suffixes and prefixes.

    Read man ksh for more information.

  • 8/3/2019 Ch12-Working With Ksh

    19/27

    U1

    Chapter12 - Working with ksh - 19

    Copyright: Sela Software Labs and Amdocs

    Constant and Known

    Names of Variables

    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 gohome by using cd.

    HISTSIZE - The number of commands remembered by the

    history mechanism.

    PATH

    - A list of directories in which ksh searches forexecutable programs.

    PS1 - Primary prompt string. In an non-interactive process,

    this variable is not initialized.

  • 8/3/2019 Ch12-Working With Ksh

    20/27

    U1

    Chapter12 - Working with ksh - 20

    Copyright: Sela Software Labs and Amdocs

    Constant and Known

    Names of Variables contd

    PS2, PS3, PS4 - Secondary prompts, will be discussedlater. When used with other shell mechanisms.

    RANDOM- Each time this variable is referenced, a randominteger, uniformly distributed between 0 and32767, isgenerated

    SECONDS -E

    ach time this variable is referenced, thenumber of secondssince shell invocationisreturned.

    Examples:$ print $RANDOM

    24227

    $ print $RANDOM19401

    $ print $SECONDS

    13429

    $ print $SECONDS

    13431

  • 8/3/2019 Ch12-Working With Ksh

    21/27

    U1

    Chapter12 - Working with ksh - 21

    Copyright: Sela Software Labs and Amdocs

    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 anenvironment variable.( PATH = One string with more then one value )

  • 8/3/2019 Ch12-Working With Ksh

    22/27

    U1

    Chapter12 - Working with ksh - 22

    Copyright: Sela Software Labs and Amdocs

    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

    cde$ csh

    % echo $a

    abc

    % echo $b

    b: Undefined variable% exit

    $ export d=ghi

    $ exit

    $ print $d

    $ exit

  • 8/3/2019 Ch12-Working With Ksh

    23/27

    U1

    Chapter12 - Working with ksh - 23

    Copyright: Sela Software Labs and Amdocs

    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 fromvalue.

    $ a=" alon"

    $ print $a

    alon

    $ print "$a"

    alon$ typeset -L a

    $ print "$a"

    alon

  • 8/3/2019 Ch12-Working With Ksh

    24/27

    U1

    Chapter12 - Working with ksh - 24

    Copyright: Sela Software Labs and Amdocs

    typeset contd

    -R Right justify and fill with leading blanks.-Z Right justify and fill with leading zeros if the first

    non-blank character is a digit.

    $typeset R10 c=abc

    $print $c

    abc$typeset Z5 d=42

    $print $d

    00042

    -l All upper-case characters are converted to lower-case.

    -u All lower-case characters are converted to upper-case.

    Why do we need the around the variable?

  • 8/3/2019 Ch12-Working With Ksh

    25/27

    U1

    Chapter12 - Working with ksh - 25

    Copyright: Sela Software Labs and Amdocs

    typeset Variable Types

    -i Parameter is an integer. This makesarithmetic faster.

    -r The given names are marked read onlyand these names cannot be changed bysubsequent assignment.

    $typeset -ia=10

    $ a=value

    ksh: value: badnumber

    $ b=value

    $ typeset r b

    $ b=new_value

    ksh: b: is read only

  • 8/3/2019 Ch12-Working With Ksh

    26/27

    U1

    Chapter12 - Working with ksh - 26

    Copyright: Sela Software Labs and Amdocs

    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 ofnames (and optionally the values) of the variables which have

    these flags set is printed. (Using + rather than - keeps thevalues from being printed.)

    If no names and flags are given, the names and attributes of allvariables are printed.

    Example:

    $ typeset -i

    a=10

    x=100

    :

  • 8/3/2019 Ch12-Working With Ksh

    27/27

    U1

    Chapter12 - Working with ksh - 27

    Copyright: Sela Software Labs and Amdocs

    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 stdinsort file | lpr

    $()

    Command Substitution - pass output as arguments

    lpr `ls *.c` lpr $(ls *.c)

    ()command grouping: executes the command/s in sub-ksh, andcombine the output for joint posting through the pipe.

    (egrep apple fruit;egrep door house) | sort

    &&the command is executed if the preceding command wassuccessfully executed.cc -c main.c && cc -o finalmain.o subs.o

    ||the command is executed if the preceding command failed.

    cc -c *.c 2> /tmp/a || mail $user < /tmp/a