Shell Functions  «Prev  Next»
Lesson 5A script that uses functions
Objective Write a script that uses multiple functions.

Script that uses Multiple Functions

Scripts are often organized by using functions. All tasks that the script performs are copied into functions. At the bottom of the script, the overall controlling loops or commands are included and the functions are called from these statements. To decipher a script written in this fashion, read the code at the bottom of the script, the main program. This gives you a general idea of what the script does. Once you understand this, you can look at the individual functions to see the details of how the tasks are performed.

Multi-Function

Here's an example shell script that contains two functions, one to square an input and the other to capitalize the first letter of each word:
#!/bin/bash

# Function to square a number
square() {
  result=$(echo "$1 * $1" | bc)
  echo $result
}

# Function to capitalize the first letter of each word
capitalize() {
  echo $* | awk '{for(i=1;i<=NF;i++){$i=toupper(substr($i,1,1)) substr($i,2)};print}'
}

# Main script

# Call the square function with an input of 5
squared=$(square 5)
echo "5 squared is: $squared"

# Call the capitalize function with a string input
capitalized=$(capitalize this is a test string)
echo "Capitalized string: $capitalized"

In this script, the square function takes a single input and uses the bc command to calculate the square of that input. The result is then printed to the console. The capitalize function takes a string input and uses the awk command to capitalize the first letter of each word in the string. The capitalized string is then printed to the console. In the main part of the script, the two functions are called with example inputs and the results are printed to the console. Note that the capitalize function is called with a string input that contains spaces, which are passed as separate arguments to the function using the $* special parameter.

Shell Scripting Technologies

  #! /bin/sh pause_screen () { 
	echo “press return to continue\c” 
	read junk clear 
	} 
	enter3nums () { 
	 echo “Enter the first number: \c
	 ” read num1 echo “
	Enter second number: \c” read num2 echo “
	Enter third number: \c” read num3 
  } 
  add3nums () { 
   total=`expr $num1 + $num2 + $num3` 
  } 
  print3nums () { 
   echo “The sum of $num1 $num2 and $num3” 
   echo “is $total” pause_screen 
  } 
  # main program enter3nums add3nums print3nums 
<end of graphic>
pause_screen (), This starts the function definition.
enter3nums (), This starts the function definition.
add3nums (), This starts the function definition.
print3nums(), This starts the function definition.
pause_screen, The pause_screen function is called from the print3nums function.
# main program, This is a comment announcing the start of the main program.
(highlight all three of these lines at once in the mouseover)
enter3nums
add3nums
print3nums, This is the main program. Reading the function calls tells you what the function does.
If you want to know more about using functions versus other scripts, look at this.

Using functions versus using Other Scripts

The course project main script, volleyball, calls scripts that are stored in separate files to perform various tasks. You can take those separate scripts and include them as functions inside the volleyball script instead of implementing them as separate scripts. When should you store a task in a function and when should you store a task in a separate script? It depends on the circumstances. Let us look at a list of the advantages and disadvantages of both methods:

Using the separate script method

Advantages Disadvantages
  1. You can run a script without running the script that calls it. This means you can debug your code one script at a time.
  2. An error in one of your scripts won’t affect the lines of code in the other scripts.
  3. You can use the ls command to see a list of your scripts.
  1. Copying your script to another computer involves copying multiple files. If you miss one file, your program won’t fully work.
  2. Scripts have more memory and CPU overhead, so they will run slower than functions.
  3. You cannot see variables created in scripts that you call.

The next lesson describes sharing data with a function.

Script uses Functions - Exercise

Click the Exercise list to sort a set of function definitions and calls into correct order.
Script uses Functions - Exercise