Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of variables in KSH

I have written a sample KornShell function to split a String, put it in an array and then print out the values. The code is as below

#!/usr/bin/ksh

splitString() {

    string="[email protected];[email protected];[email protected]"

    oIFS="$IFS"; 
    IFS=';' 
    set -A str $string
    IFS="$oIFS"
}

splitString
echo "strings count = ${#str[@]}"
echo "first : ${str[0]}";
echo "second: ${str[1]}";
echo "third : ${str[2]}";

Now the echo does not print out the values of the array, so I assume it has something to do with the scope of the array defined.

I am new to Shell scripting, can anybody help me out with understanding the scope of variables in the example above?

like image 307
Vivek Avatar asked Aug 17 '12 06:08

Vivek


People also ask

What are the scopes of variables?

In simple terms, scope of a variable is its lifetime in the program. This means that the scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.

What is the scope of a shell variable?

A environment variable defined in a shell (or a shell script) stays defined everywhere, including inside new shells (or shell scripts) executed within the shell, until the shell (or the shell script) terminates or the variable gets unset explicitly.

What is scope of variable with example?

It means the variable a can be used anywhere in the program. In the above program, variable a is a global variable. The value of a is hello . Then the variable a is accessed inside a function and the value changes to 3.

How do you write the scope of a variable?

A scope is a region of the program and broadly speaking there are three places, where variables can be declared: Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters. Outside of all functions which is called global variables.


2 Answers

The default scope of a variable is the whole script.

However, when you declare a variable inside a function, the variable becomes local to the function that declares it. Ksh has dynamic scoping, so the variable is also accessible in functions that are invoked by the function that declares the variable. This is tersely documented in the section on functions in the manual. Note that in AT&T ksh (as opposed to pdksh and derivatives, and the similar features of bash and zsh), this only applies to functions defined with the function keyword, not to functions defined with the traditional f () { … } syntax. In AT&T ksh93, all variables declared in functions defined with the traditional syntax are global.

The main way of declaring a variable is with the typeset builtin. It always makes a variable local (in AT&T ksh, only in functions declared with function). If you assign to a variable without having declared it with typeset, it's global.

The ksh documentation does not specify whether set -A makes a variable local or global, and different versions make it either. Under ksh 93u, pdksh or mksh, the variable is global and your script does print out the value. You appear to have ksh88 or an older version of ksh where the scope is local. I think that initializing str outside the function would create a global variable, but I'm not sure.

Note that you should use a local variable to override the value of IFS: saving to another variable is not only clumsy, it's also brittle because it doesn't restore IFS properly if it was unset. Furthermore, you should turn off globbing, because otherwise if the string contains shell globbing characters ?*\[ and one of the words happens to match one or more file on your system it will be expanded, e.g. set -A $string where string is a;* will result in str containing the list of file names in the current directory.

set -A str
function splitString {
  typeset IFS=';' globbing=1
  case $- in *f*) globbing=;; esac
  set -f
  set -A str $string
  if [ -n "$globbing" ]; then set +f; fi
}
splitString "$string"
like image 72
Gilles 'SO- stop being evil' Avatar answered Sep 23 '22 00:09

Gilles 'SO- stop being evil'


Variables are normally global to the shell they're defined in from the time they're defined.

The typeset command can make them local to the function they're defined in, or alternatively to make them automatically exported (even when they're updated.)

Read up "typeset" and "integer" in the manpage, or Korn's book.

like image 44
abe_crabbers Avatar answered Sep 26 '22 00:09

abe_crabbers