Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

📄 Contents

  1. Using Functions
  2. Understanding Scope, Recursion, Return Codes, and Data Sharing
  3. Summary
  4. Terms
This chapter is from the book

Understanding Scope, Recursion, Return Codes, and Data Sharing

Now that you have a basic understanding of the use and operation of functions in shell scripts, let's look at more advanced topics such as scope, recursion, return codes, and data sharing.

Scope

The term scope refers to the region within a program where a variable's value can be accessed. There are two types of scope:

  • Global scope If a variable has global scope, its value can be accessed from anywhere within a script. Variables with global scope are referred to as global variables.

  • Local scope If a variable has local scope, its value can only be accessed within the function in which it is declared. Variables with local scope are referred to as local variables.

By default all variables, except for the special variables associated with function arguments, have global scope. In ksh, bash, and zsh, variables with local scope can be declared using the typeset command. The typeset command is discussed later in this chapter. This command is not supported in the Bourne shell, so it is not possible to have programmer-defined local variables in scripts that rely strictly on the Bourne shell.

Global Variables

The following script illustrates the behavior of global variables:

#!/bin/sh

pearFunc () {
  pear=2;                 # set $pear
  echo "In pearFunc(): pear is $pear"   # print out its value
}

pearFunc                  # call pearFunc
echo "Outside of pearFunc(): pear is $pear" # print out $pear

First the script defines a function, pearFunc, that sets the value of the global variable $pear (all variables are global by default) and outputs that value. Then the script executes pearFunc. Finally, the script prints the value of $pear outside of the function. The output is

In pearFunc(): pear is 2
Outside of pearFunc(): pear is 2

As you can see from the output, the value assigned to the variable $pear in the function pearFunc is accessible outside of pearFunc.

A common use for global variables is to communicate information from a function to the main script, as illustrated in the following script:

#!/bin/sh

readPass () {
  PASS=""           # clear password
  echo -n "Enter Password: "  # print the prompt
  stty –echo          # turn off terminal echo to prevent peeping!
  read PASS          # read the password
  stty echo          # restore terminal echo
  echo             # printout a new line to make output nice
}

readPass
echo Password is $PASS

This script uses the readPass function to read in a password from the user. The readPass function reads the password and stores it in the global variable PASS. The script then accesses the password using the variable PASS.

The readPass function is quite simple. It function starts by undefining PASS. Then it issues a prompt for the password and deactivates terminal echo using the stty –echo command. Terminal echo is deactivated because you don't want someone other than the user to inadvertently see the password. Next, you read the password and store its value in PASS by using the read command. Finally, you restore terminal echo using the stty echo command and echo a new line.

Local Variables

Local variables are defined using typeset command:

typeset var1[=val1] ... varN[=valN]

Here, var1 ... varN are variable names and val1 ... valN are values to assign to the variables. The values are optional as the following example illustrates:

typeset fruit1 fruit2=banana

This command declares two local variables, fruit1 and fruit2, and assigns the value banana to the variable fruit2.

The following script illustrates the behavior of local variables:

#!/bin/sh

pearFunc () {
  typeset pear=2;             # set $pear
  echo "In pearFunc(): pear is $pear"   # print out its value
}

pearFunc                  # call pearFunc
echo "Outside of pearFunc(): pear is $pear" # print out $pear

First, the script defines a function, pearFunc, which sets the value of a local variable $pear and outputs that value. Then the script executes pearFunc. Finally, the script prints the value of $pear outside of the function. The output is

In pearFunc(): pear is 2
Outside of pearFunc(): pear is

From the output, you can see that when the value of $pear is accessed within the pearFunc it has the value 2, but when the value of $pear is accessed outside the function, it has no value.

Recursion

In the previous section, you learned about the concept of function chaining, where one function calls another function. Recursion is a special instance of function chaining in which a function calls itself. The following example illustrates the use of recursion:

reverse() {
  if [ $# -gt 0 ] ; then
   typeset arg="$1"
   shift
   reverse "$@"
   echo "$arg "
  fi
}

reverse "$@"

This script prints its arguments in reverse order. It does so by calling the function reverse with $@ as the arguments. The reverse function is really simple; it determines whether there are any arguments. If there are no arguments, the function does nothing. Otherwise, it saves the first argument, removes it from the argument list using shift and calls itself. Once this call returns, the function just prints the argument it saved.

If you name the script reverse.sh and execute it with the arguments a b c, as follows:

#!/bin/sh

readPass () {
  PASS=""           # clear password
  echo -n "Enter Password: "  # print the prompt
  stty –echo          # turn off terminal echo to prevent peeping!
  read PASS          # read the password
  stty echo          # restore terminal echo
  echo             # printout a new line to make output nice
}

readPass
echo Password is $PASS

The output is

c
b
a

NOTE

In the previous example, you executed the script using /bin/sh. This will not work on Solaris and SunOS systems. On those systems, you need to execute the script using /bin/ksh rather than /bin/sh as /bin/sh on Solaris does not support the typeset command.

The execution of this script proceeds as follows:

  1. The script executes reverse "$@" (effectively it calls reverse a b c).

  2. The function reverse determines whether $# (the number of arguments) is greater than 0. In this case, $# will be equal to 3 (a b c).

  3. Because $# is greater than 0, reverse saves the first argument, $1 (in this case a) in the local variable $arg, and then calls shift to remove it from $@. Now, $@ holds two arguments, b c.

  4. The function reverse calls itself with the shortened $@.

  5. The function reverse determines whether $# (the number of arguments) is greater than 0. In this case, $# will be equal to 2 (b c).

  6. Because $# is greater than 0, reverse saves the first argument, $1 (in this case b) in the local variable $arg, and then calls shift to remove it from $@. Now, $@ holds just one argument, c.

  7. The function reverse calls itself with the shortened $@.

  8. The function reverse determines whether $# (the number of arguments) is greater than 0. In this case, $# will be equal to 1.

  9. Because $# is greater than 0, reverse saves the first argument, $1 (in this case c) in the local variable $arg, and then calls shift to remove it from $@. Now, $@ holds no arguments.

  10. The function reverse calls itself with the shortened $@.

  11. The function reverse determines whether $# (the number of arguments) is greater than 0. Because there are no arguments in $@, this check fails and the function returns.

  12. After the call to reverse returns, you output the value of the local variable $arg, in this case c, and return.

  13. After the call to reverse returns, you output the value of the local variable $arg, in this case b, and return.

  14. After the call to reverse returns, you output the value of the local variable $arg, in this case a, and return.

Divide and Conquer

Recursion is normally used to solve problems using a technique known as divide and conquer. Basically, divide and conquer means that a problem is divided into smaller and smaller instances until an instance that is small enough to solve directly is found. Each instance that is too big to solve directly is solved recursively, and the solutions are combined to produce a solution to the original problem.

You used divide and conquer in the previous example; the function reverse kept calling itself with smaller and smaller parts of the argument list $@ until all the arguments were exhausted, and then it just printed each argument.

Return Codes

When a shell script completes, it can use the exit command to return exit status via an exit code. The function analogue to exit is the return command. This command allows function to return exit status. The exit status from a function is called its return code. The convention for return codes is the same as for exit codes; a 0 equals success and a nonzero equals failure.

The syntax of the return command is

return rc

Here rc is the return code. The following function illustrates the use of return:

isInteractive () {
  case $- in      # $- holds the invocation options
   *i*) return 0;;  # if $- contains i, the shell is interactive
  esac
  return 1
}

You can use this function to detect whether a particular shell is interactive as follows:

if isInteractive ; then
  echo "Interactive shell"
else
  echo "Non-interactive shell"
fi

Data Sharing

The functions you have seen thus far are mostly independent, but in most shell scripts functions either depend on or share data with other functions. In this section, you will look at an example in which three functions work together and share data.

Moving Around the File System

The C shell, csh, introduced three commands for quickly moving around in the UNIX directory tree:

  • popd

  • pushd

  • dirs

These commands maintain a stack of directories internally and enable the user to add and remove directories from the stack and list the contents of the stack.

Understanding Stacks

For those readers who are not familiar with the programming concept of a stack, you can think of it as a stack of plates: you can add or remove a plate only at the top of the stack. You can access only the top plate, not any of the middle plates in the stack. A stack in programming terms is similar. You can add or remove an item only at the top of the stack.

These commands are not available in Bourne shell or ksh. Newer versions of bash and zsh have introduced these commands. In this section, you will implement each of these commands as shell functions so that they can be used with any Bourne-like shell.

In csh, the directory stack used by these commands is maintained within the shell; in this implementation you will maintain the stack as an global exported environment variable, called _DIR_STACK. The entries in _DIR_STACK are separated by colons, :, just like entries in PATH or MANPATH. This allows you to handle almost any directory name.

Implementing dirs

First let's look at the simplest of the three functions, dirs. This function just lists the entries in the directory stack:

dirs() { 

  OLDIFS="$IFS"     # save IFS (internal field separator)
  IFS=:         # set IFS to :, so that we can process
             # each entry in _DIR_STACK easily

  for i in $_DIR_STACK  # print out each entry in _DIR_STACK
  do
   echo "$i \c"
  done

  echo          # print out new line (makes output pretty)

  IFS="$OLDIFS"     # restore IFS
}

First, you save the current value of IFS in OLDIFS and then you set IFS to :. Because IFS is the Internal Field Separator for the shell, modifying it allows you to use the for loop to cycle through the individual entries in _DIR_STACK. When you are finished with all the entries, you restore the value of IFS.

NOTE

The shell uses the value of the variable IFS to split up a string into separate words. The default setting for IFS is the space and tab characters. This enables the shell to determine the number of words that are in most strings. Normally, the shell uses the default value of IFS to determine how many options are supplied to a command, script, or shell function along with how many items are specified to a for loop.

In the previous example, you manipulated the value of IFS in order to simplify the processing of the entries in _DIR_STACK.

Implementing pushd

The pushd function is a bit more complicated than the dirs function. In addition to listing the directories in the stack, it must also change to a requested directory and then add that directory to the top of the stack. The requested directory is the first argument to the function. If an argument is not specified, the current directory (.) is used.

This example implements pushd as follows:

pushd() {

  # set REQ to the first argument (if given, otherwise use .)

  REQ="${1:-.}"

  # if $REQ is not a directory, print an error and return

  if [ ! -d "$REQ" ] ; then
   echo "ERROR: $REQ is not a directory." 1>&2
   return 1
  fi

  # if we can cd to $REQ, update _DIR_STACK and print it out
  # otherwise print an error and return

  if cd "$REQ" > /dev/null 2>&1 ; then
   _DIR_STACK="´pwd´:$_DIR_STACK" ; export _DIR_STACK ;
   dirs
  else
   echo "ERROR: Cannot change to directory $REQ." >&2
   return 1
  fi

  unset REQ
}

This function starts by determining the directory to push onto the stack. It uses the default value substitution form of variable substitution, covered in Chapter 9, "Substitution," to obtain this value. Then, the function determines whether the requested directory is really a directory. If it is not a directory, you print an error and return 1 to indicate failure. Otherwise, you change to that directory and then update the directory stack with the full path of the new directory. You have to use the full path rather than value in $REQ, because the value stored in $REQ might be a relative path. After the directory stack has been updated, you call dirs to output the directories stored on the stack.

Implementing popd

The popd() function is much more complicated than the other two functions. Let's look at the operations it performs:

  1. Removes the first entry from the directory stack

  2. Updates the directory stack to reflect the removal

  3. Changes to the directory indicated by the entry that was removed from the stack

  4. Displays the full path of the current directory

To simplify the first and second operations, you can implement a helper function for popd() called _popd_helper(). This function performs all the work; popd() is simply a wrapper around it. Often you need to write functions in this manner: one function that provides a simple interface and another that performs the actual work.

Implementing _popd_helper

Let's first look at the function _popd_helper to see how the directory stack is manipulated:

_popd_helper() {

  # set the directory to pop to the first argument, if 
  # this directory is empty, issue an error and return 1
  # otherwise get rid of POPD from the arguments

  POPD="$1"
  if [ -z "$POPD" ] ; then
    echo "ERROR: The directory stack is empty." >&2
    return 1
  fi
  shift

  # if any more arguments remain, reinitalize the directory
  # stack, and then update it with the remaining items, 
  # otherwise set the directory stack to null

  if [ -n "$1" ] ; then 
    _DIR_STACK="$1" ; 
    shift ;
    for i in $@ ; do _DIR_STACK="$_DIR_STACK:$i" ; done
  else
    _DIR_STACK=
  fi

  # if POPD is a directory cd to it, otherwise issue
  # an error message

  if [ -d "$POPD" ] ; then
    cd "$POPD" > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
      echo "ERROR: Could not cd to $POPD." >&2
    fi
    pwd
  else
    echo "ERROR: $POPD is not a directory." >&2
  fi

  export _DIR_STACK
  unset POPD
}

This function expects each of the directories in the directory stack to be given to it as arguments, so the first thing that it checks is whether $1, the first argument, has any value. You do this by setting $POPD equal to $1 and then checking if $POPD has a value. If the directory stack is empty, you issue an error message and return; otherwise, you shorten the stack using shift. At this point, you have taken care of the first operation.

Next, you determine whether the directory stack became empty after you removed an entry from it. Because the individual items in the stack are the arguments to this function, you need to check whether $1, the new first argument, has a value. If it does, you reinitialize the directory stack with this value and proceed to add all the remaining values back onto the stack; otherwise, you set the value of the directory stack to null. At this point, you have taken care of the second operation.

The final if statement takes care of the third and fourth operations. Here, you determine whether the path stored in $POPD is a directory. This check is required because the path might have been removed from the system after it was added to the directory stack. If the path is a directory, you try to cd to that directory. If the change is successful, you print the full path to the directory, otherwise you print an error message.

The Wrapper Function

Now that you know how the helper function works, you can write an appropriate wrapper function to translate the value of _DIR_STACK into separate arguments. This is fairly easy, thanks to IFS.

The popd() function is

popd() {
  OLDIFS="$IFS"
  IFS=:
  _popd_helper $_DIR_STACK
  IFS="$OLDIFS"
}

In this function, you first save the old value of IFS. Then you set IFS to : and call _popd_helper with the directory stack specified as arguments. After _popd_helper returns, you restore the value of IFS.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020